Skip to content

Instantly share code, notes, and snippets.

@mustafadalga
Created October 30, 2025 10:12
Show Gist options
  • Save mustafadalga/66a1471cbeca0758c4d5e69dceaa0c5d to your computer and use it in GitHub Desktop.
Save mustafadalga/66a1471cbeca0758c4d5e69dceaa0c5d to your computer and use it in GitHub Desktop.
Insertion sort algorithm
function insertionSort(arr: number[]) {
for (let i = 1; i < arr.length; i++) {
const val = arr[i]
let j = i - 1
while (j >= 0 && arr[j] > val) {
arr[j + 1] = arr[j]
j--
}
arr[j + 1] = val
}
return arr
}
console.log(insertionSort([ 9, 3, 6, 2, 1, 11 ]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment