Exchange Sort

Systematic comparisons globally.

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

How it works

Systematic comparisons globally.

Implementation

function exchangeSort(arr, stats) {
  const n = arr.length;
  for (let i = 0; i < n - 1; i++) {
    for (let j = i + 1; j < n; j++) {
      compare(i, j);
      if (arr[i] > arr[j]) {
        [arr[i], arr[j]] = [arr[j], arr[i]];
        swap(i, j);
      }
    }
    checkpoint(i, n - 1);
  }
  for (let i = 0; i < n; i++) markSorted(i);
}