Skip to content

Flashsort

Distribution sorting algorithm that approximates uniform distribution and then refines with insertion sort.

Flashsort is a hybrid distribution sorting algorithm that first classifies elements into buckets using an approximate linear mapping, then refines the partially ordered array using insertion sort.

It is designed for datasets that are approximately uniformly distributed, where it can achieve near linear performance.

Problem

Given an array AA of nn elements, sort the array efficiently by exploiting approximate distribution information.

Idea

Flashsort has two main phases:

  1. Classification phase Map each element into one of mm classes using a linear transformation.

  2. Permutation phase Rearrange elements so that each class occupies a contiguous region.

  3. Final refinement Use insertion sort to finish ordering within each region.

Mapping Function

Let:

  • min=min(A)\min = \min(A)
  • max=max(A)\max = \max(A)

Each element is mapped to class:

k=(m1)xminmaxmin k = \left\lfloor (m - 1) \cdot \frac{x - \min}{\max - \min} \right\rfloor

This distributes elements approximately uniformly if the input is uniform.

Algorithm

flashsort(A):
    n = length(A)
    if n <= 1:
        return A

    m = int(0.45 * n)
    L = [0] * m

    min_val = min(A)
    max_val = max(A)

    if min_val == max_val:
        return A

    for x in A:
        k = int((m - 1) * (x - min_val) / (max_val - min_val))
        L[k] += 1

    for i in range(1, m):
        L[i] += L[i - 1]

    i = 0
    j = 0
    k = m - 1

    while i < n:
        while i >= L[k]:
            i += 1
            k = int((m - 1) * (A[i] - min_val) / (max_val - min_val))

        flash = A[i]

        while i < L[k]:
            k = int((m - 1) * (flash - min_val) / (max_val - min_val))
            j = L[k] - 1
            A[j], flash = flash, A[j]
            L[k] -= 1

    insertion_sort(A)
    return A

Example

Sort:

A=[35,12,43,8,51,27,19] A = [35, 12, 43, 8, 51, 27, 19]

Step 1: classify into buckets using linear mapping Step 2: permute elements into approximate order Step 3: apply insertion sort

Final result:

[8,12,19,27,35,43,51] [8, 12, 19, 27, 35, 43, 51]

Correctness

The classification phase partitions the value range into intervals. The permutation phase moves elements into approximate positions so that most elements are close to their final location. The final insertion sort completes ordering.

Complexity

Let:

  • nn be number of elements

Average time:

O(n) O(n)

Worst case:

O(n2) O(n^2)

due to insertion sort if distribution is poor.

Space:

O(m) O(m)

where m0.45nm \approx 0.45n

Properties

propertyvalue
stableno
in placepartly
comparison basedpartially
distribution sensitiveyes

When to Use

Use flashsort when:

  • data is approximately uniformly distributed
  • high performance on large arrays is required
  • hybrid approach is acceptable

Avoid when:

  • distribution is highly skewed
  • worst case guarantees are required
  • stability is needed

Notes

  • Choice of mm affects performance
  • Typically m0.4nm \approx 0.4n to 0.5n0.5n
  • Final insertion sort works well because array is nearly sorted