Signup/Sign In

C++ Program To Quick Sort Using Dynamic Array

In this tutorial, we will be learning about quicksort that takes O(nlogn) time complexity in its best case, and in its worst case, it takes O(n^2) time. The basic idea of the quicksort is to choose the pivot element and on the left side of the pivot, all the elements less than that will be placed whereas on the right-hand side all the elements greater than the pivot will be placed. Similarly, we can apply this algorithm on the left and right sides respectively using recursion.

Now the question arises that how to choose the pivot element in the given array, so you can choose any element as a pivot, here, we will be choosing the first element i.e. at the index zero as the pivot element while writing the algorithm.

Let us consider some inputs to understand what should be the required output:


array: {2 3 9 7 1}


array: {1 2 3 7 9}


array: {56 9 11 7 60}


array: {7 9 11 56 60}

Quicksort Algorithm

  1. Create a function partition()
  2. Pass three parameters arr, low, high
  3. Select rightmost element as pivot
  4. declare pointer for greater element
  5. traverse through all elements and compare them with pivot
  6. If the smaller element is found swap them with pivot
  7. Return the index position
  8. Create a function QuickSort()
  9. Pass three parameters arr, low, high
  10. Find the pivot element
  11. Do Recursive call on the left pivot and right pivot
  12. The array is now sorted
  13. Print the sorted array

C++ Program For The Quick Sort

using namespace std;

int partition(int array[],int lb,int ub){
    int pivot=array[lb];
    int start=lb;
    int end=ub;
            int temp=array[start];
    int temp=array[end];
    return end;

int quick_sort(int array[],int lb,int ub){
        int loc=partition(array,lb,ub);
    return 0;

int main(){
    int arr[]={9,6,11,10,2,5};
    int n=sizeof(arr)/sizeof(arr[0]);
    cout<<"Elements of the array after sorting are:-"<<endl;
    for(int i=0;i<n;i++){
        cout<<arr[i]<<" ";
    return 0;

Elements of the array after sorting are:-
2 5 6 9 10 11


We have seen the implementation and the basic principle behind the working of quicksort. Also, we have learned the working algorithm by taking an example.

About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.