JS Built-ins
Algorithmsbeginner

Selection Sort

πŸ“– Usage Example
function selectionSort(arr) {
  const a = [...arr];
  for (let i = 0; i < a.length; i++) {
    let min = i;
    for (let j = i+1; j < a.length; j++) if (a[j] < a[min]) min = j;
    [a[i], a[min]] = [a[min], a[i]];
  }
  return a;
}

Selection sort finds the minimum of the unsorted portion and moves it to the front.

Selection Sort

Selection Sort divides the array into two parts: sorted and unsorted. It repeatedly finds the minimum element from the unsorted part and moves it to the end of the sorted part.

Time Complexity: O(nΒ²) Space Complexity: O(1)

function selectionSort(arr: number[]): number[] {
  // Implement selection sort
  // Find minimum and swap it to correct position
}

selectionSort([64, 25, 12, 22, 11]) // [11, 12, 22, 25, 64]
πŸ’‘ Hints (3)
  • β€’ Create a copy of the array with `[...arr]` to avoid mutation.
  • β€’ For each position, find the minimum element in the remaining array.
  • β€’ Swap the minimum element with the current position.