Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is quicksort faster in average than others?

As we know, the quicksort performance is O(n*log(n)) in average but the merge- and heapsort performance is O(n*log(n)) in average too. So the question is why quicksort is faster in average.

like image 504
Michael Avatar asked Nov 26 '10 22:11

Michael


People also ask

Why is quick sort the fastest?

So, it may be difficult to decide which algorithm to choose in a certain case. In practice, Quick Sort is usually the fastest sorting algorithm. Its performance is measured most of the time in O(N × log N). This means that the algorithm makes N × log N comparisons to sort N elements.

Is quicksort faster than merge sort on average?

Merge sort is more efficient and works faster than quick sort in case of larger array size or datasets. Quick sort is more efficient and works faster than merge sort in case of smaller array size or datasets.

Why quick sort is better than other sort?

Quick sort is an in-place sorting algorithm. In-place sorting means no additional storage space is needed to perform sorting. Merge sort requires a temporary array to merge the sorted arrays and hence it is not in-place giving Quick sort the advantage of space.

Why is quicksort faster for small list of elements?

Insertion sort is faster for small n because Quick Sort has extra overhead from the recursive function calls. Insertion sort is also more stable than Quick sort and requires less memory. This question describes some further benefits of insertion sort.


3 Answers

Wikipedia suggests:

Typically, quicksort is significantly faster in practice than other O(nlogn) algorithms, because its inner loop can be efficiently implemented on most architectures, and in most real-world data, it is possible to make design choices that minimize the probability of requiring quadratic time. Additionally, quicksort tends to make excellent usage of the memory hierarchy, taking perfect advantage of virtual memory and available caches. Although quicksort is not an in-place sort and uses auxiliary memory, it is very well suited to modern computer architectures.

Also have a look at comparison with other sorting algorithms on the same page.

See also Why is quicksort better than other sorting algorithms in practice? on the CS site.

like image 169
ChristopheD Avatar answered Oct 10 '22 08:10

ChristopheD


Worst case for quick sort is actually worse than heapsort and mergesort, but quicksort is faster on average.

As to why, it will take time to explain and thus i will refer to Skiena, The algorithm design manual.

A quote that summarizes the quicksort vs merge/heapsort:

When faced with algorithms of the same asymptotic complexity, implementation details and system quirks such as cache performance and memory size may well prove to be the decisive factor. What we can say is that experiments show that where a properly implemented quicksort is implemented well, it is typically 2-3 times faster than mergesort or heapsort. The primary reason is that the operations in the innermost loop are simpler. But I can’t argue with you if you don’t believe me when I say quicksort is faster. It is a question whose solution lies outside the analytical tools we are using. The best way to tell is to implement both algorithms and experiment.

like image 25
Milan Avatar answered Oct 10 '22 08:10

Milan


Timsort might be a better option as it is optimised for the kind of data seen when sorting in general, in the Python language where data often contains embedded 'runs' of presorted items. It has lately been adopted by Java too.

like image 3
Paddy3118 Avatar answered Oct 10 '22 07:10

Paddy3118