MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / countingSort

Function countingSort

CPP/sorting/radix_sort.cpp:16–40  ·  view source on GitHub ↗

Using counting sort to sort the elements in the basis of significant places

Source from the content-addressed store, hash-verified

14
15// Using counting sort to sort the elements in the basis of significant places
16void 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
43void radixsort(int array[], int size) {

Callers 1

radixsortFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected