Created
January 25, 2022 02:21
-
-
Save kevinjie/7fb2d04c287a5a328ec7513fa7a7da3c to your computer and use it in GitHub Desktop.
insertionSort
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 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