fueanta

Selection Sort in Typescript

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