fueanta

Bubble Sort in Typescript

Jul 1st, 2021
943
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const bubbleSort = <T>(arr: T[]): void => {
  2.     let swap: boolean;
  3.  
  4.     do {
  5.         swap = false;
  6.  
  7.         for (let i = 0; i < arr.length - 1; ++i) {
  8.             if (arr[i] > arr[i + 1]) {
  9.                 swap = true;
  10.  
  11.                 const temp = arr[i + 1];
  12.                 arr[i + 1] = arr[i];
  13.                 arr[i] = temp;
  14.             }
  15.         }
  16.     } while (swap);
  17. }
RAW Paste Data