• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會(huì)很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::
            #include <iostream>
            #include 
            <ctime>
            #include 
            <cmath>

            void quickSort(int *array, int first, int last) {
                
            if (first >= last) {
                    
            return;
                }
                
                
            int referenceValue = array[(first + last) / 2];
                
            int i = first;
                
            int j = last;
                
                
            while (i <= j) {
                    
            // 如果沒有i == j這一步, 則1, 2, 3, 4, 3排序是錯(cuò)誤的
                    while (i <= last && array[i] < referenceValue) {
                        
            ++i;
                    }
                    
                    
            while (j >= first && array[j] > referenceValue) {
                        
            --j;
                    }
                    
                    
            if (i <= j) {
                        
            // 如果沒有i == j這一步, 則3, 3, 3排序是死循環(huán)
                        int temp = array[i];
                        array[i] 
            = array[j];
                        array[j] 
            = temp;
                        
                        
            ++i;
                        
            --j;
                    }
                }
                
                quickSort(array, first, j);
                quickSort(array, i, last);
            }

            int main() {
                srand(time(
            0));
                
            int length = 0;
                
                
            for (int count = 0; count < 20++count) {
                    length 
            = rand() % 20;
                    
            int *array = new int[length];
                    
            for (int i = 0; i < length; ++i) {
                        
            *(array + i) = rand() % 50;
                    }
                    
                    quickSort(array, 
            0, length - 1);
                    
            for (int i = 0; i < length; ++i) {
                        std::cout 
            << array[i] << "";
                    }
                    
                    delete[] array;
                    
                    std::cout 
            << std::endl;
                }
                
                
            return 0;
            }


            測(cè)試結(jié)果:
            17, 20, 23, 23, 28, 31, 39, 41,
            2, 2, 6, 9, 10, 12, 20, 26, 28, 34, 40, 40, 44, 47,

            6, 18, 20, 24, 30, 35, 47,
            21, 22, 38, 43, 45, 45,
            5, 19,
            3, 7, 12, 17, 23, 29, 33, 36, 45, 45,
            6, 17, 27, 29, 44, 45,
            5, 10, 28, 29, 33, 37, 37, 49, 49,
            27, 49, 49,
            2, 2, 8, 11, 13, 17, 23, 23, 35, 39, 40,
            3, 9, 17, 21, 24, 25, 33, 38,
            0, 2, 13, 18, 23, 23, 29, 30, 38, 44,
            1, 7, 7, 15, 16, 17, 17, 19, 24, 25, 40, 46, 46,
            0, 4, 9, 14, 19, 25, 28, 29, 30, 35, 44, 45,
            3, 19, 21, 21, 32, 48,
            4, 9, 14, 20, 34, 35, 41,
            0, 3, 3, 6, 9, 13, 21, 21, 21, 27, 29, 36, 38, 41, 44, 47,
            2, 10, 18, 22, 24, 28, 36, 36, 44,
            10, 25, 31, 

            另一種快速排序,更好一點(diǎn):

                // 快速排序是對(duì)起泡排序的一種改進(jìn)

                public static void quickSort(int[] array) {

                    quickSort(array, 0, array.length - 1);

                }

                

                public static void quickSort(int[] array, int low, int height) {

                    if (low < height) {

                        int pivotLoc = partition(array, low, height);

                        quickSort(array, low, pivotLoc - 1);

                        quickSort(array, pivotLoc + 1, height);

                    }

                }


                public static int partition(int[] array, int low, int height) {

                    int temp = array[low];

                    while (low < height) {

                        while (low < height && array[height] >= temp) --height;

                        array[low] = array[height];

                        

                        while (low < height && array[low] <= temp) ++low;

                        array[height] = array[low];

                    }

                    array[low] = temp;

                    

                    return low;

                }


                public static void print(int[] array) {

                    System.out.print("[");

                    for (int i = 0; i < array.length - 1; ++i) {

                        System.out.print(array[i] + ", ");

                    }

                    if (array.length - 1 > 0) {

                        System.out.print(array[array.length - 1]);

                    }

                    System.out.print("]");

                }



            posted on 2008-04-08 18:13 逛奔的蝸牛 閱讀(231) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C/C++
            avtt天堂网久久精品| 最新久久免费视频| 久久国产热精品波多野结衣AV| 亚洲国产精品无码久久一区二区| 青青草国产精品久久| 狠狠色综合网站久久久久久久高清| 国产精品久久久久久福利漫画| 久久久久亚洲av成人无码电影| 99久久99久久精品国产片果冻| 久久久久久人妻无码| 久久成人永久免费播放| 国产99久久九九精品无码| 久久综合给合久久国产免费| 伊人久久大香线蕉综合5g| 色综合久久久久久久久五月| 国产精品美女久久久免费| 亚洲午夜久久久久久噜噜噜| 麻豆精品久久精品色综合| 精品人妻伦九区久久AAA片69 | 99久久国产综合精品五月天喷水| 色偷偷88888欧美精品久久久| 99久久精品无码一区二区毛片 | 久久综合综合久久狠狠狠97色88 | 久久亚洲AV成人无码国产| 久久精品99无色码中文字幕| 久久久久亚洲AV成人片| 久久综合久久伊人| 91秦先生久久久久久久| 久久狠狠高潮亚洲精品| 久久妇女高潮几次MBA| 久久精品国产WWW456C0M| 亚洲成色999久久网站| 久久国产免费观看精品3| 久久香蕉超碰97国产精品| 久久水蜜桃亚洲av无码精品麻豆| 一本大道久久香蕉成人网| 久久精品成人欧美大片| 国产巨作麻豆欧美亚洲综合久久 | 久久中文字幕精品| 一本一本久久aa综合精品| 久久婷婷国产剧情内射白浆|