Skip to content

Instantly share code, notes, and snippets.

@vit0rr
Created October 17, 2022 03:28
Show Gist options
  • Save vit0rr/3ad5b3544265ecc560e809153e4f479e to your computer and use it in GitHub Desktop.
Save vit0rr/3ad5b3544265ecc560e809153e4f479e to your computer and use it in GitHub Desktop.
Insertion sort example with TS
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