Quick Sort In C++

Link Copied To Clipboard !

quicksort-in-cpp Algorithms

Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.

  1. Always pick first element as pivot.(implemented below)
  2. Always pick last element as pivot.
  3. Pick a random element as pivot.
  4. Pick median as pivot.

The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.

Quick Sort In C++



#include<iostream>
using namespace std;

class List{
    private:
        int* arr;
        int len;

        int partition(int lower, int upper){
            if(arr == NULL) return -1;
            int temp;
            int pivot = *(arr + lower);
            int pivotIndex = lower;
            while(lower < upper){
                while(*(arr + lower) <= pivot) lower++;
                while(*(arr + upper) > pivot) upper--;
                if(lower < upper){
                    temp = *(arr + lower);
                    *(arr + lower) = *(arr + upper);
                    *(arr + upper) = temp;
                }
            }
            temp = *(arr + pivotIndex);
            *(arr + pivotIndex) = *(arr + upper);
            *(arr + upper) = temp;
            return upper;
        }
    public:
        List(){
            this->arr = NULL;
            this->len = 0;
        }

        void populate(){
            do{
                cout<<"enter the size:"<<endl;
                cin>>this->len;
            }while(this->len < 0);

            this->arr = new int[this->len];

            for(int i = 0; i < this->len; i++){
                cout<<"enter "<<i<<"th item"<<endl;
                cin>>*(arr+i);
            }
        }

        void quicksort(int lower, int upper){
            if(arr != NULL && lower < upper){
                int part = partition(lower, upper);
                quicksort(lower, part-1);
                quicksort(part+1, upper);
            }
        }

        void display(){
            if(arr == NULL){
                cout<<"list is empty !"<<endl;
                return;
            }
            cout<<"[";
            for(int i = 0; i < this->len; i++){
                if(i != len-1){
                    cout<<*(arr+i)<<", ";
                }else{
                    cout<<*(arr+i);
                }
            }
            cout<<"]"<<endl;
        }

        int length(){
            return this->len;
        }

        ~List(){
            delete arr;
        }
};

int main(){
    List list;
    list.populate();
    cout<<"unsorted list:";
    list.display();
    cout<<"sorted list:";
    list.quicksort(0, list.length()-1);
    list.display();
    return 0;
}


You May Also Like