C++ Program for Quicksort



Quick sort algorithm is also known as partition exchange sort and is based on partitioning of an array of data into smaller sub-arrays. A large array is partitioned into two arrays such that the left sub-array holds values smaller than the pivot element and the right sub-array holds values greater than the pivot value.

Quicksort partitions an array and then calls itself recursively twice to sort the two resulting sub-arrays: left and right sub-arrays. Quick sort falls uses the divide and conquer approach of problem-solving methodology. This algorithm is quite efficient for large-sized data sets as its average and worst-case complexity are O(n^2), respectively.

In this article, we have an unsorted array. Our task is to implement the quick sort algorithm to sort the given array in C++.

Example

The following example sorts the given array using the quick sort algorithm:

Input:
array = {50, 23, 9, 18, 61, 32}

Output:
Sorted array = {9, 18, 23, 32, 50, 61}

The explanation of the above example is as follows:

In this example, we will choose last element as pivot.
First Pass: pivot = 32
If element > pivot, no swapping. 
If element < pivot, swap with the previous element.

50 > 32 => no swapping, 23 < 32 => swap 50 and 23,
9 < 32 => swap 50 and 9, 18 < 32 => swap 50 and 18,
61 > 32 => no swapping.
Array after first pass: 23, 9, 18, 32, 61, 50

Second Pass:
For left sub-array pivot = 18 and right sub-array pivot = 50.
Similarly, array after second pass: 9, 18, 23, 32, 50, 61

Array after sorting: 9, 18, 23, 32, 50, 61

Here is the visual representation of the above explanation:

Quick Sort Algorithm

Steps to Implement Quick Sort Algorithm

  • First, we will choose a pivot element in the given array. In this code, we have used the rand() function to choose a random pivot element. Then, we swap this randomly chosen pivot element with the element at the end of the array.
  • The we partition the array around the pivot element such that the left sub-array has elements less than the pivot element and the right sub-array has the elements greater than or equal to the pivot element.
  • The partition function implements the above step. If the current element is less than the pivot element, then we swap this element with the element marked as low. This places all the elements lower than the pivot element in the left sub-array.
  • After choosing the pivot element and partitioning, we recursively call the quickSort() function until there is a single element in the sub-array.

C++ Implementation of Quick Sort Algorithm

The C++ implementation of the quick sort technique using the above steps is as follows:

#include <iostream>
#include <cstdlib>  

using namespace std;

void swap(int &a, int &b) {
   int temp = a;
   a = b;
   b = temp;
}

int partition(int a[], int low, int high) {
   int pivot = high;
   int index = low;
   for(int i = low; i < high; i++) {
      if(a[i] < a[pivot]) {
         swap(a[i], a[index]);
         index++;
      }
   }
   swap(a[pivot], a[index]);
   return index;
}

int pivot(int a[], int low, int high) {
   int n = rand();
   int pvt = low + n % (high - low + 1);
   swap(a[high], a[pvt]);
   return partition(a, low, high);
}

void quickSort(int a[], int low, int high) {
   if(low < high) {
      int pindex = pivot(a, low, high);
      quickSort(a, low, pindex - 1);
      quickSort(a, pindex + 1, high);
   }
}

int main() {
   int arr[] = {7, 4, 2, 6, 3, 1, 5};
   int n = sizeof(arr)/sizeof(arr[0]);

   cout << "Array before sorting: ";
   for(int i = 0; i < n; i++)
      cout << arr[i] << " ";

   quickSort(arr, 0, n - 1);
   cout << "\nSorted array: ";
   for(int i = 0; i < n; i++)
      cout << arr[i] << " ";
   return 0;
}

The output of the above code is as follows:

Array before sorting: 7 4 2 6 3 1 5 
Sorted array: 1 2 3 4 5 6 7 
Updated on: 2025-05-12T16:52:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements