fueanta

Quick Sort in Typescript

Jul 1st, 2021
1,255
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const quickSort = <T>(array: T[]): T[] => {
  2.     if (array.length < 2)
  3.         return array;
  4.  
  5.     const lastIndexOfArray = array.length - 1;
  6.     const pivot = array[lastIndexOfArray];
  7.  
  8.     const leftArray: T[] = [], rightArray: T[] = [];
  9.  
  10.     for (const element of array.slice(0, lastIndexOfArray))
  11.         (element < pivot) ? leftArray.push(element) : rightArray.push(element);
  12.  
  13.     return [...quickSort(leftArray), pivot, ...quickSort(rightArray)];
  14. }
  15.  
  16. export default quickSort;
  17.  
RAW Paste Data