Skip to content

Instantly share code, notes, and snippets.

@kevinjie
Created January 25, 2022 02:21
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/7fb2d04c287a5a328ec7513fa7a7da3c to your computer and use it in GitHub Desktop.
Save kevinjie/7fb2d04c287a5a328ec7513fa7a7da3c to your computer and use it in GitHub Desktop.
insertionSort
export default function sort(originalArray) {
const arr = [...originalArray]
for(let i = 1; i < arr.length; i += 1) {
let currentIndex = i
while(
arr[currentIndex - 1] !== undefined
&& arr[currentIndex - 1] > arr[currentIndex]
) {
[
arr[currentIndex],
arr[currentIndex - 1]
] = [
arr[currentIndex - 1],
arr[currentIndex]
]
currentIndex -= 1
}
}
return arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment