Skip to content
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

Merged
merged 1 commit into from
Mar 28, 2015
Merged

Quicksort worst case runtime is quadratic, O(n^2) #81

merged 1 commit into from
Mar 28, 2015

Conversation

raypereda
Copy link
Contributor

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

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
@raypereda
Copy link
Contributor Author

@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!

@dasilvacontin
Copy link

Please merge! 👍
It's even on the Wikipedia linked article for Quicksort.

@jaredsburrows
Copy link

@ericdrowell Let's get this merged in please.

ericdrowell added a commit that referenced this pull request Mar 28, 2015
Quicksort worst case runtime is quadratic, O(n^2)
@ericdrowell ericdrowell merged commit abaee47 into ericdrowell:master Mar 28, 2015
@ericdrowell
Copy link
Owner

Thanks guys! Yea that was just a clerical error. Thanks for fixing it up.

@dasilvacontin
Copy link

Nice!

@raypereda raypereda deleted the patch-1 branch April 8, 2015 20:40
@983
Copy link

983 commented Dec 1, 2015

The paper makes the assumption that "2. Pivot-choosing takes O(1) comparisons".

The approximate median-selection algorithm can also be used as a pivot strategy in quicksort, yielding an optimal algorithm, with worst-case complexity O(n log n).

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.

@raypereda
Copy link
Contributor Author

@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.

@983
Copy link

983 commented Dec 2, 2015

source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants