Skip to content

Instantly share code, notes, and snippets.

@yashuvit89
Created April 2, 2014 15:24
Show Gist options
  • Save yashuvit89/9936340 to your computer and use it in GitHub Desktop.
Save yashuvit89/9936340 to your computer and use it in GitHub Desktop.
Selection Sort
var arr = [7, 10, 5, 3, 8, 4, 2, 9, 6];
var selectionSort = function(arr){
var i,
j,
min;
for (i = 0; i < arr.length; i++){
min = i;
for (j = i+1; j < arr.length; j++){
if(arr[j] < arr[min]){
min = j;
}
}
if(min !== i){
swap (i, min, arr);
}
}
console.log(arr);
}
function swap (ele1, ele2, arr){
var temp;
temp = arr[ele1];
arr[ele1] = arr[ele2];
arr[ele2] = temp;
}
//Execute selectionSort
selectionSort(arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment