#include<stdio.h>
void fun(int array[],int low,int high)
{
int i = low;
int j = high;
int temp = array[i];
while(i < j)
{
while((array[j] >= temp) && (i < j))
{
j--;
array[i] = array[j];
}
while((array[i] <= temp) && (i < j))
{
i++;
array[j]= array[i];
}
}
array[i] = temp;
if(i-1>low)
{
fun(array,low,i-1);
}
if(high>i+1)
{
fun(array,j+1,high);
}
else
{
return;
}
}
void main()
{
int array[10];
printf("输入十个数字进行快速排序:\n");
for(int i=0;i<10;i++)
{
printf("请输入第%d个数:",i+1);
scanf("%d",&array[i]);
}
fun(array,0,9);
printf("对这十个数字从小到大快速排序得:");
for(i=0;i<10;i++)
{
printf("%d ",array[i]);
}
printf("\n");
}
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;
}