Skip to content

Instantly share code, notes, and snippets.

@egermano
Created September 11, 2012 17:48
Show Gist options
  • Save egermano/3700234 to your computer and use it in GitHub Desktop.
Save egermano/3700234 to your computer and use it in GitHub Desktop.
Insertion Sort
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;
};
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