fueanta

Insertion Sort in Typescript

Jul 1st, 2021
889
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const insertionSort = <T>(arr: T[]): void => {
  2.     for (let i = 1; i < arr.length; ++i) {
  3.         const v = arr[i];
  4.         let j = i - 1;
  5.         while (j >= 0 && arr[j] > v) {
  6.             arr[j + 1] = arr[j];
  7.             --j;
  8.         }
  9.         arr[j + 1] = v;
  10.     }
  11. }
RAW Paste Data