14

I have seen many places say quicksort is good because it fits to cache-related stuff, such as said in wiki

Additionally, quicksort's sequential and localized memory references work well with a cache

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

Could anyone give me some insight about this claim? How is quicksort related to cache? Normally what means that cache in the statement? Why quicksort is better for a cache?

Thanks

1

4 Answers 4

19

quicksort changes the array inplace - in the array it is working on [unlike merge sort, for instance - which creates a different array for it]. Thus, it applies the principle of locality of reference.

Cache benefits from multiple accesses to the same place in the memory, since only the first access needs to be actually taken from the memory - the rest of the accesses are taken from cache, which is much faster the access to memory.

Merge sort for instance - needs much more memory [RAM] accesses - since every accessory array you create - is accessing the RAM again.

Trees are even worse - since 2 sequential accesses in a tree are not likely to be close to each other. [Cache is filled in blocks, so for sequential accesses - only the first byte in the block is a "miss" and the others are a "hit"].

Sign up to request clarification or add additional context in comments.

1 Comment

I believe what you said is indeed the answer. But could you please give me an example for relating quicksort to cache?
5

What goes into the cache is determined by algorithms that pretty much guess at what you're going to use soon based on what you're currently requesting. This usually means blocks of memory that are close to each other, such as arrays.

After a few iterations, quicksort will be working with blocks that fit completely into the cache, and this substantially increases performance. (Compare with, say, select sort, which may be accessing memory locations that are far apart with most operations.)

Comments

1

Quicksort is an in-place sorting algorithm. It moves elements to the left and right of the pivot via using swaps. Each time a swap occurs, it is likely that the cache line is loaded and subsequent swap will occur from the same cache line.

Comments

1

From an algorithmic point of view the time complexity is usually just the number of memory accesses. From this point of view, quicksort is good in average O(n log n), but not the best algortihm. First, because it's worst complexity is O(n2) and does meet this cases in real life (typically for reverse-sorted or constant inputs) when some others are O(n log n) in the worst case.

What makes quicksort a good algorithm is that on real computers, not all memory accesses take the same time.

The main memory, SDRAM has a latency that seems very long from the CPU's point of view (typically hundreds of CPU cycles). Fortunately, large portions of the memory can be requested and put in a smaller, quicker memory: the cache (CPUs often have multiple layers of cache but I'll refer to all of these as "the cache"). For this reason a computer runs faster when it works on data that is already (or still) in the cache. When it fetches new memory region, the CPU has to wait for the main memory to answer, which makes a notable performance hit, called a cache miss.

So an important factor for sorting large arrays quickly is memory accesses pattern. For this, quicksort is really good and generate very few cache misses.

Why ? Because it scans the array sequentially (and repeats on smaller segments). The most of its memory accesses take place in the cache.

Heap sort on the other end needs to maintain a search tree-like structure and has a more "random" memory access pattern.

Even merge sort which is very efficient on cache still needs additional memory accesses, because it cannot work in place: either because it needs to be recursive keep temporary data in the stack or it needs linked lists (which means pointers and indirect memory accesses and then cache misses).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.