Language: EN FI

Exercises > Counting methods for sorting

These exercises demonstrate the content of the ByTheMark service. These are meant for private individuals for learning data structures and algorithms. If you want to try out more content, you can register to the ByTheMark Personal free of charge.

Sort the following array of key values by means of counting sort.

 1. for (j=0; j<M; j++)
 2.   aux[j] = 0;
 3. for (i=0; i<N; i++) 
 4.   aux[input[i]]++;
 5. for (j=1; j<M; j++)
 6.   aux[j] += aux[j-1];
 7. for (i=N-1; i>=0; i--) { 
 8.   output[aux[input[i]]] = input[i];
 9.   aux[input[i]]--;
10. }

  Last modified Tue Mar 01 20:54:04 EET 2011