Last active
August 29, 2018 16:02
-
-
Save oliverbth05/ed4c9b6f9ae85163fa2f79a0ea4a1ed9 to your computer and use it in GitHub Desktop.
Insertion Sort JS
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(arr) { | |
for (var i = 1; i < arr.length; i++) { | |
if (arr[i] < arr[i-1]) { | |
for (var j = 0; j <= i; j++) { | |
if (arr[j] > arr[i]) { | |
var temp = arr[j]; | |
arr[j] = arr[i] | |
arr[i] = temp | |
} | |
} | |
} | |
} | |
return arr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No exactly an insertion sort. Instead of the second loop starting from the top (i - 1), it starts from the 0 index and goes up until i.