Double Selection Sort

Selects both the minimum and maximum in each pass, placing them at the beginning and end respectively. Approximately 2x fewer passes than standard selection sort.

Best O(n²) Avg O(n²) Worst O(n²) Space O(1) Stable No In-place Yes Comparison-based

How it works

Selects both the minimum and maximum in each pass, placing them at the beginning and end respectively. Approximately 2x fewer passes than standard selection sort.

Implementation

function doubleSelectionSort(arr) {
  let left = 0, right = arr.length - 1;
  while (left < right) {
    let min = left, max = left;
    for (let i = left; i <= right; i++) {
      if (arr[i] < arr[min]) min = i;
      if (arr[i] > arr[max]) max = i;
    }
    [arr[left], arr[min]] = [arr[min], arr[left]];
    if (max === left) max = min;
    [arr[right], arr[max]] = [arr[max], arr[right]];
    left++; right--;
  }
}