There are many way to sort a list using quick sort concept .i.e recursive way & iterative way.
 |
| quick sort |
Recursive Approach
- choose a no in the list a pivote
- now sort list such that no less then pivote no. come on left of it & other on right
- In above steps list break in two part . let left list as brand new list and repeat all steps(1,2,3).
- do similar operation on right list as on left.
Now how to do Steps 2. there are many ways I did mention few of them..
Related post : bubble-sort-in-c
 |
| quick sort algorithm |
Steps shown in image :
- first we choose last element as pivote i.e. 66
- Than move from left to right till encounter first no greater than pvote(66).
- Than move from upper bound -1 to first no. encounter less than pivote (66).
- Now swap element found in step 2 & step 3.
- Repeat step 2,3,4 until encounter index of data found in step 2 is greater than thay of index of data in step 3
- Now put pivote at divider position.by swapping to element found at step three in step 5.
Note : see the Program given below carefully.follow all comments .And remember in our C program we take first element at pivote.
Quick Sort ( C++ Program ):
//Quick SOrt
//beginer2cs
#include<iostream>
#include<new>
using namespace std;
void Display(int *A,int n);
void Quick_sort(int *A,int p, int r);
int Partition(int *A,int p,int r);
int main(){
int size,*arr;
cout<<"Enter Size of list to sort : ";
cin>>size;
arr=new int[size]; //allocate space
for(int i=0;i<size;i++){
cin>>arr[i];
}
Quick_sort(arr,0,size-1);
Display(arr,size);
return 0;
}
void Quick_sort(int *A,int p, int r){
int q;
if(p<r){
q=Partition(A,p,r);
Quick_sort(A,p,q-1); //left Subarray
Quick_sort(A,q+1,r); //right subarray
}
}
int Partition(int *A,int p,int r){
int pivote=A[p]; //we choose first element as pivote
int dn=p;
int up=r;
int temp;
while(dn<up){
while(A[dn]<=pivote)
dn++;
while(A[up]>pivote) //important point
up--;
if(dn<up){
//swap dn & up index value
temp=A[dn];
A[dn]=A[up];
A[up]=temp;
}
else
break;
}
//put pivote at middle
temp=A[up];
A[up]=A[p];
A[p]=temp;
return up; //return index of pivote
}
void Display(int *A,int n){
int i;
cout<<"Dispaly Data :";
for(i=0;i<n;i++){
cout<<A[i]<<" ";
}
cout<<endl;
}
Quick Sort Modified (C++)
//Quick SOrt
//Modified Partition function
#include<iostream>
#include<new>
using namespace std;
void Display(int *A,int n);
void Quick_sort(int *A,int p, int r);
int Partition(int *A,int p,int r);
void swap(int &a, int &b);
int main(){
int size,*arr;
cout<<"Enter Size of list to sort : ";
cin>>size;
arr=new int[size]; //allocate space
for(int i=0;i<size;i++){
cin>>arr[i];
}
Quick_sort(arr,0,size-1);
Display(arr,size);
return 0;
}
void Quick_sort(int *A,int p, int r){
int q;
if(p<r){
q=Partition(A,p,r);
Quick_sort(A,p,q-1); //left Subarray
Quick_sort(A,q+1,r); //right subarray
}
}
int Partition(int *A,int p,int r){
int pivote=A[r]; //we choose last element as pivote
int i=p-1;
int temp;
for(int j=p;j<=r-1;j++){
if(A[j]<=pivote){
i++;
//swap A[i] & A[j]
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
temp=A[i+1];
A[i+1]=A[r];
A[r]=temp;
return i+1;
}
void Display(int *A,int n){
int i;
cout<<"Dispaly Data :";
for(i=0;i<n;i++){
cout<<A[i]<<" ";
}
cout<<endl;
}
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT