OneCompiler

ascending order in selection sort

137

function selectionsort(a) {
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;
}
}

if (i !== min) {
  let temp = a[i];
  a[i] = a[min];
  a[min] = temp;
}

}
return a;
}

const res = selectionsort([34, 25, 24, 5, 8, 2]);
console.log(res);