#include<iostream>
using namespace std;
void swap(int& a,int& b)
{
int c=a;
a=b;
b=c;
}
int partion(int a[10],int low,int hi)
{
int i,j,ce,lo,h1,b[3];
ce=(low+hi)/2;
if((a[ce]>=a[low] && a[ce]<=a[hi])||(a[ce]<=a[low] && a[ce]>=a[hi]))
swap(a[low],a[ce]);
else if((a[hi]>=a[low] && a[hi]<=a[ce])||(a[hi]<=a[low] && a[hi]>=a[ce]))
swap(a[low],a[hi]);
lo=low+1;
h1=hi;
while(lo<=hi)
{
while(a[lo]<a[low])
lo++;
while(a[hi]>a[low] && hi>=lo)
hi--;
if(lo<hi)
{
swap(a[lo],a[hi]);
hi--;lo++;
}
}
swap(a[low],a[hi]);
return hi;
}
void quick(int a[10],int low,int hi)
{
int ce;
if(low<hi)
{
ce=partion(a,low,hi);
quick(a,low,ce-1);
quick(a,ce+1,hi);
}
}
int main()
{
int data[20],n,co=0;
cout<<"enter data\n";
cin>>n;
while(n!=-1) //-1 for end inputs
{
data[co++]=n;
cin>>n;
}
quick(data,0,co-1);
for(int i=0;i<co;i++) //output in sorted order
cout<<data[i]<<" ";
return 0;
}
using namespace std;
void swap(int& a,int& b)
{
int c=a;
a=b;
b=c;
}
int partion(int a[10],int low,int hi)
{
int i,j,ce,lo,h1,b[3];
ce=(low+hi)/2;
if((a[ce]>=a[low] && a[ce]<=a[hi])||(a[ce]<=a[low] && a[ce]>=a[hi]))
swap(a[low],a[ce]);
else if((a[hi]>=a[low] && a[hi]<=a[ce])||(a[hi]<=a[low] && a[hi]>=a[ce]))
swap(a[low],a[hi]);
lo=low+1;
h1=hi;
while(lo<=hi)
{
while(a[lo]<a[low])
lo++;
while(a[hi]>a[low] && hi>=lo)
hi--;
if(lo<hi)
{
swap(a[lo],a[hi]);
hi--;lo++;
}
}
swap(a[low],a[hi]);
return hi;
}
void quick(int a[10],int low,int hi)
{
int ce;
if(low<hi)
{
ce=partion(a,low,hi);
quick(a,low,ce-1);
quick(a,ce+1,hi);
}
}
int main()
{
int data[20],n,co=0;
cout<<"enter data\n";
cin>>n;
while(n!=-1) //-1 for end inputs
{
data[co++]=n;
cin>>n;
}
quick(data,0,co-1);
for(int i=0;i<co;i++) //output in sorted order
cout<<data[i]<<" ";
return 0;
}
No comments:
Post a Comment