Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawarvijay/f0dea040be9db4c92182c327390b0bfa to your computer and use it in GitHub Desktop.
Save pawarvijay/f0dea040be9db4c92182c327390b0bfa to your computer and use it in GitHub Desktop.
Sort numbers without effecting alphabets - sort by selection sort
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++)
{
if (typeof items[j] === 'string') {
} else {
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, 'p', 'a', 5, 3, 'i', 7, 1, 'b', 4, 8])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment