Created
May 7, 2018 13:48
-
-
Save Announcement/dc9c221ab3de27d29b70d38541b3d754 to your computer and use it in GitHub Desktop.
sorts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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