Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #68 from MadhushaPrasad/feature/InsertionSort
Browse files Browse the repository at this point in the history
Feat (InsertionSort) : Create InsertionSort TypeScript Example
  • Loading branch information
chamodshehanka authored Oct 18, 2021
2 parents 9ab8ae7 + aa56d45 commit e58995d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sorting Algorithms/Insertion Sort/InsertionSort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const insertionSort = (arr: Array<number>) => {
for (let i = 1; i < arr.length; i++) {
let max = arr[i];
let j = i - 1;

while ((j > -1) && (arr[j] > max)) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = max;
}
};

const ar: number [] = [56, 12, 32, 87, 65, 54, 43, 32, 31, 2];
console.log(ar);
insertionSort(ar);
console.log(ar);

0 comments on commit e58995d

Please sign in to comment.