Quicksort superiority over Heap Sort

后端 未结 5 490
花落未央
花落未央 2020-11-29 00:07

Heap Sort has a worst case complexity of O(nlogn) while Quicksort has O(n^2). But emperical evidences say quicksort is superior. Why is that?

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 00:52

    One of the major factors is that quicksort has better locality of reference -- the next thing to be accessed is usually close in memory to the thing you just looked at. By contrast, heapsort jumps around significantly more. Since things that are close together will likely be cached together, quicksort tends to be faster.

    However, quicksort's worst-case performance is significantly worse than heapsort's is. Because some critical applications will require guarantees of speed performance, heapsort is the right way to go for such cases.

提交回复
热议问题