COUNTING-SORT(A)
Input: an array indexed from 0 to A.size-1
The key element is a small integer.
Output: The original array ordered
- min ← min(A.keys)
- max ← max(A.keys)
- count ← array with max-min elements initialzed to 0
- for i ← 0 to A.size-1
- pos ← A[i].key
- count[pos] ← count[pos] + 1
- for i ← 1 to max
- count[i] ← count[i] + count[i-1]
- for i ← A.size-1 to 1
- pos ← A[i].key
- B[count[pos]] ← A[i]
- count[pos] ← count[pos] - 1
- return B