</pre><pre name="code" class="cpp">#include <stdio.h>
#define LENGTH 10000
void swap(int* a,int x,int y){
if (x == y) return;
a[x] ^= a[y];a[y] ^= a[x];a[x] ^= a[y];
}
void qsort(int* a,int p,int r){
if(p < r){
int q = partition(a,p,r);
qsort(a,p,q-1);
qsort(a,q+1,r);
}
}
int partition(int* a,int p,int r){
int x,i,j;
x = a[r];
i = p;
for(j = p;j < r; j++)
if(a[j] <= x)
swap(a,i++,j);
swap(a,i,r);
return i;
}
void print(int* a,int x,int y){
int i;
for (i = x; i < y+1; i++)
printf("%d ",a[i]);
printf("\n");
}
int main(){
int n = 10;
int a[LENGTH] = {10,4,5,7,23,2,4,5,4,1};
print(a,0,n-1);
qsort(a,0,n-1);
print(a,0,n-1);
/*
//poj 2388
int n,i;
int a[LENGTH] = {0};
scanf("%d",&n);
for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
qsort(a,0,n-1);
printf("%d",a[n/2]);
*/
return 0;
}
快排代码
最新推荐文章于 2024-03-29 15:31:23 发布