

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solved assignment for Algorithm Fundamentals. Submitted to Prof. Jeevanlata Khatri at Avinashilingam University. It includes: Sorting, Numbers, Array, Element, Algorithm, Loop, Invariant, Subarray, Running, Time, Parameters
Typology: Exercises
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Your assignment must be uploaded / submitted before or on April 23, 2012
Please view the assignment submission process document provided to you by the Virtual University.
Please note that your assignment will not be graded if:
Note: Material that is an exact copy from handouts or internet would be graded Zero marks. Your solution should consist of the material found through different sources and written in your own words.
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A, and exchange it with A[2]. Continue in this manner for the first n - 1 elements of A. Write pseudo code for this algorithm, which is known as selection sort. What loop invariant does this algorithm maintain? Why does it need to run for only the first n - 1 element, rather than for all n elements? Give the best-case and worst-case running times of selection sort in ฮ -notation.
n โ length [ A ]
for j โ 1 to n (^) โ 1
do smallest โ j for i โ j + 1 to n do if A [ i ] < A [ smallest ]
then smallest โ i exchange A [ j ] A [ smallest ]
The algorithm maintains the loop invariant that at the start of each iteration of the outer for loop, the subarray A [1.. j (^) โ 1] consists of
the j (^) โ 1 smallest elements in the array A [1.. n ], and this subarray is in sorted order. After the first n โ 1 elements, the subarray A [.. n (^) โ 1] contains the smallest n (^) โ 1 elements, sorted, and therefore element A [ n ] must be the largest element. The running time of the algorithm is ฮ (n^2 ) for all cases.
Question 2:
We can extend our notation to the case of two parameters n and m that can go to infinity independently at different rates. For a given function g ( n , m ), we denote by O ( g ( n , m )) the set of functions
O ( g ( n , m )) = { f ( n , m ): there exist positive constants c , n 0, and m 0 such that 0 โค f ( n , m ) โค cg ( n , m ) for all n โฅ n 0 and m โฅ m 0 }.
Give corresponding definitions for ฮฉ ( g ( n , m )) and ฮ ( g ( n , m )).
Solution:
ฮฉ (g (n, m)) = { f (n,m) : there exist positive constants c , n 0 , and m 0
such that 0 โค cg (n, m) โค f (n ,m) for all n โฅ n 0 and m โฅ m 0 }.
ฮ (g (n ,m)) = { f (n, m) : there exist positive constants c 1 , c 2 , n 0 , and m 0
such that 0 โค c 1 g(n, m) โค f (n ,m) โค c 2 g (n, m) for all n โฅ n 0 and m โฅ m 0 }.
docsity.com