Created
October 17, 2022 03:28
-
-
Save vit0rr/3ad5b3544265ecc560e809153e4f479e to your computer and use it in GitHub Desktop.
Insertion sort example with TS
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
type Props = { | |
length: number; | |
arr: number[]; | |
} | |
function insertionSort(arr: Props['arr']) { | |
for (let i = 1; i < arr.length; i++) { | |
let currentVal = arr[i]; | |
for (var j = i - 1; j >= 0 && arr[j] > currentVal; j--) { | |
arr[j + 1] = arr[j]; | |
} | |
arr[j + 1] = currentVal; | |
console.log(arr); | |
} | |
} | |
insertionSort([5, 2, 4, 6, 1, 3]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment