• <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>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::
            #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排序是錯誤的
                    while (i <= last && array[i] < referenceValue) {
                        
            ++i;
                    }
                    
                    
            while (j >= first && array[j] > referenceValue) {
                        
            --j;
                    }
                    
                    
            if (i <= j) {
                        
            // 如果沒有i == j這一步, 則3, 3, 3排序是死循環
                        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;
            }


            測試結果:
            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, 

            另一種快速排序,更好一點:

                // 快速排序是對起泡排序的一種改進

                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 逛奔的蝸牛 閱讀(229) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            久久国产精品免费一区二区三区| 久久发布国产伦子伦精品| 国产成人综合久久久久久| 久久激情五月丁香伊人| 精品久久久中文字幕人妻| 久久久无码精品亚洲日韩按摩| 久久免费线看线看| 狠狠色婷婷久久一区二区 | 久久精品国产WWW456C0M| 久久午夜夜伦鲁鲁片免费无码影视 | 亚洲国产精品久久电影欧美| 亚洲精品国产成人99久久| 精品国产乱码久久久久软件| 97超级碰碰碰久久久久| yy6080久久| 欧美久久天天综合香蕉伊| 久久最新精品国产| 久久久久人妻一区精品色| 久久综合色之久久综合| 欧美综合天天夜夜久久| 久久久久亚洲av无码专区| 久久久久久国产a免费观看不卡| 久久久久久久亚洲Av无码| 久久91精品国产91| 亚洲午夜精品久久久久久浪潮| 国产—久久香蕉国产线看观看| 久久国产乱子精品免费女| 久久久久亚洲精品天堂| 久久久噜噜噜www成人网| 久久受www免费人成_看片中文| 久久精品国产福利国产琪琪| 91精品观看91久久久久久| 嫩草影院久久国产精品| 9191精品国产免费久久| 久久av高潮av无码av喷吹| 久久国产成人精品国产成人亚洲| 亚洲国产精品一区二区久久| 大香网伊人久久综合网2020| 久久e热在这里只有国产中文精品99| 久久精品www| 久久亚洲2019中文字幕|