Other Type Of Sorting : Click here :: most important ::
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
void display(int *x,int s)
{
cout<<endl;
for(int i=0;i<s;i++)
{
cout<<x[i]<<" ";
}}
int partition(int* A, int p, int r)
{
int pivot = r;
int low = p;
int high = r-1;
while(low<high)
{ display(A,9);
while(low<high && A[low]<A[pivot])
{
low++;
}
while(low<high && A[high]>A[pivot])
{
high--;
}
if(A[high]<A[low]){
int n=A[low];
A[low]=A[high];
A[high]=n; }
} if(A[low]>A[pivot]) {
int temp=A[low];
A[low]=A[pivot];
A[pivot]=temp; }
return low;
}
void quickSort(int* A, int p, int r)
{
if( p < r )
{
int q = partition(A, p, r);
quickSort(A, p, q-1);
quickSort(A, q+1, r);
}
}
void main()
{
clrscr();
int A[]={3,1,2,8,7,4,5,6,9};
quickSort(A, 0,8);
display(A,9);
getch();
}
---------------------------------
Another : -
----------------------------------
#include <iostream.h>
#include <conio.h>
void swap(int *a,int b,int c)
{
int temp=a[b];
a[b]=a[c];
a[c]=temp;
}
void display(int *a,int s)
{
cout<<endl;
for(int i=0;i<=s;i++){
cout<<a[i]<<" "; }
cout<<endl;
}
void quickSort(int *a,int,int);
int partition(int *, int,int);
void main()
{
int A[] = {6,10,13,5,8,3,2,25,4,11};
int p=0;
int q=10;
clrscr();
cout<<"======Original======="<<endl;
display(A,9);
quickSort(A,p,q);
cout<<"======Sorted======="<<endl;
display(A,9);
getch();
}
void quickSort(int *A, int p,int q)
{
int r;
if(p<q)
{ display(A,9); getch();
r=partition(A, p,q);
quickSort(A,p,r);
display(A,9); getch();
quickSort(A,r+1,q);
display(A,9);
}
}
int partition(int *A, int p,int q)
{
int x= A[p];
int i=p;
int j;
for(j=p+1; j<q; j++)
{ display(A,9); getch();
if(A[j]<=x)
{
i=i+1;
swap(A,i,j);
}
}
swap(A,i,p);
return i;
}
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
void display(int *x,int s)
{
cout<<endl;
for(int i=0;i<s;i++)
{
cout<<x[i]<<" ";
}}
int partition(int* A, int p, int r)
{
int pivot = r;
int low = p;
int high = r-1;
while(low<high)
{ display(A,9);
while(low<high && A[low]<A[pivot])
{
low++;
}
while(low<high && A[high]>A[pivot])
{
high--;
}
if(A[high]<A[low]){
int n=A[low];
A[low]=A[high];
A[high]=n; }
} if(A[low]>A[pivot]) {
int temp=A[low];
A[low]=A[pivot];
A[pivot]=temp; }
return low;
}
void quickSort(int* A, int p, int r)
{
if( p < r )
{
int q = partition(A, p, r);
quickSort(A, p, q-1);
quickSort(A, q+1, r);
}
}
void main()
{
clrscr();
int A[]={3,1,2,8,7,4,5,6,9};
quickSort(A, 0,8);
display(A,9);
getch();
}
---------------------------------
Another : -
----------------------------------
#include <iostream.h>
#include <conio.h>
void swap(int *a,int b,int c)
{
int temp=a[b];
a[b]=a[c];
a[c]=temp;
}
void display(int *a,int s)
{
cout<<endl;
for(int i=0;i<=s;i++){
cout<<a[i]<<" "; }
cout<<endl;
}
void quickSort(int *a,int,int);
int partition(int *, int,int);
void main()
{
int A[] = {6,10,13,5,8,3,2,25,4,11};
int p=0;
int q=10;
clrscr();
cout<<"======Original======="<<endl;
display(A,9);
quickSort(A,p,q);
cout<<"======Sorted======="<<endl;
display(A,9);
getch();
}
void quickSort(int *A, int p,int q)
{
int r;
if(p<q)
{ display(A,9); getch();
r=partition(A, p,q);
quickSort(A,p,r);
display(A,9); getch();
quickSort(A,r+1,q);
display(A,9);
}
}
int partition(int *A, int p,int q)
{
int x= A[p];
int i=p;
int j;
for(j=p+1; j<q; j++)
{ display(A,9); getch();
if(A[j]<=x)
{
i=i+1;
swap(A,i,j);
}
}
swap(A,i,p);
return i;
}
0 comments:
Post a Comment