Bingo Sort

Selection sort adapted for duplicates.

Best O(n+k) Avg O(n^2) Worst O(n^2) Space O(1) Stable No In-place Yes Comparison-based

How it works

Selection sort adapted for duplicates.

Implementation

function bingoSort(arr, stats) {
  const n = arr.length;
  let max = n - 1;
  let nextValue = arr[max];
  for (let i = max - 1; i >= 0; i--) {
    if (arr[i] > nextValue) nextValue = arr[i];
  }
  while (max > 0 && arr[max] === nextValue) {
    max--;
  }
  while (max > 0) {
    let value = nextValue;
    nextValue = arr[max];
    for (let i = max - 1; i >= 0; i--) {
      compare(i, max);
      if (arr[i] === value) {
        [arr[i], arr[max]] = [arr[max], arr[i]];
        swap(i, max);
        max--;
      } else if (arr[i] > nextValue) {
        nextValue = arr[i];
      }
    }
    while (max > 0 && arr[max] === nextValue) {
      max--;
    }
  }
}