HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulevector-algorithms-0.9.1.0Haskell2010

Data.Vector.Algorithms.Intro

This module implements various algorithms based on the introsort algorithm, originally described by David R. Musser in the paper /Introspective Sorting and Selection Algorithms/. It is also in widespread practical use, as the standard unstable sort used in the C++ Standard Template Library.

Introsort is at its core a quicksort. The version implemented here has the following optimizations that make it perform better in practice:

  • Small segments of the array are left unsorted until a final insertion sort pass. This is faster than recursing all the way down to one-element arrays.

  • The pivot for segment [l,u) is chosen as the median of the elements at l, u-1 and (u+l)/2. This yields good behavior on mostly sorted (or reverse-sorted) arrays.

  • The algorithm tracks its recursion depth, and if it decides it is taking too long (depth greater than 2 * lg n), it switches to a heap sort to maintain O(n lg n) worst case behavior. (This is what makes the algorithm introsort).

  • 1 type
  • 11 values

Sorting

5 declarations

Selecting

3 declarations
valueselect
  1. :: (PrimMonad m, MVector v e, Ord e)
  2. => v (PrimState m) e
  3. -> Int

    number of elements to select, k

  4. -> m ()
#

Moves the least k elements to the front of the array in no particular order.

valueselectBy
  1. :: (PrimMonad m, MVector v e)
  2. => Comparison e
  3. -> v (PrimState m) e
  4. -> Int

    number of elements to select, k

  5. -> m ()
#

Moves the least k elements (as defined by the comparison) to the front of the array in no particular order.

Partial sorting

4 declarations
typetype Comparison e = e -> e -> Ordering
#

A type of comparisons between two values of a given type.