Question: void selectionSort ( int A [ ] , int N ) { for ( int k = 0 ; k < N - 1 ;
void selectionSortint A int N
for int k ; k N; k
lines : find the smallest value in AkN
mi will hold the index for the smallest value.
int mi k ;
for int j k ; j N ; j
if Aj Ami mi j ;
move swap the smallest value to Ak
int t Ak ;
Ak Ami ;
Ami t ;
Trace the above function on the array For each iteration of
the outer for loop, you need to produce a record of the values in array A similar
to Figure in the textbook and slides.
Why does the innerloop maintain the index of the minimum value in variable
mi rather than the minimum value itself?
Is there a bestcase or worstcase for this algorithm? Explain elaborate on your
answer?
pssst the answer is no there isnt
Derive the formula that counts the number of steps it takes for this function to
sort a list of size N
So whats the orderofgrowth of this algorithm?
What would be a suitable loopinvariant for the outer loop?
Prove the correctness of the loopinvariant by proving the initialization and
maintenance steps. Assume that the inner loop lines is correct and that at line
the value in Ami is indeed the smallest value in AkN
Use the loopinvariant, and the termination condition of the outer loop to prove
the correctness of the SelectionSort algorithm
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
