Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is quick sort better at cache locality than mergesort?

In answers related to quicksort vs mergesort, it is commonly stated that quicksort exploits cache locality (locality of reference) better than mergesort.

As both sorts follow a divide and conquer approach, I don't understand how quicksort is more cache-friendly. Can anyone provide more insight related to this?

Also, there's notes on in-place merge sort. If this is practical (I have no idea whether it is), can merge sort also be cache-friendly?

like image 676
dev_nut Avatar asked Jan 30 '18 03:01

dev_nut


People also ask

Why Quick sort has good locality of reference?

One of the main reasons for efficiency in quick sort is locality of reference, which makes it easy for the computer system to access memory locations that are near to each other, which is faster than memory locations scattered throughout the memory which is the case in merge sort.

Why is quick sort cache efficient?

Quicksort has excellent cache performance within each run, and is most efficient in instructions executed (among comparison-based sorts) because of its efficient inner loops. However, quicksort makes Θ(logN) passes, and thus incurs many cache misses.

Why is quick sort better than Heapsort?

It runs fast, much faster than Heap and Merge algorithms. The secret of Quicksort is: It almost doesn't do unnecessary element swaps. Swap is time consuming. With Heapsort, even if all of your data is already ordered, you are going to swap 100% of elements to order the array.

In which cases quick sort performs best?

The best-case behavior for quicksort occurs when we are lucky and partition process always picks middle element as pivot. In other words, this is a case of balanced partition, where both sub-problems are n/2 size each.


1 Answers

If you're sorting an array that fits into cache, then quicksort will require fewer memory accesses just because mergesort needs to allocate a second array. Quicksort will load the array into cache and then proceed without waiting for memory at all. Mergesort will pay the additional cost of accessing the second array.

If you're sorting an array that doesn't fit into cache, then quicksort still wins from a locality point of view, because as they recurse to sort smaller sections, both algorithms will soon get to sections that do fit into cache, and for those quicksort is faster by the above argument. On the upper levels of the sort that don't fit into cache, quicksort and mergesort perform pretty much equivalently from a cache locality point of view.

like image 194
Matt Timmermans Avatar answered Oct 17 '22 01:10

Matt Timmermans