Skip to content

Instantly share code, notes, and snippets.

@Announcement
Created May 7, 2018 13:48
Show Gist options
  • Save Announcement/dc9c221ab3de27d29b70d38541b3d754 to your computer and use it in GitHub Desktop.
Save Announcement/dc9c221ab3de27d29b70d38541b3d754 to your computer and use it in GitHub Desktop.
sorts
function InsertionSort (array, swap, compare) {
if (compare === null || compare === undefined)
compare = (a, b) => {
return a < b }
if (swap === null || swap === undefined)
swap = (a, b, c) => {
const [ x, y ] = [ a[b], a[c] ]
a[b] = y
a[c] = x; return a }
//a[b] = a[b] ^ a[c];
//a[c] = a[c] ^ a[b];
//a[b] = a[b] ^ a[c]; return a }
for (let h = 1, length = array.length; h < length; h++)
for (let k = h - 1; k >= 0; k--)
if (!compare(array[h], array[k]))
break;
else
array = swap(array, h, k);
return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment