Created
January 28, 2016 22:55
-
-
Save cdmz/8e6e6673b9f18f847ae5 to your computer and use it in GitHub Desktop.
insertion_sort 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
function insertion_sort(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; | |
}; | |
console.log(insertion_sort([7, 4, 5, 2, 9, 1])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment