Last active
April 25, 2017 12:51
-
-
Save pawarvijay/162d09071a206dfcfcf14b9b5510722c to your computer and use it in GitHub Desktop.
Selection sort in javascript From geeksforgeeks.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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