Created
October 30, 2025 10:12
-
-
Save mustafadalga/66a1471cbeca0758c4d5e69dceaa0c5d to your computer and use it in GitHub Desktop.
Insertion sort algorithm
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
| 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