Binary MSD Radix Sort

Bitwise most-significant-bit-first partitioning radix sort for integer keys.

Best O(n) Avg O(nw) Worst O(nw) Space O(log n) Stable No In-place Yes Distribution

How it works

Bitwise most-significant-bit-first partitioning radix sort for integer keys.

Implementation

function binaryMSDRadixSort(arr, stats) {
  function sort(lo, hi, bit) {
    if (lo >= hi || bit < 0) return;
    let i = lo;
    let j = hi;
    while (i <= j) {
      while (i <= j) {
        const left = (arr[i] ^ 0x80000000) >>> bit;
        if ((left & 1) !== 0) break;
        i++;
      }
      while (i <= j) {
        const right = (arr[j] ^ 0x80000000) >>> bit;
        if ((right & 1) === 0) break;
        j--;
      }
      if (i < j) {
        swap(i, j);
        const t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
        i++;
        j--;
      }
    }
    sort(lo, j, bit - 1);
    sort(i, hi, bit - 1);
  }
  if (arr.length > 1) sort(0, arr.length - 1, 31);
}