javasortingmergesortheapsort

Heap Sort vs Merge Sort in Speed


Which algorithm is faster when iterating through a large array: heap sort or merge sort? Why is one of these algorithms faster than the other?


Solution

  • Although time complexity is the same, the constant factors are not. Generally merge sort will be significantly faster on a typical system with a 4 or greater way cache, since merge sort will perform sequential reads from two runs and sequential writes to a single merged run. I recall a merge sort written in C was faster than an optimized heap sort written in assembly.

    One issue is that heap sort swaps data, that's two reads and two writes per swap, while merge sort moves data, one read and one write per move.

    The main drawback for merge sort is a second array (or vector) of the same size as the original (or optionally 1/2 the size of the original) is needed for working storage, on a PC with 4 GB or more of RAM, this usually isn't an issue.

    On my system, Intel 3770K 3.5 ghz, Windows 7 Pro 64 bit, Visual Studio 2015, to sort 2^24 = 16,777,216 64 bit unsigned integers, heap sort takes 7.98 seconds while bottom up merge sort takes 1.59 seconds and top down merge sort takes 1.65 seconds.