Selection Sort

Sort an array using the selection sort algorithm by repeatedly finding the minimum element.

Code

Algorithms
const a = [...arr];
for (let i = 0; i < a.length; i++) {
  let minIdx = i;
  for (let j = i + 1; j < a.length; j++) {
    if (a[j] < a[minIdx]) minIdx = j;
  }
  if (minIdx !== i) [a[i], a[minIdx]] = [a[minIdx], a[i]];
}
return a;

Parameters

Array to sort

Browser·fetch() may be limited by CORS

More JavaScript Snippets