Sharif_
مدیر بازنشسته
بنا به درخواست دوستان
کد:
#include<iostream.h>
int *a,NUM_ITEMS;
int main()
{
void enterdata();
void quicksort(int,int);
int i;
cout<<"\n\nEnter the number of items:";
cin>>NUM_ITEMS;
a=new int [NUM_ITEMS];
if(a==NULL)
cout<<"Memory allocation error";
cout<<"\nEnter the numbers:";
enterdata();
quicksort(0,NUM_ITEMS-1);
cout<<"Done with sort.\n";
for (i = 0; i <NUM_ITEMS; i++)
cout<<"\n"<<a[i];
return 0;
}
void enterdata()
{
int i;
for(i=0;i<NUM_ITEMS;i++)
cin>>a[i];
}
void quicksort(int lower,int upper)
{
int split(int,int),pivot;
if(upper>lower)
{
pivot=split(lower, upper);
quicksort(lower,pivot-1);
quicksort(pivot+1,upper);
}
}
int split(int lower,int upper)
{
int i,p,q,t;
p=lower+1;
q=upper;
i=a[lower];
while(q>=p)
{
while(a[p]<i)
p++;
while(a[q]>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[lower];
a[lower]=a[q];
a[q]=t;
return q;
}