Created
Sep 2, 2025
Last Modified
3 months ago

Quick Sort

Quick Sort

It is a popular sorting method based on the divide-and-conquer approach. Unlike merge sort, where the division is always in the middle, in quicksort the division can take place at any position.

In quicksort, the division depends on the value of an element. Partition is the situation in quicksort where, if we achieve partition about some index s, then:

  • a[s] is the pivot element.

  • All elements preceding a[s] are <= a[s].

  • All elements following a[s] are >= a[s].

Clearly, after partition, a[s] takes its final position in the sorted array. Partition is the most important procedure of quicksort. After achieving partition about s, a[s] takes its final position, leaving two unsorted subarrays:

  • the part preceding a[s], and

  • the part following a[s].

In quicksort, the pivot (partition element) can be at any location, depending on the value of the key element. The simplest strategy is to select the first element as the pivot.


Worst Case of Quick Sort

For input size N, when elements are in strictly increasing order, after the first comparison we achieve partition at the first position, replacing the first element by itself. This leaves us with a subarray of size N – 1.

Again, after the next comparison, the first element of this subarray replaces itself, leaving a subarray of size N – 2.

This process continues. Finally, quicksort will sort the subarray of size 2, and after that the array will be completely sorted.

Thus, in the worst case, quicksort performs:

$By Comutative low (a + b) = (b + a)

AP Series:

$

  • = common difference

  • = first term of the series

  • = term of the series

Sum of first n term of an ap series

$


we have

  • = 1

  • = 1

  • term =


If

T(n)g(n)$ grow at the same rate.

So,



Best Case of Quick Sort

The best case of quicksort occurs when we achieve partition exactly in the middle of the subarray.

For an array , partitioning happens at its mid position. After partition, the array is divided into two equal parts:

  • of size if is odd (), or

  • of size if is even ().

This means that in the very first position, the pivot divides the array into two equal halves.

If is the time taken by quicksort to sort an array of size , then from recurrence, we have:

on solving


Average Case of Quick Sort

The possibility of getting a partition at any position is , where .

If is the average time to sort an array of size , then the recurrence is:

  • = Cost of partitoning

  • = Cost of left Subarray.

  • = Cost of right Subarray.


Notice the Symmetry of Double Counting

Notice that for each pivot , the recurrence adds . Because of symmetry:

k=1n$ with two terms each, we can just sum over all subarray sizes once and multiply by 2.

From Recurrance relation (2) we have

We have:

Eq. (2):

Eq. (3):


Step 1: Subtract LHS


Step 2: Subtract RHS

Simplify each part:

  • The summations cancel except the last term:

So the RHS =


Step 3: Final Relation


Multiply on both side

Put in this