Skip to content

Instantly share code, notes, and snippets.

@koalicioous
Created August 12, 2023 00:35
Show Gist options
  • Save koalicioous/56f89e888bdec1623e1125998680b5e2 to your computer and use it in GitHub Desktop.
Save koalicioous/56f89e888bdec1623e1125998680b5e2 to your computer and use it in GitHub Desktop.
JavaScript Insertion Sort
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