Skip to content

Instantly share code, notes, and snippets.

@aabir
Created November 20, 2019 04:21
Show Gist options
  • Save aabir/a2518af0a4da41e5c06c9a0f74326a8f to your computer and use it in GitHub Desktop.
Save aabir/a2518af0a4da41e5c06c9a0f74326a8f to your computer and use it in GitHub Desktop.
Selection Sort in Javascript
function selectionSort(items)
{
var itemLen = items.length;
var min;
for( i = 0; i < itemLen; i++){
min = i;
for( j = i+1; j < itemLen; j++ ){
if(items[j] < items[min]){
min = j
}
}
if(min !== i){
var temp = items[i];
items[i] = items[min];
items[min] = temp;
}
}
return items;
}
items = [31, 14, 20, 28];
document.write(selectionSort(items));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment