Array Sorting and Definitions-Algorithm Fundamentals-Assignment Solution, Exercises of Algorithms and Programming

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

2011/2012

Uploaded on 08/01/2012

shamabhat_84
shamabhat_84 ๐Ÿ‡ฎ๐Ÿ‡ณ

80 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 502 Fundamental of Algorithms
Assignment # 01
Spring 2012
Total Marks = 20
Deadline
Your๎˜ƒassignment๎˜ƒmust๎˜ƒbe๎˜ƒuploaded๎˜ƒ/๎˜ƒsubmitted๎˜ƒbefore๎˜ƒor๎˜ƒon๎˜ƒApril 23, 2012
Upload Instructions
Please๎˜ƒview๎˜ƒthe๎˜ƒassignment๎˜ƒsubmission๎˜ƒprocess๎˜ƒdocument๎˜ƒprovided๎˜ƒto๎˜ƒyou๎˜ƒby๎˜ƒthe๎˜ƒ
Virtual๎˜ƒUniversity.๎˜ƒ
๎˜ƒ
Rules for Marking
Please๎˜ƒnote๎˜ƒthat๎˜ƒyour๎˜ƒassignment๎˜ƒwill๎˜ƒnot๎˜ƒbe๎˜ƒgraded๎˜ƒif:๎˜ƒ
โ€ข It๎˜ƒis๎˜ƒsubmitted๎˜ƒafter๎˜ƒdue๎˜ƒdate๎˜ƒ
โ€ข The๎˜ƒfile๎˜ƒyou๎˜ƒuploaded๎˜ƒdoes๎˜ƒnot๎˜ƒopen๎˜ƒ
โ€ข The๎˜ƒfile๎˜ƒyou๎˜ƒuploaded๎˜ƒis๎˜ƒcopied๎˜ƒfrom๎˜ƒsomeone๎˜ƒelse๎˜ƒor๎˜ƒfrom๎˜ƒinternet๎˜ƒ
โ€ข It๎˜ƒis๎˜ƒin๎˜ƒsome๎˜ƒformat๎˜ƒother๎˜ƒthan๎˜ƒ.doc
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.
Assignment Statements:
Question 1:
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.
Solution:
SELECTION-SORT (A)
n โ† length[ A]
for j โ† 1 to n โˆ’ 1
do smallest โ† j
for i โ† j + 1 to n
do if A[i ] < A[smallest]
docsity.com
pf2

Partial preview of the text

Download Array Sorting and Definitions-Algorithm Fundamentals-Assignment Solution and more Exercises Algorithms and Programming in PDF only on Docsity!

CS 502 Fundamental of Algorithms

Assignment # 01

Spring 2012

Total Marks = 20

Deadline

Your assignment must be uploaded / submitted before or on April 23, 2012

Upload Instructions

Please view the assignment submission process document provided to you by the Virtual University.

Rules for Marking

Please note that your assignment will not be graded if:

  • It is submitted after due date
  • The file you uploaded does not open
  • The file you uploaded is copied from someone else or from internet
  • It is in some format other than .doc

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.

Assignment Statements:

Question 1:

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.

Solution:

SELECTION-SORT (A)

n โ† length [ A ]

for j โ† 1 to n (^) โˆ’ 1

do smallest โ† j for i โ† j + 1 to n do if A [ i ] < A [ smallest ]

docsity.com

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