Created
January 27, 2022 02:35
-
-
Save kevinjie/5b81e63beb7ce7f669e798aeca05fc6f to your computer and use it in GitHub Desktop.
shellSort
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
export default function sort(originalArray) { | |
const array = [...originalArray] | |
let gap = Math.floor(array.length / 2) | |
while(gap > 0) { | |
for(let i = 0; i < array.length - gap; i += 1) { | |
let currentIndex = i | |
let gapShiftedIndex = i + gap | |
while(currentIndex >= 0) { | |
if (array[currentIndex] > array[gapShiftedIndex]) { | |
[ | |
array[gapShiftedIndex], | |
array[currentIndex] | |
] = [ | |
array[currentIndex], | |
array[gapShiftedIndex] | |
] | |
} | |
gapShiftedIndex = currentIndex | |
currentIndex -= gap | |
} | |
} | |
gap = Math.floor(gap / 2) | |
} | |
return array | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment