Quick Sort JavaScript

PHOTO EMBED

Thu Jul 07 2022 21:47:01 GMT+0000 (Coordinated Universal Time)

Saved by @Mahmoudsalamazz #javascript #array #quicksort

function quickSort(arr) {
  if (arr.length <= 1) {
    return arr;
  }
  const Pivot = arr[arr.length - 1];
  const LeftSide = [];
  const RightSide = [];
  for (let index = 0; index < arr.length - 1; index++) {
    if (arr[index] < Pivot) {
      LeftSide.push(arr[index]);
    } else {
      RightSide.push(arr[index]);
    }
  }
  return [...quickSort(LeftSide), Pivot, ...quickSort(RightSide)];
}
const a = quickSort([1, 2, 5, 7, 3, 0.99, -199, -1, 99]);

a; // [ -199, -1, 0.99, 1, 2, 3, 5, 7, 99 ]

content_copyCOPY