美文网首页
QUICK SORT

QUICK SORT

作者: larrymusk | 来源:发表于2017-11-27 16:28 被阅读0次
    
    int partition(int A[] , int len)
    {
            int pivot = A[0];
    
            int low = 0;
            int high = len-1;
            while(low < high){
                    for(; high>low && A[high] >= pivot; high--);
                            A[low] = A[high];
                    for(; high>low && A[low] <= pivot; low++);
                            A[high] = A[low];
    
            }
    
            A[low] = pivot;
    
            return low;
    }
    
    void quicksort(int A[], int len)
    {
            if(len == 0 || len == 1)
                    return;
    
            int index = partition(A,len);
    
            quicksort(A, index);
            quicksort(A+index+1, len-index-1);
    }
    int main()
    {
            int A[] = {1,5,4,7,9,2,3,6,8};
    
            int len = sizeof(A)/sizeof(A[0]);
    
            for(int i = 0; i < len ; i++)
                    printf("%d ", A[i]);
    
            printf("\n");
            quicksort(A,len);
            for(int i = 0; i < len ; i++)
                    printf("%d ", A[i]);
    
            printf("\n");
            return 1;
    }
    

    相关文章

      网友评论

          本文标题:QUICK SORT

          本文链接:https://www.haomeiwen.com/subject/jjydbxtx.html