top of page
Search

Sorting Techniques - Quick Sort (Partition Exchange Sort)

Updated: Nov 9, 2025

Quick Sort method sorts one element at a time. The element selected for sorting called pivot element will be placed in array at its proper position such that to the left of it all elements are smaller or equal and to the right all elements are greater (in case of ascending sorting). An array will be divided into two parts at sorted position of the pivot and sorting continues with sub - arrays by selecting pivot element.

The purpose of partition is to allow a specific element to find its proper position with respect to the others in the subarray. It allows sorting to be performed on only subarray resulting into fewer elements in memory. It is highly efficient to sort large data sets by dividing them into smaller sub arrays.

Virtual memory is an extension of RAM to hold data which is not needed immediately, but can be accessed any time. Such data is stored in part of secondary storage (disk) which is considered part of RAM. As quick sort partitions array into sub-arrays and sorts one sub-array at a time, other sub arrays can be stored in secondary storage to avoid unnecessary occupation of memory. This method allows better utilization of memory, hence Quick Sort is best suitable for virtual memory environment.

#define MAXSZ 10


int partition(int a[], int lb, int ub)

{

    int down = 0, up = ub;

    int pivot = a[lb], hold;

    

    while (down < up)

    {

        while (a[down] <= pivot && down < ub) down++;

        while (a[up] > pivot) up--;

        

        if (down < up)

        {

            hold = a[down];

            a[down] = a[up];

            a[up] = hold;

        }

    }

    a[lb] = a[up];

    a[up] = pivot;

    

    return up;

}

If we consider the file size (or array size) n is a power of 2 ( n = 2-square), so that m = log n (log n to the base 2).If we assume proper position of pivot element always turns out to be exact middle of the sub-array. Then first pass has an array of size n and comparisons are (n-1) , this array splits after placing pivot element at its proper position resulting into 2 sub-arrays of size approximately n/2, which means 2*(n/2) comparisons in next pass.

n+ 2*(n/2) + 2*(n/3) + .............+ n*(n/n)

OR

n + n + n + ........+ n (m terms) where m = log n.

An efficiency of Quick Sort is O(n*m) OR (n * log n).

n are the comparisons and log n (log n to the base 2) are passes.

Best Case - Unsorted Data with pivot element position turns out be exactly middle of sub array always (Uniform distribution of elements) - O(n log n)

Average Case - Unsorted Data - O (n log n)

Worst Case - Sorted data in the same order or it is oppositely sorted (data is sorted in any order ascending or descending ) - O(n-square)

Quick sort needs to hold n elements and one extra memory location for swapping. Hence, its space complexity is O(n+1).

Quick Sort and Bubble Sort both methods are known as exchange sort methods. Both methods need not only comparison but also exchanging of elements (swapping).

    #define MAXSZ 10


    int partition(int a[], int lb, int ub)

    {

        int down = 0, up = ub;

        int pivot = a[lb], hold;

        

        while (down < up)

        {

            while (a[down] <= pivot && down < ub) down++;

            while (a[up] > pivot) up--;

            

            if (down < up)

            {

                hold = a[down];

                a[down] = a[up];

                a[up] = hold;

            }

        }

        a[lb] = a[up];

        a[up] = pivot;

        

        return up;

    }


    void quicksort(int a[], int lb, int ub)

    {

        int pos;

        if (lb < ub)

        {

            pos = partition(a,lb,ub);

            quicksort(a,lb,pos-1);

            quicksort(a,pos+1,ub);

        }

    }


    Want to read more?

    Subscribe to questionbankos.com to keep reading this exclusive post.

     
     
     

    Recent Posts

    See All
    Upcoming Sessions

    We are organizing sessions on various topics for members of our website. Some sessions will be absolutely free for all members which will be updated in due course. For more information , WhatsApp on 9

     
     
     
    Mastering Data Structures & Algorithms

    Recommended only for Advanced Learners having sound knowledge of Programming in C/C++/Java/Python. Duration - 3 Months (80 sessions approx) Fees - INR 12,000/- Session Time - Evening (9:00 PM - 10:00

     
     
     
    bottom of page