Quick Sort Algorithm
Time-complexity:: Worst case (array already sorted/reverse sorted or too many duplicates): O(n^2), Average case : O(nlogn), Best case(when partition always results middle element as pivot):O(nlogn) Auxiliary Space: O(1), not Stable preferred for sorting arrays over merge sort
Algorithm
quicksort(A, lo, hi) is
* if lo < hi then
* p := partition(A, lo, hi)
* quicksort(A, lo, p)
* quicksort(A, p + 1, hi)
* partition(A, lo, hi) is
* pivot := A[lo]
* i := lo – 1
* j := hi + 1
*loop forever
* do
* i := i + 1
* while A[i] < pivot
*do
* j := j – 1
*while A[j] > pivot
*if i >= j then
* return j
*swap A[i] with A[j]