Implementation of a stable partition algorithm that operates with O(n log n) time complexity and O(1) memory overhead