Anda di halaman 1dari 8

ALGORITHM DESIGN AND ANALYSIS

1. (10 pts) Illustrate the operation of Insertion-sort algorithm on array


A=<31,41,59,26,41,58>.
ANSWER:
The operation of Insertion Sort algorithm can be explained on the array in the
following way:

For first iteration we will start with the second element in the following way
say, key=41. The iterations will occur for n-1 elements i.e, from the second
element till the last one.

Compare 31 to 41.
Since 31<41, no change occurs.
We move to the third element.

Now key=59.
Compare 59 with 41. Since 41<59, no change occurs.
We move to the fourth element.

Now key=26.
Compare 26 with 59. Since 59>26, 26 shifts to the position of 59.
31,41,26,59,41,58.

Compare 26 to 41. Since 41>26, 26 shifts to the position of 41.


31,26,41,59,41,58.

Compare 26 to 31. Since 31>26, 26 shifts to the position of 31.


26,31,41,59,41,58.

Now key=41.
Compare 41 with 59. Since 59>41, 41 shifts to the position of 59.
26,31,41,41,59,58.

Compare 41 with 41. Since 41!>41, 41 no change occurs.


26,31,41,41,59,58.

Now key=58.
Compare 58 with 59. Since 59>58, 58 shifts to the position of 59.
26,31,41,41,58,59.

Compare 58 with 41. Since 41!>58, no change occurs.

26,31,41,41,58,59.
2. (10 pts) The input to the algorithm Unknown illustrated below is an array
A of N
numbers. (1) What is the output of the algorithm? (2) Using big-O notation
to show
the running time of the algorithm.
Input: Array A of N numbers;
Unknown(A)
for j = 1 to N-1
if A[N] < A[j]
exchange A[j] and A[N]
Output A[N];
ANSWER:
Consider an array of 5 elements:
A[5]={3,4,9,2,1}
For j=1 to 4

A[5]<A[1] (1<3)
A[5]=3
A[1]=1
The array is 1,4,9,2,3

Then compare A[5]<A[2] (3<4)


A[5]=4
A[2]=3
The array is 1,3,9,2,4

Then compare A[5]<A[3] (4<9)


A[5]=9
A[3]=4
The array is 1,3,4,2,9

Then compare A[5]<A[4] (9>2)


No change occurs.
The array is 1,3,4,2,9
The operation now comes out of the loop with the output A[5]=9 i.e, the largest
element of the array.

RUNNING TIME OF ALGORITHM:


In the above algorithm, the running time is O(n) because the performance will grow
linearly and in direct proportion to the size of the input data set.
For e.g. If the no. of elements in array is 2 the iteration is 1.
If the no. of elements in array is 3 the iteration is 2. Similarly if the no. of
elements in the array is n then the iterations will be n-1. Therefore, the running time
of the algorithm is O(n).

3. What is the worst case and best case running time of selection-sort? How
does it compare to insertion sort?

ANSWER:

For the selection sort, lowest element is selected from n elements and moved
to the first position and this requires n-1 comparisons. Then the next lowest
element is searched which requires n-2 comparisons and so on. Therefore, for:
(n-1)+(n-2)++2+1=n(n-1)/2=O(n^2) comparisons.

With the selection sort method, the time complexity for each case (worst and best) is
the
same, because whatever the array looks like, the function will go
through all the values in it. So the number of comparisons and assignments is
independent of the data.

Insertion Sort is very similar to Selection Sort but only till the nth iteration,
where first n elements in the array are sorted. Insertion Sort will scan as
elements to place n+1st element while selection sort has to scan all the
elements to find out the n+1st element. That means that when the array is
already sorted or almost sorted, insertion sort performs in O(n) time.
Simple calculation shows that insertion sort will therefore usually perform
about half as many comparisons as selection sort, although it can perform just
as many or far fewer depending on the order the array was in prior to sorting. It
can be seen as an advantage for some real-time applications that selection sort
will perform identically regardless of the order of the array, while insertion
sort's running time can vary considerably. However, this

is more often an advantage for insertion sort in that it runs much more
efficiently if the array is already sorted or "close to sorted."
While selection sort is preferable to insertion sort in terms of number of writes
((n) swaps versus (n2) swaps), it almost always far exceeds (and never
beats) the number of writes that cycle sort makes, as cycle sort is theoretically
optimal in the number of writes. This can be important if writes are significantly
more expensive than reads, such as with EEPROM or Flash memory, where
every write lessens the lifespan of the memory.
Finally, selection sort is greatly outperformed on larger arrays by
(n log n) divide-and-conquer algorithms such as mergesort. However,
insertion sort or selection sort are both typically faster for small arrays (i.e.
fewer than 1020 elements). A useful optimization in practice for the recursive
algorithms is to switch to insertion sort or selection sort for "small enough" sub
lists.
4. (20 pts) An array A[1..n] contains all integers from 0 to n except one
number. It would be easy to determine to the missing number by using an
auxiliary array B[0..n] to record which numbers appear in A. Here, we want
to avoid the additional storage of B with the size O(n). Devise an algorithm
to determine the missing integer in O(n) time under this constraint. (Note,
you can still use additional constant memory as temporary storage.)

ANSWER:
The algorithm of the above problem can be written as follows:
1. Enter the number of the elements.
2. Enter the elements.
3. Initializing two elements sum and k as 0. i.e, sum=0 , k=0
4. For all elements a[0],a[1].of array a do

{
Add a[i] to k;
}
5. Find the sum of the first n natural numbers:
sum= A.length*(A.length+1)/2;
6. The missing element is d=(sum-k);
For example, let the elements in the array be 1, 2, 3, 4, x, 6 - where x is the
missing element in the array. The sum of the elements in the array except 5 will
be 16 and the sum of the first 6 natural numbers is 6*(6+1)/2=21. Therefore, the
missing element will be 21-16=5.

With this we can avoid the additional storage of data and since the no. of
iterations depend on the no. of elements we can say that performance will grow
linearly and in direct proportion to the size of the input data set. Therefore, we can
determine the missing number in O(n) time.

5.(20 pts) Let A[1..n] be an array of n distinct numbers. If i < j and A[i] >
A[j], then the pair (i, j) is called an inversion of A. a) List the five inversions
of the array <2, 3, 8, 6, 1>. b) What array with elements from the set {1, 2,
, n} has the most inversions? How many does it have? c) What is the
relationship between the running time of insertion sort and the number
inversions in the input array?
Justify your answer.

ANSWER:

The five inversions of the array <2,3,8,6,1> are (1,5), (2,5) (3,4), (3,5),(4,5).

The array with all the elements in the reverse order will have the maximum
inversions. The total number of inversions can be calculated in the following
way:
Let n be the total no. of elements then for the first element the inversion will be
n-1, for the second one inversion will be n-2 and so on. The total will be
therefore,
(n-1)+(n-2)+..+2+1=n*(n-1)/2

The running time of the insertion sort i.e, the no. of steps insertion sort
executes is equal to the no. of inversions that existed in the input array.
Suppose there is an array A where elements from index 0 to j-1 are sorted and
we have to place the jth element in the right position.
Let a and b be the index of the elements from 0 to j-1, if A[a]<A[j] then no
inversion occurs as a<j, in this case the no. of inversions will be 0.
Similarly consider element at position b, if A[b]>A[j] then inversion exists.
Therefore, inversion exists between the jth element till the position b where the
insertion sort executes before placing the A[j] in the right position.
In other words, it can be said that the number of inversions determine the
running time of the Insertion Sort.

6. (20 pts) You are given two sorted lists of size m and n. Devise an O(log m
+ log n) time algorithm for computing the kth smallest element in the union
of the two list.

ANSWER:
Let us consider two sorted arrays a and b. We need to find out the kth smallest
element from the union of the two sorted arrays.
The algorithm can be written as:

int find(int a[], int b[], int k) {


int i = 0, j = 0, count = 0, smallest = -1;
if (a.length + b.length < k) {
return smallest;
}
while(count < k && i < a.length && j < b.length) {
if (a[i] < b[j]) {
smallest = a[i];
i++;
} else {
smallest = b[j];
j++;
}
count++;
}
if(count == k) {
return smallest;
}
if (i == a.length) {
return b[k-count+j-1];
}
if (j == b.length) {
return a[k-count+j-1];
}
return smallest;
}

Anda mungkin juga menyukai