Created
January 12, 2014 06:12
-
-
Save jack-wong-build/8381583 to your computer and use it in GitHub Desktop.
Insertion 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 insertionSort = function(array){ | |
var target; | |
var sortedIndex = 0; | |
var targetIndex; | |
for(var i = 0; i < array.length; i++){ | |
target = array[i]; | |
targetIndex = i; | |
for(var j = i; j >= sortedIndex; j--){ | |
if (array[j] > target){ | |
array[targetIndex] = array[j]; | |
array[j] = target; | |
targetIndex--; | |
} | |
} | |
} | |
return array; | |
} | |
//console.log(insertionSort([2, 1, 3, 5, 4, 7, 9, 21, 10, 5])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment