Skip to content

Instantly share code, notes, and snippets.

@kevinjie
Created January 27, 2022 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinjie/5b81e63beb7ce7f669e798aeca05fc6f to your computer and use it in GitHub Desktop.
Save kevinjie/5b81e63beb7ce7f669e798aeca05fc6f to your computer and use it in GitHub Desktop.
shellSort
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