The Algorithm: Informal
The Algorithm: Informal
[edit] Informal
1. Count the discrete elements in the array. (after calculating the minimum and the maximum) 2. Accumulate the counts. 3. Fill the destination array from backwards: put each element to its countth position. Each time put in a new element decrease its count.
counts[i] += counts[ i - 1 ]; // store the elements in a new ordered array int* sorted = new int[num_of_elements]; // B for (int i = num_of_elements - 1; i >= 0; --i) // decrementing the counts value ensures duplicate values in A // are stored at different indices in sorted. sorted[ --counts[ A[i] - min ] ] = A[i]; delete[] counts; return sorted; }