Counting Sort
2024-06-19
Sorting algorithm I discovered. It comes w/ the caveat, that values of the to be sorted array need to be within a reasonable range (since they are used as indices in the bookkeeping array).
But the principle is neat:
- iterate over to be sorted array
- create a bookeeping array whose
- indices reflect the "to be sorted array's" values
- values at specific indices denote how often a value appears in the unsorted array
- based on the bookeeping array create a sorted version
Quick implementation attempt:
using namespace std;
int
It sorts in O(n)
of course, and therefore beats the general optimized sorting algorithms, but its use case seems highly specialized, and having to sort particularly "smaller" data sets w/ a knowable small range seems an unlikely performance bottleneck. You'd also not want to use it if it leads to a lot of separate memory allocations. The principle is neat though.