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.
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; }
Best Case : Ω (n log(n))
Average Case : θ (n log(n))
Worst Case: O (n^2)
O(log(n))
** where n is the length of the array