快排序加测试

#include "stdio.h"
#include "stdlib.h"

void QuickSort(int*a,int low,int high)
{
        int left,right,pivot;
        pivot=a[low];
        left=low;
        right=high;
        while(low<high)
        {
                while(low<high&&a[high]>=pivot)
                        high--;
                a[low]=a[high];
                while(low<high&&a[low]<=pivot)
                        low++;
                a[high]=a[low];        
        }
        a[low]=pivot;
        
        if(left!=low)
                QuickSort(a,left,low-1);
        if(right!=high)
                QuickSort(a,low+1,right);
}

int main()
{
        int i,N,temp;
        int *a;
        while(~scanf("%d",&N))
        {
                a=(int*) malloc(sizeof(int));
                for(i=0;i<N;i++)
                        scanf("%d",&a[i]);
                if(N==1)
                {
                        printf("%d\n",a[0]);
                        printf("%d\n",-1);
                }
                else 
                {
                        for(i=0;i<N;i++)
                        {
                                if(a[i]>a[0])
                                {
                                        temp=a[0];
                                        a[0]=a[i];
                                        a[i]=temp;
                                }
                        }
                        printf("%d\n",a[0]);
                        QuickSort(a,1,N-1);
                        for(i=1;i<N;i++)
                        {
                                if(i==N-1)
                                        printf("%d\n",a[i]);
                                else
                                        printf("%d ",a[i]);
                        }

                }
        }
        return 0;        
}

 

posted @ 2013-10-06 10:51  小薇林  阅读(180)  评论(0编辑  收藏  举报