Skip to main content
QUICK REVIEW

[Paper Review] Insertion Sort is O(n log n)

Michael A. Bender, Martı́n Farach-Colton|ArXiv.org|Jul 1, 2004
Algorithms and Data CompressionComputer Science4 references5 citations
TL;DR

This paper introduces Gapped Insertion Sort (also known as Library Sort), a variant of insertion sort that maintains evenly spaced gaps between elements to reduce insertion time. By using random input permutation and a spreading factor, the algorithm achieves O(n log n) expected time complexity with high probability, significantly improving upon traditional O(n²) insertion sort.

ABSTRACT

Traditional Insertion Sort runs in O(n^2) time because each insertion takes O(n) time. When people run Insertion Sort in the physical world, they leave gaps between items to accelerate insertions. Gaps help in computers as well. This paper shows that Gapped Insertion Sort has insertion times of O(log n) with high probability, yielding a total running time of O(n log n) with high probability.

Motivation & Objective

  • To address the inefficiency of traditional insertion sort, which runs in O(n²) time due to costly element shifting during insertions.
  • To explore whether maintaining gaps between elements in the sorted array can reduce insertion cost in practice and theory.
  • To analyze the expected and high-probability performance of a gap-based insertion algorithm under random input permutation.
  • To formalize and prove that insertion time becomes O(log n) with high probability when gaps are strategically maintained.
  • To provide a simplified and rigorous analysis of gap-based sorting with high-probability bounds, improving upon prior expectation-only analyses.

Proposed method

  • The algorithm uses a larger array S of size (1+ε)n to store n elements, with gaps distributed evenly to allow faster insertions.
  • Insertions are performed in rounds: each round doubles the number of elements inserted and rebalances them using a spreading factor of 2+2ε.
  • During each round, binary search is used on support elements (previously inserted elements) to locate the insertion point in O(log m) time.
  • Insertions only require shifting elements until the nearest gap is reached, limiting the number of moved elements to O(log n) with high probability.
  • The analysis leverages probabilistic bounds using binomial coefficients and Stirling's approximation to show that long sequences of intercalated elements are unlikely.
  • The union bound is applied across non-overlapping segments of size (2+ε)c log m to ensure gaps exist within any such segment with high probability.

Experimental results

Research questions

  • RQ1Can maintaining evenly spaced gaps in a sorted array reduce the expected cost of insertion in insertion sort?
  • RQ2What is the expected number of elements that must be shifted during insertion when gaps are maintained with a spreading factor?
  • RQ3Under what conditions does the insertion cost drop from O(n) to O(log n) with high probability?
  • RQ4How does the choice of gap size (controlled by ε) affect the trade-off between space overhead and insertion time?
  • RQ5Can a high-probability analysis of insertion cost be achieved for gap-based insertion sort, beyond just expectation bounds?

Key findings

  • The insertion cost for the first O(√n) elements is O(n) in the worst case, consistent with standard insertion sort.
  • The total cost of all rebalancing operations is O(n) due to amortized analysis over doubling rounds.
  • The search cost for each insertion is O(log m), where m is the number of elements inserted so far, due to binary search on support elements.
  • For m = Ω(√n), the number of elements moved per insertion is O(log m) with high probability, due to the presence of gaps within any segment of (2+ε)c log m elements.
  • The probability that a segment of (2+ε)c log m elements contains more than (1+ε)c log m intercalated elements is polynomially small, ensuring gaps exist with high probability.
  • The overall running time of Library Sort is O(n log n) with high probability, achieved by combining O(n) rebalancing cost, O(n log n) search cost, and O(n log n) insertion cost with high probability.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.