• <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)  編輯 收藏 引用

            99麻豆久久久国产精品免费| 久久国产视屏| 久久66热人妻偷产精品9| 亚洲一区二区三区日本久久九| 99久久成人18免费网站| 久久精品人人做人人爽电影| 色成年激情久久综合| 久久99九九国产免费看小说| www.久久99| 久久天堂AV综合合色蜜桃网| 久久精品国产WWW456C0M| 久久亚洲精精品中文字幕| 久久国产免费| 国产日韩久久久精品影院首页| 香蕉久久夜色精品升级完成| 久久久久亚洲爆乳少妇无| 777米奇久久最新地址| 精品综合久久久久久97| 亚洲精品乱码久久久久久蜜桃 | 日产精品久久久久久久| Xx性欧美肥妇精品久久久久久| 人妻无码αv中文字幕久久琪琪布| 亚洲国产成人精品无码久久久久久综合| 亚洲午夜久久久影院| 7777精品伊人久久久大香线蕉| 久久人人爽人人爽人人片AV麻豆 | 久久精品国产久精国产思思| 久久人人爽人人爽人人片AV不| 久久一区二区三区免费| 久久国产影院| 人妻无码精品久久亚瑟影视| 久久激情亚洲精品无码?V| 久久男人中文字幕资源站| 美女久久久久久| 亚洲欧洲精品成人久久曰影片| 亚洲第一永久AV网站久久精品男人的天堂AV | 久久精品视频一| 国产免费久久精品99re丫y| 久久午夜免费视频| 国产偷久久久精品专区| 色欲久久久天天天综合网|