Fast and stable sort algorithm that uses O(1) memory