Header Ads Widget

Quick Sort Algorithm

Sorting is a way of arranging items in a systematic manner. Quicksort is the widely used sorting algorithm that makes n log n comparisons in average case for sorting an array of n elements. It is a faster and highly efficient sorting algorithm. This algorithm follows the divide and conquer approach. Divide and conquer is a technique of breaking down the algorithms into subproblems, then solving the subproblems, and combining the results back together to solve the original problem.

Divide: In Divide, first pick a pivot element. After that, partition or rearrange the array into two sub-arrays such that each element in the left sub-array is less than or equal to the pivot element and each element in the right sub-array is larger than the pivot element.

Conquer: Recursively, sort two subarrays with Quicksort.

Combine: Combine the already sorted array.

Quicksort picks an element as pivot, and then it partitions the given array around the picked pivot element. In quick sort, a large array is divided into two arrays in which one holds values that are smaller than the specified value (Pivot), and another array holds the values that are greater than the pivot.

After that, left and right sub-arrays are also partitioned using the same approach. It will continue until the single element remains in the sub-array.

Quick Sort Algorithm

Choosing the pivot

Picking a good pivot is necessary for the fast implementation of quicksort. However, it is typical to determine a good pivot. Some of the ways of choosing a pivot are as follows -

  • Pivot can be random, i.e. select the random pivot from the given array.
  • Pivot can either be the rightmost element of the leftmost element of the given array.
  • Select median as the pivot element.

Quick Sort Pivot Algorithm

Based on our understanding of partitioning in quick sort, we will now try to write an algorithm for it, which is as follows.

Step 1 − Choose the highest/lowest index value has pivot
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot

Algorithm

enter image description here

Working of Quick Sort Algorithm

Now, let's see the working of the Quicksort Algorithm.

To understand the working of quick sort, let's take an unsorted array. It will make the concept more clear and understandable.

Let the elements of array are -

Quick Sort Algorithm

In the given array, we consider the leftmost element as pivot. So, in this case, a[left] = 24, a[right] = 27 and a[pivot] = 24.

Since, pivot is at left, so algorithm starts from right and move towards left.

Quick Sort Algorithm

Now, a[pivot] < a[right], so algorithm moves forward one position towards left, i.e. -

Quick Sort Algorithm

Now, a[left] = 24, a[right] = 19, and a[pivot] = 24.

Because, a[pivot] > a[right], so, algorithm will swap a[pivot] with a[right], and pivot moves to right, as -

Quick Sort Algorithm

Now, a[left] = 19, a[right] = 24, and a[pivot] = 24. Since, pivot is at right, so algorithm starts from left and moves to right.

As a[pivot] > a[left], so algorithm moves one position to right as -

Quick Sort Algorithm

Now, a[left] = 9, a[right] = 24, and a[pivot] = 24. As a[pivot] > a[left], so algorithm moves one position to right as -

Quick Sort Algorithm

Now, a[left] = 29, a[right] = 24, and a[pivot] = 24. As a[pivot] < a[left], so, swap a[pivot] and a[left], now pivot is at left, i.e. -

Quick Sort Algorithm

Since, pivot is at left, so algorithm starts from right, and move to left. Now, a[left] = 24, a[right] = 29, and a[pivot] = 24. As a[pivot] < a[right], so algorithm moves one position to left, as -

Quick Sort Algorithm

Now, a[pivot] = 24, a[left] = 24, and a[right] = 14. As a[pivot] > a[right], so, swap a[pivot] and a[right], now pivot is at right, i.e. -

Quick Sort Algorithm

Now, a[pivot] = 24, a[left] = 14, and a[right] = 24. Pivot is at right, so the algorithm starts from left and move to right.

Quick Sort Algorithm

Now, a[pivot] = 24, a[left] = 24, and a[right] = 24. So, pivot, left and right are pointing the same element. It represents the termination of procedure.

Element 24, which is the pivot element is placed at its exact position.

Elements that are right side of element 24 are greater than it, and the elements that are left side of element 24 are smaller than it.

Quick Sort Algorithm

Now, in a similar manner, quick sort algorithm is separately applied to the left and right sub-arrays. After sorting gets done, the array will be -

Quick Sort Algorithm

Example

Example of Non Recursive Quick Sort:

1.    44  33  11  55  77  90  40  60  99  22  88    

Let 44 be the Pivot element and scanning done from right to left

Comparing 44 to the right-side elements, and if right-side elements are smaller than 44, then swap it. As 22 is smaller than 44 so swap them.

  22  33      11      55      77      90      40      60      99      44      88     

Now comparing 44 to the left side element and the element must be greater than 44 then swap them. As 55 are greater than 44 so swap them.

22    33      11      44      77      90      40      60      99      55      88

Recursively, repeating steps 1 & steps 2 until we get two lists one left from pivot element 44 & one right from pivot element.

22    33      11      40      77      90      44      60      99      55      88

Swap with 77:

22    33      11      40      44      90      77      60      99      55      88

Now, the element on the right side and left side are greater than and smaller than 44 respectively. 

Now we get two sorted lists:

DAA Quick sort

And these sublists are sorted under the same process as above done.

These two sorted sublists side by side.

DAA Quick sort
DAA Quick sort

Merging Sublists:

DAA Quick sort

                          SORTED LISTS

Quick-sort complexity

Now, let's see the time complexity of quicksort in best case, average case, and in worst case. We will also see the space complexity of quicksort.

1. Time Complexity

Case

Time Complexity

Best Case

O(n*logn)

Average Case

O(n*logn)

Worst Case

O(n2)

  • Best Case Complexity - In Quicksort, the best-case occurs when the pivot element is the middle element or near to the middle element. The best-case time complexity of quicksort is O(n*logn).
  • Average Case Complexity - It occurs when the array elements are in jumbled order that is not properly ascending and not properly descending. The average case time complexity of quicksort is O(n*logn).
  • Worst Case Complexity - In quick sort, worst case occurs when the pivot element is either greatest or smallest element. Suppose, if the pivot element is always the last element of the array, the worst case would occur when the given array is sorted already in ascending or descending order. The worst-case time complexity of quicksort is O(n2).

Though the worst-case complexity of quicksort is more than other sorting algorithms such as Merge sort and Heap sort, still it is faster in practice. Worst case in quick sort rarely occurs because by changing the choice of pivot, it can be implemented in different ways. Worst case in quicksort can be avoided by choosing the right pivot element.

2. Space Complexity

Space Complexity

O(n*logn)

Stable

NO

  • The space complexity of quicksort is O(n*logn).

Implementation of quicksort

Now, let's see the programs of quicksort in different programming languages.

Program: Write a program to implement quicksort in C language.

#include <stdio.h>  

/* function that consider last element as pivot,  

place the pivot at its exact position, and place  

smaller elements to left of pivot and greater  

elements to right of pivot.  */  

int partition (int a[], int start, int end)  

{  

    int pivot = a[end]; // pivot element  

    int i = (start - 1);  

  

    for (int j = start; j <= end - 1; j++)  

    {  

        // If current element is smaller than the pivot  

        if (a[j] < pivot)  

        {  

            i++; // increment index of smaller element  

            int t = a[i];  

            a[i] = a[j];  

            a[j] = t;  

        }  

    }  

    int t = a[i+1];  

    a[i+1] = a[end];  

    a[end] = t;  

    return (i + 1);  

}  

  

/* function to implement quick sort */  

void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting index, end = Ending index */  

{  

    if (start < end)  

    {  

        int p = partition(a, start, end); //p is the partitioning index  

        quick(a, start, p - 1);  

        quick(a, p + 1, end);  

    }  

}  

  

/* function to print an array */  

void printArr(int a[], int n)  

{  

    int i;  

    for (i = 0; i < n; i++)  

        printf("%d ", a[i]);  

}  

int main()  

{  

    int a[] = { 24, 9, 29, 14, 19, 27 };  

    int n = sizeof(a) / sizeof(a[0]);  

    printf("Before sorting array elements are - \n");  

    printArr(a, n);  

    quick(a, 0, n - 1);  

    printf("\nAfter sorting array elements are - \n");    

    printArr(a, n);  

      

    return 0;  

}  

  Output:

Quick Sort Algorithm

Post a Comment

0 Comments