Too Long; Didn't Read
The problem is perfectly suited for Count Sort, a special sorting algorithm that works against data that is guaranteed to fit into a fixed range. Count Sort is one of my favorite algorithms because for the right data it is a sort that works in linear time, that’s O(n) in both time and space complexity. My stable count sort solution solved the problem in 552ms and 21.1 MB of memory: #Count Sort count = [ 0 ]*(limit+ 1 ) for i in people: count[i] += 1 p = 0 for p in range(len(people)