News for hackers. Stuff that matters.
Re:A numerical analysis of Quicksort: How many cases are bad... by Kenji on Friday August 28, 2015 @10:41AM (#10135649)
I remember, at uni, in the second semester, we had an assignment to specifically craft a sequence of n numbers 1..n that would trigger the worst case (#permutations) in a quicksort algorithm that always takes the first number in the array as the pivot. It was a nightmare. I spent days on it, and I was barely able to scrape together a notation to specify such a series. Turns out the sample solutions were like "The appropriate sequence is not easy to write. The sequence must be designed such that every chosen pivot halves the area that will be stored." Well, yeah.

EDIT: I wrote worst case #permutations. That's the best case #comparisons. So, no, I don't mean a sorted sequence ;)

Re:A numerical analysis of Quicksort: How many cases are bad... by rachbowyer on Friday August 28, 2015 @10:54AM (#10135732)
If quicksort always takes the first element in the array as the pivot, then an array that is already sorted is the worst case.
Re:A numerical analysis of Quicksort: How many cases are bad... by TheLoneWolfling on Friday August 28, 2015 @10:57AM (#10135754)
Doesn't a sorted array trigger the worst case in that case? Either ascending or decending. It's one of the reasons not to choose the first element as the pivot, iirc.

A much more interesting case is the median-of-three pivot. (Namely, median of first / middle / last elements). You can still do it, however.

Re:A numerical analysis of Quicksort: How many cases are bad... by Sharlin on Friday August 28, 2015 @11:59AM (#10136174)
What do you mean by worst case #permutations?