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

            misschuer

            常用鏈接

            統計

            積分與排名

            百事通

            最新評論

            ternarysearch

            /*本算法是搜索來的, 稍加修飾了下, 貼出來, 以后容易找*/
            /* Note:Your choice is C IDE */
            #include 
            <iostream>
            #include 
            <algorithm>
            #include 
            <ctime>
            #include 
            <cstdio>
            #include 
            <cstring>
            #define N 101
            using namespace std;

            int partition(int arg[], int low, int high) {
                
                
            int x = arg[high];
                
            int i = low - 1;
                
            int ex;
                
                
            for(int j = low; j <= high - 1++ j) {
                    
                    
            if (arg[ j ] <= x) {
                        
                        i 
            ++;
                        
            if(i == j) continue ;
                        ex 
            = arg[ i ];
                        arg[ i ] 
            = arg[ j ];
                        arg[ j ] 
            = ex;
                    }
                }

                ex 
            = arg[i + 1];
                arg[i 
            + 1= arg[high];
                arg[high] 
            = ex;
                
            return i + 1;
            }

            void quickSort(int arg[], int low, int high) {
                
                
            if (low < high) {
                    
                    
            int item = partition(arg, low, high);
                    quickSort(arg, low, item 
            - 1);
                    quickSort(arg, item 
            + 1, high);
                }
            }

            int ternarySearch(int arg[], int x, int low, int high) {
                
                
            int Fpiont;
                
            int Spiont;
                
            int l = high - low ;
                
                Fpiont 
            = (high - low) / 3 + low;
                Spiont 
            = 2 * (high - low) / 3 + low;
                
                
            if(x < arg[Fpiont] && x > arg[low] && l > 3)
                    
            return ternarySearch(arg, x, low, Fpiont);
                
                
            if(x < arg[Spiont] && x > arg[Fpiont] && l > 3)
                    
            return ternarySearch(arg, x, Fpiont, Spiont);
                
                
            if(x > arg[Spiont] && x < arg[high] && l > 3)
                    
            return ternarySearch(arg, x, Spiont, high);
                
                
            if(x == arg[Spiont]) return Spiont;
                
                
            if(x == arg[Fpiont]) return Fpiont;
                
                
            if(x == arg[low]) return low;
                
                
            if(x == arg[high]) return high;
                
                
            return -1;
            }

            int main() {
                
                
            int input[N];
                
            int ans, x, i;
                
                srand( (unsigned)time( NULL ) );
                
                
            //數字最好不要重復
                for(i = 0; i < N; ++ i)
                    input[ i ] 
            = rand() % 100 ;
                
                sort(input, input 
            + N);
                
                
            for (i = 0; i < N; ++ i) printf("%d\t",input[ i ]);
                
                printf(
            "\nPlease input the number that you want to search: ");
                
                scanf(
            "%d",&x);
                
                ans 
            = ternarySearch(input, x, 0, N-1);
                
                
            if(ans == -1) printf("can't find!\n");
                
            else printf("its position is %d\n",ans + 1);
                
                puts(
            "finish!");
                
            return 0;
            }

            posted on 2011-03-17 13:09 此最相思 閱讀(163) 評論(0)  編輯 收藏 引用

            99久久精品费精品国产| 久久露脸国产精品| 91精品国产91久久久久久| 久久人人爽人人爽AV片| 97精品国产97久久久久久免费| 2021久久国自产拍精品| 亚洲国产日韩欧美综合久久| 精品综合久久久久久888蜜芽| 国产精品伦理久久久久久| 国产亚洲精久久久久久无码77777| 久久精品国产半推半就| 777午夜精品久久av蜜臀| 开心久久婷婷综合中文字幕| 久久精品国产亚洲av影院| 久久久久综合中文字幕| 高清免费久久午夜精品| 精品久久久久久久久免费影院| 久久综合九色综合精品| 久久午夜羞羞影院免费观看| 色99久久久久高潮综合影院| 伊人久久精品线影院| 久久久久无码精品国产| 狠狠综合久久AV一区二区三区| 国产农村妇女毛片精品久久| 天天躁日日躁狠狠久久| 精品无码久久久久国产动漫3d| 久久久久99精品成人片牛牛影视| 国内精品久久国产大陆| 狠狠色丁香久久婷婷综合五月| 97精品依人久久久大香线蕉97 | 人妻无码αv中文字幕久久| 欧美麻豆久久久久久中文| 国产无套内射久久久国产| 久久国产一区二区| 久久99毛片免费观看不卡| 精品永久久福利一区二区| 狠狠色噜噜色狠狠狠综合久久 | 国产99久久久久久免费看| 国内精品久久久久影院网站| 狠狠色丁香久久综合五月| 99久久国产综合精品五月天喷水|