algorithms icon indicating copy to clipboard operation
algorithms copied to clipboard

Quicksort should shuffle first

Open RyanNaughton opened this issue 9 years ago • 1 comments

Quicksort should first randomly shuffle the container. If it doesn't and the array is already sorted, then it will actually be O(n^2), which is the worst case. By shuffling, it results in a probabilistic guarantee of О(n log n).

https://github.com/kanwei/algorithms/blob/master/lib/algorithms/sort.rb#L180

See: http://stackoverflow.com/questions/27645471/how-does-random-shuffling-in-quick-sort-helps-in-increasing-the-efficiency-of-th

RyanNaughton avatar Oct 20 '15 06:10 RyanNaughton

It looks like this issue can be closed.

GarrisonJ avatar Jan 18 '24 01:01 GarrisonJ