Lably
Skip to content

Introsort

Hybrid sorting algorithm

  • 4 min read
  • 4 chapters

Related on Lably

Through Quicksort, Logarithm and Insertion sort

Introsort was invented by David Musser in Musser (1997), in which he also introduced introselect, a hybrid selection algorithm based on quickselect (a variant of quicksort), which falls back to median of medians and thus provides worst-case linear complexity, which is optimal. Both algorithms were introduced with the purpose of providing generic algorithms for the C++ Standard Library which had both fast average performance and optimal worst-case performance, thus allowing the performance requirements to be tightened. Introsort is in-place and a non-stable algorithm.

Pseudocode

If a heapsort implementation and partitioning functions of the type discussed in the quicksort article are available, the introsort can be described succinctly as

<b id="mwJg">procedure</b> sort(A : array): maxdepth ← ⌊log<sub id="mwJw">2</sub>(length(A))⌋ × 2 introsort(A, maxdepth) <b id="mwKA">procedure</b> introsort(A, maxdepth): n ← length(A) <b id="mwKQ">if</b> n < 16: insertionsort(A) <b id="mwKg">else if</b> maxdepth = 0: heapsort(A) <b id="mwKw">else</b>: p ← partition(A) <i id="mwLA">// assume this function does pivot selection, p is the final position of the pivot</i> introsort(A[1:p-1], maxdepth - 1) introsort(A[p+1:n], maxdepth - 1)

The factor 2 in the maximum depth is arbitrary; it can be tuned for practical performance. A[i:j] denotes the array slice of items i to j including both A[i] and A[j]. The indices are assumed to start with 1 (the first element of the A array is A[1]).

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication