Advanced JavaScript Training
Quick Sort

Quick Sort

  • Quick Sort is a popular, efficient, and divide-and conquer sorting algorithm.It selects a 'pivot' element and partitions the array into two sub-arrays:elements less than the pivot and elements greater than the pivot.
  • It has an average time complexity of O(n log n) and is often used in practice.
  
    function quickSort(arr) {
        if (arr.length <= 1) return arr;

        const pivot = arr[0];
        const left = [];
        const right = [];

        for (let i = 1; i < arr.length; i++) {
            if (arr[i] < pivot) {
                left.push(arr[i]);
           } else {
               right.push(arr[i]);
           }
       }

       return quickSort(left).concat(pivot, quickSort(right));
   }

   const unsortedArray = [64, 34, 25, 12, 22, 11, 90];
   const sortedArray = quickSort(unsortedArray);
  
Have a doubt?
Post it here, our mentors will help you out.