Skip to content

Instantly share code, notes, and snippets.

@pawarvijay
Last active April 25, 2017 12:51
Show Gist options
  • Save pawarvijay/162d09071a206dfcfcf14b9b5510722c to your computer and use it in GitHub Desktop.
Save pawarvijay/162d09071a206dfcfcf14b9b5510722c to your computer and use it in GitHub Desktop.
Selection sort in javascript From geeksforgeeks.com
function selectionSort(items)
{
console.log(items)
var length = items.length;
for (var i = 0; i < length - 1; i++)
{
var min = i;
for (var j = i + 1; j < length; j++)
{
console.log('items[j] ' + items[j] + ' items[min] ' + items[min])
if (items[j] < items[min])
{
min = j;
}
}
if (min != i)
{
var tmp = items[i];
items[i] = items[min];
items[min] = tmp;console.log(items);
}
}
}
selectionSort([ 9, 5 , 3 , 7 , 1, 4 , 8])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment