Created
April 2, 2014 15:24
-
-
Save yashuvit89/9936340 to your computer and use it in GitHub Desktop.
Selection Sort
This file contains 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
var arr = [7, 10, 5, 3, 8, 4, 2, 9, 6]; | |
var selectionSort = function(arr){ | |
var i, | |
j, | |
min; | |
for (i = 0; i < arr.length; i++){ | |
min = i; | |
for (j = i+1; j < arr.length; j++){ | |
if(arr[j] < arr[min]){ | |
min = j; | |
} | |
} | |
if(min !== i){ | |
swap (i, min, arr); | |
} | |
} | |
console.log(arr); | |
} | |
function swap (ele1, ele2, arr){ | |
var temp; | |
temp = arr[ele1]; | |
arr[ele1] = arr[ele2]; | |
arr[ele2] = temp; | |
} | |
//Execute selectionSort | |
selectionSort(arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment