Skip to content

Commit

Permalink
Implement insertionSort.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Dec 31, 2014
1 parent b593183 commit e957260
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions insertionSort/insertionSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@
// Example usage:
// insertionSort([2, 1, 3]); // yields [1, 2, 3]

var insertionSort = function(array) {
// Your code goes here. Feel free to add helper functions if needed.
var insertionSort = function (array) {
var length = array.length;
var i, j, hole;

if (length < 2) { return array; }
for (i = 1; i < length; i += 1) {
hole = array[i];
j = i;
while (j > 0 && array[j - 1] > hole) {
array[j] = array[j - 1];
j -= 1;
}
array[j] = hole;
}

return array;
};

0 comments on commit e957260

Please sign in to comment.