Skip to content

Instantly share code, notes, and snippets.

@ramzesucr
Created July 12, 2017 10:19
Show Gist options
  • Save ramzesucr/b3c20811a1961db6c8d8c21f7223e0f4 to your computer and use it in GitHub Desktop.
Save ramzesucr/b3c20811a1961db6c8d8c21f7223e0f4 to your computer and use it in GitHub Desktop.
Insertion sort
const insertionSort = (input) => {
let data = input.slice()
for(var i = 1; i < data.length; i++) {
for(var j = i; j > 0; j--) {
if(data[j] < data[j - 1]) { exchange(data, j - 1, j) } else { break }
}
}
return data;
}
const exchange = (list, left, right) => {
const mid = list[left]
list[left] = list[right]
list[right] = mid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment