Using counting sort to sort the elements in the basis of significant places
| 14 | |
| 15 | // Using counting sort to sort the elements in the basis of significant places |
| 16 | void countingSort(int array[], int size, int place) { |
| 17 | const int max = 10; |
| 18 | int output[size]; |
| 19 | int count[max]; |
| 20 | |
| 21 | for (int i = 0; i < max; ++i) |
| 22 | count[i] = 0; |
| 23 | |
| 24 | // Calculate count of elements |
| 25 | for (int i = 0; i < size; i++) |
| 26 | count[(array[i] / place) % 10]++; |
| 27 | |
| 28 | // Calculate cumulative count |
| 29 | for (int i = 1; i < max; i++) |
| 30 | count[i] += count[i - 1]; |
| 31 | |
| 32 | // Place the elements in sorted order |
| 33 | for (int i = size - 1; i >= 0; i--) { |
| 34 | output[count[(array[i] / place) % 10] - 1] = array[i]; |
| 35 | count[(array[i] / place) % 10]--; |
| 36 | } |
| 37 | |
| 38 | for (int i = 0; i < size; i++) |
| 39 | array[i] = output[i]; |
| 40 | } |
| 41 | |
| 42 | // Main function to implement radix sort |
| 43 | void radixsort(int array[], int size) { |