Created
January 5, 2015 03:22
-
-
Save kidGodzilla/4d45f3945b9f1fb6fec1 to your computer and use it in GitHub Desktop.
Selection Sort in JavaScript
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 selectionSort = function (a) { | |
// Move forward through an array, swapping the value at i with the smallest value after i | |
for (var i = -1; ++i < a.length;) { | |
// Move forward from i and remember the position of the smallest value | |
for (var m = j = i; ++j < a.length;) { | |
// If the value at j is smaller than our current minimum, remember it's position | |
if (a[m] > a[j]) m = j; | |
} | |
// Swap the value at i with the minimum value following i | |
var t = a[m]; | |
a[m] = a[i]; | |
a[i] = t; | |
} | |
return a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment