Skip to content

Instantly share code, notes, and snippets.

@mitrakmt
Last active November 26, 2019 23:28
Show Gist options
  • Save mitrakmt/9e1b3440db54daa78bd9a4db0ceea3c1 to your computer and use it in GitHub Desktop.
Save mitrakmt/9e1b3440db54daa78bd9a4db0ceea3c1 to your computer and use it in GitHub Desktop.
Exploring insertion sort in JavaScript.
function sort(array, compare) {
for (var i = 1; i < array.length; i++) {
var item = array[i];
var indexHole = i;
while (indexHole > 0 && compare(array[indexHole - 1], item) > 0) {
array[indexHole] = array[--indexHole];
}
array[indexHole] = item;
if (sortExternal.shiftObserver) {
sortExternal.shiftObserver(i, indexHole);
}
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment