Help please! Consider the algorithm for the sorting problem that sorts an array
ID: 3784754 • Letter: H
Question
Help please!
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: Algorithm ComparisonCountingSort(A[0.. n - 1], S[0.. n - 1])//Sorts an array by comparison counting//Input: Array A[0.. n - 1] of orderable values//Output: Array S[0.. n - 1] of A' s elements sorted in nondecreasing order for i leftarrow 0 - 1 do Count[i] leftarrow 0 for i leftarrow 0 to n - 2 do for j leftarrow i + 1 to n - 1 do if A[i]Explanation / Answer
a.
Count array will be set to 0.
Let us run the nested array
i=0,j=1
60>35 so count[0]=1
i=0,j=2
60<81 so count[2]=1
i=0,j=3
60<98 so count[3]=1
i=0,j=4
60>14 so count[0]=2
i=0,j=5
60>47 so count[0]=3
i=1,j=2
35<81 so count[2]=2
i=1,j=3
35<98 so count[3]=2
i=1,j=4
35>14 so count[1]=1
i=1,j=5
35<47 so count[5]=1
i=2,j=3
81<98 so count[3]=3
i=2,j=4
81>14 so count[2]=3
i=2,j=5
81>47 so count[2]=4
i=3,j=4
98>14 so count[3]=4
i=3,j=5
98>47 so count[3]=5
i=4,j=5
14<47 so count[5]=2
The final values of count are 3 1 4 5 0 2
Therefore S will be
14 35 47 60 81 98
b. Yes the algorithm is stable
c. This is not in place sorting.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.