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]Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.