Skip to content

Instantly share code, notes, and snippets.

@oliverbth05
Last active August 29, 2018 16:02
Show Gist options
  • Save oliverbth05/ed4c9b6f9ae85163fa2f79a0ea4a1ed9 to your computer and use it in GitHub Desktop.
Save oliverbth05/ed4c9b6f9ae85163fa2f79a0ea4a1ed9 to your computer and use it in GitHub Desktop.
Insertion Sort JS
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
}
@oliverbth05
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment