-
Notifications
You must be signed in to change notification settings - Fork 316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quicksort worst case runtime is quadratic, O(n^2) #81
Conversation
Doug McIlroy's 1999 paper “A Killer Adversary for Quicksort” describes how to find inputs to the sort that display this quadratic behavior. http://www.cs.dartmouth.edu/~doug/mdmspe.pdf Here's a discussion on the topic by Russ Cox at http://research.swtch.com/qsort
@ericdrowell If there is any doubt about the the correctness of the proposal I'm glad to discuss more. Thanks for creating the Big O Cheat Sheet on github! |
Please merge! 👍 |
@ericdrowell Let's get this merged in please. |
Quicksort worst case runtime is quadratic, O(n^2)
Thanks guys! Yea that was just a clerical error. Thanks for fixing it up. |
Nice! |
The paper makes the assumption that "2. Pivot-choosing takes O(1) comparisons". If you don't believe wikipedia, here is some code comparing qsort from the C standard library to a quicksort implementation that chooses the median as the pivot element, both using the worst case sequence generator from the paper. |
@983 In standard quicksort, choosing the pivot is O(1). quicksort with perfect pivot selection, call it quicksort2, is a different algorithm than quicksort. The performance of quicksort2 is different than performance of quicksort. On average, quicksort is going to be faster than quicksort2. In the benchmark code you provided, you initialize the array with a[i] = i. That is a special case that is going to do very well with quicksort2. Please try running the benchmark with a[i] = random number between 0 and 5000, or better yet randomly shuffle the array a[i] before running your benchmark. I'd would be interested in seeing the results of the plot after those changes are made. |
Doug McIlroy's 1999 paper “A Killer Adversary for Quicksort” describes how to find inputs to the sort that exhibit this quadratic behavior.
Here's a discussion on the topic by Russ Cox at http://research.swtch.com/qsort