The basic version of quick sort algorithm was invented by C. A. R. Hoare in 1960 and formally introduced quick sort in 1962. It is used on the principle of divide-and-conquer. Quick sort is an algorithm of choice in many situations because it is not difficult to implement, it is a good "general purpose" sort and it consumes relatively fewer resources during execution.

- It is in-place since it uses only a small auxiliary stack.
- It requires only
*n*log*(n)*time to sort n items. - It has an extremely short inner loop
- This algorithm has been subjected to a thorough mathematical analysis, a very precise statement can be made about performance issues.

- It is recursive. Especially if recursion is not available, the implementation is extremely complicated.
- It requires quadratic (
*i.e.,**n*^{2}) time in the worst-case. - It is fragile i.e., a simple mistake in the implementation can go unnoticed and cause it to perform badly.

Quick sort works by partitioning a given array
*A*[*p* . . *r*]
into two non-empty sub array *A*[*p* . . *q*] and
*A*[*q*+1
. . *r*] such that every key in
*A*[*p* . . *q*] is less
than or equal to every key in *A*[*q*+1 . . *r*]. Then the two subarrays
are sorted by recursive calls to Quick sort. The exact position of the partition
depends on the given array and index q
is computed as a part of the partitioning procedure.

## QuickSort

- If
p<rthenqPartition (A,p,r)- Recursive call to Quick Sort (
A,p,q)- Recursive call to Quick Sort (
A,q+r,r)

Note that to sort entire array, the initial call Quick Sort (*A*, 1,
length[*A*])

As a first step, Quick Sort chooses as pivot one of the items in the array to be sorted. Then array is then partitioned on either side of the pivot. Elements that are less than or equal to pivot will move toward the left and elements that are greater than or equal to pivot will move toward the right.

Partitioning procedure rearranges the subarrays in-place.

PARTITION (A,p,r)

x←A[p]i←p-1j←r+1- while TRUE do
- Repeat
j←j-1- until
A[j] ≤x- Repeat
i←i+1- until
A[i] ≥x- if
i<j- then exchange
A[i] ↔A[j]- else return
j

Partition selects the first key,
*A*[*p*] as a pivot key about
which the array will partitioned:

Keys ≤ *A*[*p*] will be
moved towards the left .

Keys ≥
*A*[*p*] will be
moved towards the right.

The running time of the partition procedure is
(*n*) where
*n* =
*r *-* p *+1
which is the number of keys in the array.

Another argument that running time of PARTITION on a subarray of size
(*n*)
is as follows: Pointer *i* and pointer
*j* start at each end and move
towards each other, conveying somewhere in the middle. The total number of times
that *i* can be incremented and
*j* can be decremented is therefore
O(*n*). Associated with each increment or decrement there are
O(1)
comparisons and swaps. Hence, the total time is O(*n*).

Since all the elements are equal, the "less than or equal" teat in lines 6
and 8 in the PARTITION (*A*, *p*, *r*) will always be true. this
simply means that repeat loop all stop at once. Intuitively, the first repeat
loop moves *j* to the left; the second repeat loop moves
*i* to the
right. In this case, when all elements are equal, each repeat loop moves
*i*
and *j* towards the middle one space. They meet in the middle, so
*q=
Floor(p+r/2)*. Therefore, when all elements in the array
*A*[*p* . . *r*]
have the same value equal to *Floor(p+r/2)*.

The running time of quick sort depends on whether partition is balanced or unbalanced, which in turn depends on which elements of an array to be sorted are used for partitioning.

A very good partition splits an array up into two equal sized arrays. A bad partition, on other hand, splits an array up into two arrays of very different sizes. The worst partition puts only one element in one array and all other elements in the other array. If the partitioning is balanced, the Quick sort runs asymptotically as fast as merge sort. On the other hand, if partitioning is unbalanced, the Quick sort runs asymptotically as slow as insertion sort.

The best thing that could happen in Quick sort would be that each
partitioning stage divides the array exactly in half. In other words, the best
to be a median of the keys in *A*[*p* . . *r*] every time
procedure 'Partition' is called. The procedure 'Partition' always split the
array to be sorted into two equal sized arrays.

If the procedure 'Partition' produces two regions of size
*n*/2. the
recurrence relation is then

T(n) = T(

n/2) + T(n/2) + (n)

= 2T(n/2) + (n)

And from case 2 of Master theorem

T(n) =
(*n *lg* n*)

The worst-case occurs if given array *A*[1 . . *n*] is already
sorted. The PARTITION (*A*, *p*,* r*) call always return
p so
successive calls to partition will split arrays of length *n*, *n*-1,
*n*-2, . . . , 2 and running time proportional to
*n* + (*n*-1) +
(*n*-2) + . . . + 2 = [(*n*+2)(*n*-1)]/2 =
(*n*^{2}).
The worst-case also occurs if *A*[1 . . *n*] starts out in reverse
order.

In the randomized version of Quick sort we impose a distribution on input. This does not improve the worst-case running time independent of the input ordering.

In this version we choose a random key for the pivot. Assume that procedure
Random (*a, b*) returns a random integer in the range [*a, b*); there
are *b*-*a*+1 integers in the range and procedure is equally likely to
return one of them. The new partition procedure, simply implemented the swap
before actually partitioning.

RANDOMIZED_PARTITION (A, p, r)

i← RANDOM (p, r)

ExchangeA[p]↔A[i]

return PARTITION (A, p, r)

Now randomized quick sort call the above procedure in place of PARTITION

RANDOMIZED_QUICKSORT (A, p, r)If p < r then

q ← RANDOMIZED_PARTITION (A, p, r)

RANDOMIZED_QUICKSORT (A, p, q)

RANDOMIZED_QUICKSORT (A, q+1, r)

Like other randomized algorithms, RANDOMIZED_QUICKSORT has the property that no particular input elicits its worst-case behavior; the behavior of algorithm only depends on the random-number generator. Even intentionally, we cannot produce a bad input for RANDOMIZED_QUICKSORT unless we can predict generator will produce next.

**Worst-case**

Let

T(n) be the worst-case time for QUICK SORT on input size n. We have a recurrence

T(n) = max_{1≤q≤n-1}(T(q) +T(n-q)) + (n) --------- 1where

qruns from 1 ton-1, since the partition produces two regions, each having size at least 1.Now we guess that

T(n) ≤cn^{2 }for some constantc.Substituting our guess in equation 1.We get

(

Tn) = max_{1≤q≤n-1}(cq^{2 }) +c(n-q^{2})) + (n)

=cmax (q^{2}+ (n-q)^{2}) + (n)Since the second derivative of expression

q^{2}+ (n-q)^{2}with respect to q is positive. Therefore, expression achieves a maximum over the range 1≤ q ≤ n -1 at one of the endpoints. This gives the bound max (q^{2}+ (n-q)^{2})) 1 + (n-1)^{2 = }n^{2 }+ 2(n-1).Continuing with our bounding of

T(n) we get

T(n) ≤c[n^{2}- 2(n-1)] + (n)

=cn^{2}- 2c(n-1) + (n)Since we can pick the constant so that the 2

c(n-1) term dominates the (n) term we have

T(n) ≤cn^{2}Thus the worst-case running time of quick sort is (

n).^{2}

If the split induced by RANDOMIZED_PARTITION puts constant fraction of elements on one side of the partition, then the recurrence tree has depth (lg

n) and (n) work is performed at (lgn) of these level. This is an intuitive argument why the average-case running time of RANDOMIZED_QUICKSORT is (nlgn).Let

T(n) denotes the average time required to sort an array of n elements. A call to RANDOMIZED_QUICKSORT with a 1 element array takes a constant time, so we haveT(1) = (1).After the split RANDOMIZED_QUICKSORT calls itself to sort two subarrays. The average time to sort an array

A[1 . .q] isT[q] and the average time to sort an arrayA[q+1 . .n] isT[n-q]. We have

T(n) = 1/n(T(1) +T(n-1) +^{n-1}∑_{q=1}T(q) +T(n-q))) + (n) ----- 1We know from worst-case analysis

T(1) = (1) andT(n-1) =O(n^{2})T(n) = 1/n((1) +O(n^{2})) + 1/n^{n-1}∑_{q=1 }(r(q) +T(n-q))_{ + }(n)

= 1/n^{ n-1}∑_{q=1}(T(q) +T(n-q)) + (n) ------- 2

= 1/n[2^{ n-1}∑_{k=1}(T(k)] + (n)

= 2/n^{ n-1}∑_{k=1}(T(k) + (n) --------- 3Solve the above recurrence using substitution method. Assume inductively that

T(n) ≤anlgn+bfor some constantsa> 0 andb> 0.If we can pick 'a' and 'b' large enough so that

nlgn+b> T(1). Then for n > 1, we have

T(n) ≥^{ n-1}∑_{k=1 }2/n(aklgk+b) + (n)

= 2a/n^{n-1}∑_{k=1 }klgk -1/8(n^{2}) + 2b/n(n-1) + (n) ------- 4At this point we are claiming that

^{n-1}∑_{k=1 }klgk ≤ 1/2 n^{2 }lgn -1/8(n^{2})Stick this claim in the equation 4 above and we get

T(n)≤2a/n[1/2n^{2 }lgn- 1/8(n^{2})] + 2/n b(n-1) + (n)

≤ anlgn-an/4 + 2b +(n) ---------- 5In the above equation, we see that (

n) +bandan/4 are polynomials and we certainly can choose 'a' large enough so thatan/4 dominates (n) +b.We conclude that QUICKSORT's average running time is (

nlg(n)).

**Conclusion**

Quick sort is an in place sorting
algorithm whose worst-case running time is
(*n ^{2}*)
and expected running time is
(

voidquickSort(intnumbers[],intarray_size) { q_sort(numbers, 0, array_size - 1); }voidq_sort(intnumbers[],intleft,intright) {intpivot, l_hold, r_hold; l_hold = left; r_hold = right; pivot = numbers[left];while(left < right) {while((numbers[right] >= pivot) && (left < right)) right--;if(left != right) { numbers[left] = numbers[right]; left++; }while((numbers[left] <= pivot) && (left < right)) left++;if(left != right) { numbers[right] = numbers[left]; right--; } } numbers[left] = pivot; pivot = left; left = l_hold; right = r_hold;if(left < pivot) q_sort(numbers, left, pivot-1);if(right > pivot) q_sort(numbers, pivot+1, right); }