Quick Sort

Description

Quick sort,is a Divide and Conquer algorithm.A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value. Quicksort partitions an array and then calls itself recursively twice to sort the two resulting subarrays. This algorithm is quite efficient for large-sized data sets as its average and worst-case complexity are O(nLogn) and O(n2), respectively.

Code


  function quick(arr,start,end) {
      let i = start,j = end,mid = Math.floor((start+end)/2);
      let pivot = arr[mid];
      while(i <= j) {
          while(arr[i] < pivot) {
              i++;
          }
          while(arr[j] > pivot) {
              j--;
          }
          if(i <= j) {
              swaps++;
              let temp = arr[i];
              arr[i] = arr[j];
              arr[j] = temp;
              i++; j--;
          }
      }
      if(start < i - 1)
          quick(arr,start,i - 1);
      if(i < end)
          quick(arr,i,end);
      return;
  }
  
  
  function quickMain(arr){
      start = 0 ;
      end = arr.length - 1 ;
      quick(arr,start,end);
      return arr;
  }
                    
Complexity
Time Complexity:

Best Case : Ω (n log(n))
Average Case : θ (n log(n))
Worst Case: O (n^2)

Space Complexity:

O(log(n))

** where n is the length of the array

To Learn more about Quick Sort, refer the link given below: Quick sort