-Selection Sort: Introduction
-Selection Sort: Implementation
Selection Sort: Introduction
Selection sort is similar to bubble sort, but instead of first placing the large values into sorted positions, it places small values into sorted positions.
Selection Sort: Implementation
Selection Sort Example
function sselectionSort(arr){
for(var i = 0; i < arr.length; i++){
var lowest = i;
for(var j = i+1; j < arr.length; j++){
if(arr[j] < arr[lowest]){
lowest = j;
}
}
if(i !== lowest){
//SWAP!
var temp = arr[i];
arr[i] = arr[lowest];
arr[lowest] = temp;
}
}
return arr;
}
function selectionSort(arr) {
const swap = (arr, idx1, idx2) =>
([arr[idx1], arr[idx2]] = [arr[idx2], arr[idx1]]);
for (let i = 0; i < arr.length; i++) {
let lowest = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[lowest] > arr[j]) {
lowest = j;
}
}
if (i !== lowest) swap(arr, i, lowest);
}
return arr;
}
selectionSort([0,2,34,22,10,19,17]);
Top comments (0)