Question: Below is the pseudo code of Count Sort. The indexes are 0 - based for the array C , but 1 - based

Below is the pseudo code of Count Sort. The indexes are 0-based for the array "C", but 1-based for arrays "A" and "B". The algorithm is stable. However, if we change the last loop to go from 1 upto A.length instead of A.length down to 1, it no longer remains stable.
Modify the code below, so that if we go from 1 up to A.length, it still remains stable having asymptotic time complexity of O(n+k).
Count Sort (A,B,C)
??? let C[0..k] be a new array
for i=0 to k
C[i]=0;
for j=1 to A.length
C[A[j]]=C[A[j]]+1;
For i=1 to k
C[i]=C[i]+C[i-1]
For j=A.length to 1
B[C[A[j]]]=A[j]
C[A[j]]=C[A[j]]-1;
 Below is the pseudo code of Count Sort. The indexes are

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!