Created
August 12, 2023 00:35
-
-
Save koalicioous/56f89e888bdec1623e1125998680b5e2 to your computer and use it in GitHub Desktop.
JavaScript Insertion Sort
This file contains 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
const arr = [10,11,13,5,6,6,2] | |
const insertionSort = (arr: number[]) => { | |
for (let i = 1; i < arr.length; i++) { | |
let currentValue = arr[i] | |
let j = i - 1 | |
while (j >= 0 && arr[j] > currentValue) { | |
arr[j + 1] = arr[j] | |
j-- | |
} | |
arr[j + 1] = currentValue | |
} | |
return arr | |
} | |
console.log(insertionSort(arr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment