• <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 此最相思 閱讀(165) 評論(0)  編輯 收藏 引用

            久久天天躁狠狠躁夜夜躁2O2O| 久久人人爽人人爽人人片av麻烦| 99久久久精品| AA级片免费看视频久久| 久久精品女人天堂AV麻| 亚洲AV无码久久寂寞少妇| 久久精品男人影院| 久久精品国产亚洲AV蜜臀色欲| 久久午夜无码鲁丝片| 精品久久久无码中文字幕| 蜜臀久久99精品久久久久久小说| 成人a毛片久久免费播放| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 伊人久久大香线蕉综合Av| 久久久久久A亚洲欧洲AV冫| 久久综合九色综合网站| 久久无码精品一区二区三区| 国产成年无码久久久久毛片| 热99RE久久精品这里都是精品免费| 国产精品对白刺激久久久| 亚洲国产成人久久一区久久| 91精品婷婷国产综合久久| 久久婷婷五月综合97色一本一本 | 欧美国产成人久久精品| 99久久免费国产精品| 99久久99这里只有免费费精品| 伊人久久国产免费观看视频| 久久996热精品xxxx| 久久久青草青青国产亚洲免观| 大美女久久久久久j久久| 曰曰摸天天摸人人看久久久| 久久精品国产免费| 日韩精品久久久久久| 久久中文娱乐网| 久久国产精品77777| 国产精品一久久香蕉国产线看观看| 亚洲精品乱码久久久久久| 久久久亚洲裙底偷窥综合| 精品久久久一二三区| 久久久噜噜噜久久中文字幕色伊伊| 久久人妻少妇嫩草AV无码蜜桃|