Created
September 11, 2012 17:48
-
-
Save egermano/3700234 to your computer and use it in GitHub Desktop.
Insertion 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
function insertionSort(values) { | |
var length = values.length; | |
for(var i = 1; i < length; ++i) { | |
var temp = values[i]; | |
var j = i - 1; | |
for(; j >= 0 && values[j] > temp; --j) { | |
values[j+1] = values[j]; | |
} | |
values[j+1] = temp; | |
} | |
return values; | |
}; |
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 result = sort([7, 4, 5, 2, 9, 1]); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment