Brief explanation of Quick Sort algorithm

Manish Goyal

Manish Goyal

@manish-r2Hoep Oct 22, 2024
On request of one user from ce,i am going to explain one of the most commonly used sorting algorithm ie quick sort

It is based on Divide and conquer approach.

It is one of the common practical example of recursion

There are generally 3 steps in this

1:-Divide a problem a multiple sub problems
2:-Solve them individually.
3:-Merge those sub problems to find solution of main problem

Algorithm

1. Choose any key element in an array which is know a pivot.
2. Reorder the array such that that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it. After the partitioning, the pivot is in its final position.
3. Recursively reorder two sub-lists: the sub-list of lesser elements and the sub-list of greater elements.


Below is the program written in c++ ,it can help you for better understanding

//Program for Quicksort
#include<iostream.h>
#include<conio.h>
int partition(int a[],int,int);
void quick(int a[],int,int);
void main()
{
int *a,n,i;
cout<<"Enter size of array"<<endl;
cin>>n;
a=new int[n];
cout<<"Please enter elements for array"<<endl;
for(i=0;i<n;i++)
{
cin>>a;
}
quick(a,0,n-1);
cout<<"Elments are"<<endl;
for(i=0;i<n;i++)
{
cout<<a<<endl;
}
getch();
}
void quick(int a[],int start,int finish)
{
if(start<finish)
{
int q=partition(a,start,finish);
quick(a,start,q-1);
quick(a,q+1,finish);
}
}
int partition(int a[],int start,int finish)
{
int x=a[finish],temp,temp1,j;
int i=start-1;
for(j=start;j<=finish-1;j++)
{
if(a[j]<=x)
{
i=i+1;
temp=a;
a=a[j];
a[j]=temp;
}
}
temp1=a[i+1];
a[i+1]=a[j];
a[j]=temp1;
return (i+1);
}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • rishi0922

    rishi0922

    @rishi0922-a2xTAa Sep 2, 2010

    its very helpful nd clearing many doubts
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Sep 2, 2010

    Nice post goyal.
    Hope you come with more in future.
  • paresh006

    paresh006

    @paresh006-dn8DBN Sep 4, 2010

    Quick sort like merge sort,is based on divide and conquer paradigm..
    Three steps are required to be done these are:-

    Divide:-Suppose an array is A[p...r]...rearrange the array into two(possibly empty)subarrays A[p....q-1]and A[q+1...r] such that each element of A[p....q-1]is less than or equal to A[q],which is,in turn, less than or equal to each element of A[q+1...r]..compute the index q as part of this partitioning procedure.

    Conquer:-Sort the two subarrays A[p...q-1]and A[q+1...r]by recursive calls to quicksort.

    Combine:-Since the subarrays are sorted in place,no work is needed to combine them.the entire array A[p..r]is now sorted..

    QUICKSORT(A,p,r)

    1:if p<r
    2: then q<-PARTITIONED(A,p,r)
    3: QUICKSORT(A,p,q-1)
    4: QUICKSORT(A,q+1,r)
    To sort an entire array A,the initial call is QUICKSORT(A,1,length[A]).

    Partitioning the array

    PARTITION(A,p,r)
    1:x<-A[r]
    2:i<-p-1
    3: for j<-p to r-1
    4: do if A[j]<=x
    5: then i<-i+1
    6: exchange A<->A[j]
    7:exchangeA[i+1]<->A[r]
    8:return i+1
  • BCA_GIRL

    BCA_GIRL

    @bca-girl-wzX9cA Sep 4, 2010

    Thanks..........really its very useful.
  • anandkumarjha

    anandkumarjha

    @anandkumarjha-jzdDMA Sep 4, 2010

    Fabulous job Goyal...It's easy to learn by this method

    hope that you will bribg such useful stuff regularly