Sorting (Quicksort) ,Partition

Your code is not right: it does not do a stable partition of the array.

Imagine your array is

100, 33, 333, 22, 222, 11

After the stable partitioning around the first element, it should become

33, 22, 11, 100, 333, 222
// 33, 22, and 11 in the same order as in the original array
// 333 and 222 in the same order as in the original array

But, instead, it becomes

22, 11, 33, 100, 222, 333

Note that this partition implementation is ok for a non-stable quick sort: the pivot element is in the final position, all elements to its left are smaller, and all elements to its right are larger.

Leave a Comment