Your first implementation worked but had degenerate behavior with specific datasets. The pivot in your original working code was *(last - 1)
so the simplest fix would be to swap a random element with *(last - 1)
. The rest of your original partition code would work unchanged.
2
solved C++ QuickSort Isn’t Working Correctly [closed]