Skip to content

Instantly share code, notes, and snippets.

@hiroshi-maybe
Created February 3, 2013 09:25
Show Gist options
  • Save hiroshi-maybe/4701045 to your computer and use it in GitHub Desktop.
Save hiroshi-maybe/4701045 to your computer and use it in GitHub Desktop.
Selection sort implemented with Javascript.
var selectionSort = function(array) {
for(var i=0, length=array.length; i<length; i+=1) {
var min_i = i;
for(var j=i+1; j<length; j+=1) {
if (array[j] < array[min_i]) {
min_i = j;
}
}
var small = array[min_i];
array[min_i] = array[i];
array[i] = small;
}
return array;
};
console.log(selectionSort([8,4,3,7,6,5,2,1]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment