• [算法] 快速排序


    参考 http://www.cnblogs.com/morewindows/archive/2011/08/13/2137415.html

         http://baike.baidu.com/view/19016.htm?fromId=115472

     #define MAXLENGTH 10

    void quick_sort(int a[], int start, int end)
    {
        if(start<end){
        int key=a[start];//开始比较对象
        int i=start;       
        int j=end;
        while (i<j) {
            while (i<j &&a[j]>=key) {
                j--;             //开始比较右边,直到发现小于key的数
            }
            if(i<j){
                 a[i]=a[j];    
                a[j]=key;    //交换
                 i++;
            }
            while (i<j && a[i]<key) {
                i++;
            }
            if(i<j){
               a[j]=a[i];
                a[i]=key;
                 j--;
            }
        }
          a[i]=key;
       
            quick_sort(a,start,i-1);//左边
             quick_sort(a, i+1, end);//右边
        }

    }

    int main(int argc, const char * argv[])
    {

        
        @autoreleasepool {
          
            int a[MAXLENGTH]={9,56,45,76,23,45,37,12,19,3};
            for (int b=0; b<MAXLENGTH ; b++) {
                printf("%3d",a[b]);

                        }
            quick_sort(a,0,MAXLENGTH-1);//起始位置,结束位置

             printf("\n");
            for (int b=0; b<MAXLENGTH; b++) {
                printf("%3d",a[b]);
       
            }
            
        }
        return 0;
    }

  • 相关阅读:
    旧题复习{6}
    CF219D. Choosing Capital for Treeland [树形DP]
    POJ1947 Rebuilding Roads[树形背包]

    洛谷P1280 尼克的任务[DP]
    NOIP2003pj栈[卡特兰数]
    NOIP2001统计单词个数[序列DP]
    洛谷P1415 拆分数列[序列DP 状态 打印]
    POJ2828 Buy Tickets[树状数组第k小值 倒序]
    CF380C. Sereja and Brackets[线段树 区间合并]
  • 原文地址:https://www.cnblogs.com/jinjiantong/p/2969921.html
Copyright © 2020-2023  润新知