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.
const insertionSort = (array) => {
const length = array.length;
for (let i = 1; i < length; i++) {
let key = array[i];
let j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1;
}
array[j+1] = key;
}
return array;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment