时间:2022-02-18 09:15:19 | 栏目:C代码 | 点击:次
2
#include<iostream.h>
#include<malloc.h>
void interchange(int* m,int* n)
{
int temp=*m;
*m=*n;
*n=temp;
}
int partition(int array[],int p,int q)
{
int i,j;
i=p;
j=q+1;
while(1)
{
do i++;
while((array[i]<array[p])&&(i!=q));
do j--;
while((array[j]>array[p])&&(j!=p));
if(i<j)
interchange(&array[i],&array[j]);
else
break;
}
interchange(&array[p],&array[j]);
return j;
}
void quicksort(int array[],int p,int q)
{
int j;
if (p<q)
{
j=partition(array,p,q);
quicksort(array,p,j-1);
quicksort(array,j+1,q);
}
}
void main()
{
int n,i;
cout<<"please input the number of array:";
cin>>n;
int* a=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
cout<<"please the "<<i+1<<"th element :";
cin>>a[i];
}
cout<<"before sort:";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
quicksort(a,0,n-1);
cout<<"after sort:";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}