I think this kind of sorting can be achieved in O(n), with something like the following:
let input = [1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2]
// build the frequency dictionary (easy task)
let frequencies = input.reduce(into: [:]) { $0[$1] = ($0[$1] ?? 0) + 1 }
// allocate a 2-D array of ints, each item in this array will hold the numbers that
// appear I times in the input array
let frequencyTable: [[Int]] = frequencies.reduce(into: Array(repeating: [Int](), count: input.count)) {
// substracting one as arrays are zero indexed
// we can't get of of bounds here since the maximum frequency is the
// number of elements in the input array
// Also replicating the numbers by their frequency, to have
// the same contents as in the input array
$0[$1.value - 1] += Array(repeating: $1.key, count: $1.value)
}
// lastly, simply flatten the 2-D array to get the output we need
let output = frequencyTable.flatMap { $0 }
print(output)
Sample result:
[4, 1, 3, 2, 2, 5, 5, 5, 6, 6, 6, 6]
Note that the order of numbers with the same frequency might differ based on how the dictionary hash function works.
Also we sacrifice space (allocated 2-D array) in favour of time.
The frequencyTable
contents will look similar to this (again the order of 1, 4, 3 might differ):
[[4, 3, 1], [2, 2], [5, 5, 5], [6, 6, 6, 6], [], [], [], [], [], [], [], []]