Why should Insertion Sort be used after threshold crossover in Merge Sort

后端 未结 6 753
鱼传尺愫
鱼传尺愫 2021-01-05 15:12

I have read everywhere that for divide and conquer sorting algorithms like Merge-Sort and Quicksort, instead of recursing until only a single eleme

6条回答
  •  有刺的猬
    2021-01-05 15:28

    1. Insertion sort is faster in practice, than bubblesort at least. Their asympotic running time is the same, but insertion sort has better constants (fewer/cheaper operations per iteration). Most notably, it requires only a linear number of swaps of pairs of elements, and in each inner loop it performs comparisons between each of n/2 elements and a "fixed" element that can be stores in a register (while bubble sort has to read values from memory). I.e. insertion sort does less work in its inner loop than bubble sort.
    2. The answer claims that 10000 n lg n > 10 n² for "reasonable" n. This is true up to about 14000 elements.

提交回复
热议问题