views:

2746

answers:

17

What is the fastest known sort algorithm for absolute worst case? I don't care about best case and am assuming a gigantic data set if that even matters.

A: 

Assuming randomly sorted data, quicksort.

O(nlog n) mean case, O(n^2) in the worst case, but that requires highly non-random data.

You might want to describe your data set characteristics.

Blank Xavier
But the OP asked for absolute worst case -- in which case quicksort is N^2.
Rick Copeland
Which was in this answer, so really no need for the -1.
Steve Haigh
I agree with Steve
Vijay Dev
reluctantly removing the -1 -- I still don't think the answer is helpful, though
Rick Copeland
I'll put one of my own in then. The OQ was for fastest known sort algorithim in worst case. O(n^2) would not qualify.
T.E.D.
I agree no downvote, but the question ask best case in worst case scenario.
TStamper
I don't believe all other replies have answered the question, do they get a downvote too?
Steve Haigh
@Steve, maybe you're not getting what people are saying here, they are saying the question is what is the fastest algorithm in worst case scenario, not what are sorting algorithms worst case..quicksort does not fall in this category
TStamper
I do get it, and I get that this answer is not addressing that point, I'm just saying -1 seems a bit harsh because that answer is clear and qualifies that QS is indeed o(n^2) in worst case.
Steve Haigh
oh ok, I stated earlier that I agree no downvote, so we are on the same page
TStamper
I think the question is wrong. Worst case behaviour for a large data set implies an extremely unlikely data distribution. I'm not sure the author really understood what he was asking for.
Blank Xavier
Why would you presume to know what the author wants? Shouldn't the author be the authority on that? He seems to have spelled it out fairly clearly to me.
mquander
@mquander - because for a gigantic data set, the absolute worst case is incredibly unlikely to occur. It would be incredible that such a situation would commonly occur and so to optimize against it is bad practise. I think it much more likely the author should actually be concerned about mean behaviour, but that he doesn't realise it.
Blank Xavier
+1  A: 

It largely is related to the size of your dataset and whether or not the set is already ordered (or what order it is currently in).

Entire books are written on search/sort algorithms. You aren't going to find an "absolute fastest" assuming a worst case scenario because different sorts have different worst-case situations.

TheTXI
A: 

See http://stackoverflow.com/questions/680541/quick-sort-vs-merge-sort for a comparison of Quicksort and Mergesort, which are two of the better algorithms in most cases.

Paul Tomblin
+11  A: 

make sure you have seen this:

visualizing sort algorithms - it helped me decide what sort alg to use.

mkoryak
Visualizing sort algorithm is a wonderful way to experiencing different algorithms but it's also good to note something like http://www.hatfulofhollow.com/posts/code/visualisingsorting/index.html
nevets1219
A: 

This is massively depends on the charectaristics of the data.

+6  A: 

If you are using binary comparisons, the best possible sort algorithm takes O(N log N) comparisons to complete. If you're looking for something with good worst case performance, I'd look at MergeSort and HeapSort since they are O(N log N) algorithms in all cases.

HeapSort is nice if all your data fits in memory, while MergeSort allows you to do on-disk sorts better (but takes more space overall).

There are other less-well-known algorithms mentioned on the Wikipedia sorting algorithm page that all have O(n log n) worst case performance. (based on comment from mmyers)

Rick Copeland
A: 

It all depends on the data you're trying to sort. Different algorithms have different speeds for different data. an O(n) algorithm may be slower than an O(n^2) algorithm, depending on what kind of data you're working with.

Alex Fort
Did you read the question?
mquander
+1  A: 

It depends on the size, according to the Big O notation O(n).

Here is a list of sorting algorithms BEST AND WORST CASE for you to compare. My preference is the 2 way MergeSort

TStamper
According to http://en.wikipedia.org/wiki/Sorting_algorithm, there are at least six comparison-sorting algorithms with O(n lg n) worst case (which is the theoretical minimum).
Michael Myers
true, which the link shows..there is a tie, but implementation Mergesort is what I prefer
TStamper
My favorite is the merge sort too. It's stable, has guaranteed worst case performance of O(n log n), is easy to understand and write, and is amenable to large data sets that don't fit into memory.
Mark Ransom
A: 

I've always preferred merge sort, as it's stable (meaning that if two elements are equal from a sorting perspective, then their relative order is explicitly preserved), but quicksort is good as well.

Adam Robinson
Quicksort is O(n^2) in the worst case; modern implementations are usually designed to make that worst case exceedingly unlikely. Heapsort has O(n ln n) worst-case behavior, and requires O(1) additional memory.
David Thornley
+1  A: 

Quicksort is usually the fastest, but if you want good worst-case time, try Heapsort or Mergesort. These both have O(n log n) worst time performance.

Zifre
+1  A: 

If you have a sufficiently huge data set, you're probably looking at sorting individual bins of data, then using merge-sort to merge those bins. But at this point, we're talking data sets huge enough to be VASTLY larger than main memory.

I guess the most correct answer would be "it depends".

Vatine
A: 

The lowest upper bound on Turing machines is achieved by merge sort, that is O(n log n). Though quick sort might be better on some datasets.

You can't go lower than O(n log n) unless you're using special hardware (e.g. hardware supported bead sort, other non-comparison sorts).

Eduard - Gabriel Munteanu
Since the data for a turing machine is on tape, quick sort is going to be very slow compared to merge sort on almost all nontrivial datasets.
Captain Segfault
+1  A: 

For the man with limitless budget

Facetious but correct: Sorting networks trade space (in real hardware terms) for better than O(n log n) sorting!

Without resorting to such hardware (which is unlikely to be available) you have a lower bound for the best comparison sorts of O(n log n)

O(n log n) worst case performance (no particular order)

Beating the n log n

If your data is amenable to it you can beat the n log n restriction but instead care about the number of bits in the input data as well

Radix and Bucket are probably the best known examples of this. Without more information about your particular requirements it is not fruitful to consider these in more depth.

ShuggyCoUk
+1  A: 

It depends both on the type of data and the type of resources. For example there are parallel algorithms that beat Quicksort, but given how you asked the question it's unlikely you have access them. There are times when the "worst case" for one algorithm is "best case" for another (nearly sorted data is problematic with Quick and Merge, but fast with much simpler techniques).

acrosman
A: 

On the importance of specifying your problem: radix sort might be the fastest, but it's only usable when your data has fixed-length keys that can be broken down into independent small pieces. That limits its usefulness in the general case, and explains why more people haven't heard of it.

http://en.wikipedia.org/wiki/Radix_sort

P.S. This is an O(k*n) algorithm, where k is the size of the key.

Mark Ransom
+7  A: 

Depends on data. For example for integers (or anything that can be expressed as integer) the fastest is radix sort which for fixed length values has worst case complexity of O(n). Best general comparison sort algorithms have complexity of O(n log n).

vartec
+1 I just realized that this is the fastest if N>10, Best and Worst Case: O(n)
TStamper
+1  A: 

If you have a gigantic data set (ie much larger than available memory) you likely have your data on disk/tape/something-with-expensive-random-access, so you need an external sort.

Merge sort works well in that case; unlike most other sorts it doesn't involve random reads/writes.

Captain Segfault