Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the algorithm for the sorting problem that sorts an array by counting,

ID: 3623312 • Letter: C

Question

Consider the algorithm for the sorting problem that sorts an array by counting, for each of its elements, the number of smaller elements and then uses this information to put the element in its appropriate position in the sorted array:

(a)    Apply this algorithm to sorting the list 6, 3, 8, 3, 9, 1, 3, 9. Present the intermediate steps clearly for the numbers.

(b)   Is this algorithm stable?

Explanation / Answer

after first for loop,the contents of array counts are : Count = input Array A[0...7] is length of the Count,A as well as S is 8 i.e. n=8. now we start with second for loop for(i =0 to n-1) step 1 : a) i =0,j=1 and A[i] > A[j] so the Count array content will be b) i=0,j=2, here A[i] A[j] so the Count array content will be d) i=0,j=4 ,here A[i]A[j] so the Count array content will be f) i=0,j=6 ,here A[i]>A[j] so the Count array content will be g) i=0,j=7 ,here A[i]= A[j] so the Count array content will be f) i=1,j=7 ,here A[i] A[j] so the Count array content will be b) i=2,j=4 ,here A[i] A[j] so the Count array content will be d) i=2,j=6 ,here A[i] >= A[j] so the Count array content will be e) i=2,j=7 ,here A[i] =A[j] so the Count array content will be d) i=3,j=7 ,here A[i] A[j] so the Count array content will be b) i=4,j=6, here A[i] >A[j] so the Count array content will be c) i=4,j=7 ,here A[i] >= A[j] so the Count array content will be Step 6 : a) i=5,j=6 ,here A[i]