测试程序
- /*快速排序 2011.10.1*/
-
- #include <iostream>
- #include<algorithm>
- using namespace std;
- int partion(int a[],int low,int high) //返回枢轴位置
- {
- int i=low;
- int j=high;
- int pivot=a[low];
- while(i<j)
- {
- while(i<j&&a[j]>=pivot)
- {
- j--;
- }
- if(i<j)
- {
- swap(a[i],a[j]);
- i++;
- }
- while(i<j&&a[i]<=pivot)
- {
- i++;
- }
- if(i<j)
- {
- swap(a[i],a[j]);
- j--;
- }
- }
- return i;
- }
- void quickSort(int a[],int low,int high) //快速排序
- {
- if(low<high)
- {
- int pivotpos=partion(a,low,high);
- quickSort(a,low,pivotpos-1);
- quickSort(a,pivotpos+1,high);
- }
- }
- int main(int argc, char *argv[])
- {
- int n;
- while(scanf("%d",&n)!=EOF)
- {
- int i;
- int *a=new int[n];
- for(i=0;i<n;i++)
- {
- scanf("%d",&a[i]);
- }
- quickSort(a,0,n-1);
- for(i=0;i<n;i++)
- {
- printf("%d ",a[i]);
- }
- printf("\n");
- }
- return 0;
- }
|