posts - 97,  comments - 93,  trackbacks - 0
          呵呵 不知道寫的如何。但是總感覺,在UltraEdit下寫,還是很爽的,或許是因為學校辦公室的電腦內存太小的緣故吧。
           1 /**
           2  * @the author:Nicky(EN) QuQiang(CH)
           3  * @the data :2006.8.27
           4  **/
           5 // 當然本算法最好編制成兩到3個文件
           6 
           7 public class HalfSearch{
           8     private static int lengthValue;
           9     private static byte  flag=0;
          10     private static int SearchValue=0;
          11    
          12     //定義返回數組類型的方法  此方法給出的public權限
          13     public static int[] Sort(int[] b){
          14         for(int i=0;i<b.length;i++){
          15             for(int j=i+1;j<b.length;j++){
          16                 int temp;
          17                 if(b[i]>b[j]){
          18                     temp=b[i];b[i]=b[j];b[j]=temp;
          19                 }
          20             }
          21         }
          22         System.out.println("\u6309\u5e8f\u6392\u5217\u4e3a:");  //對應漢字為     按序排列為:
          23         for(int i=0;i<b.length;i++){
          24           System.out.println(b[i]);
          25         }                            //冒泡排序法排序  或直接使用JDK提供的sort()函數進行排列
          26         return b;
          27     }
          28 
          29     //折半查找
          30     private static int Half(int[] a){
          31         int m=a.length,k,n=1;
          32         System.out.println("\u5f53\u524d\u5171\u8f93\u5165"+m+"\u4e2a\u6570\u7b26"); //對應漢字為  "當前共輸入" 個數符
          33         int index=0;
          34         if(m==1){
          35            if(a[0]==SearchValue){
          36              flag=1;
          37              index=1;
          38            }
          39            else{
          40               flag=0;
          41               index=1;
          42            }
          43         }             //考慮到用戶除輸入查找數符外,只輸入一個數符,即n=m=1
          44         for(int i=1;;i++){
          45           if(n<m){
          46              index+=1;
          47              k=(n+m)/2;
          48              if(a[k-1]>SearchValue){
          49                  m=k-1;      //此時定義最大值為當前中間值的前一個值  //第 次查找未成功,當前值大于SearchValue
          50                  System.out.println("\u7b2c"+index+"\u6b21\u67e5\u627e\u672a\u6210\u529f\uff0c \u5f53\u524d\u503c\u5927\u4e8e"+SearchValue);//否則將9改為變量
          51              }else if(a[k-1]<SearchValue){
          52                  n=k+1;          //未找到,則在后半區間進行查找  //小于
          53                  System.out.println("\u7b2c"+index+"\u6b21\u67e5\u627e\u672a\u6210\u529f\uff0c \u5f53\u524d\u503c\u5c0f\u4e8e"+SearchValue);//否則將9改為變量
          54              }else{
          55                  System.out.println("\u67e5\u627e\u6210\u529f");  //查找成功
          56                  flag=1;
          57                  break;
          58              }
          59            }else break;
          60          }
          61         return index;
          62     }                      //也可以定義一個用戶需輸入的查找值
          63    
          64     //使用說明
          65    private static void usage(){
          66       System.out.println("Usage:java HalfSearch program [the number you search] [the array list you give]");
          67       System.out.println("\t [the number you search] the only number you want to use it to test this program");
          68       System.out.println("\t [the Array list you give] A array list may be it contains it may be not");
          69       System.exit(0);
          70    }
          71       
          72     //主函數的功能  傳入查找數組
          73     public static void main(String[] args){
          74         if(args.length==0||args.length==1){
          75             usage();
          76             System.exit(0);
          77         }
          78         SearchValue=Integer.parseInt(args[0]);
          79         lengthValue=args.length;
          80         int index=0;
          81         int[] TransArray=new int[args.length-1];
          82         for(int i=1;i<args.length;i++){
          83             TransArray[i-1]=Integer.parseInt(args[i]);
          84             System.out.println(TransArray[i-1]);
          85         }
          86         index=HalfSearch.Half(HalfSearch.Sort(TransArray));
          87         //輸出查詢結果
          88         switch(flag){
          89              //在本次折半查找中共經過  次查找,未成功   成功
          90             case 0: System.out.println("\u5728\u672c\u6b21\u6298\u534a\u67e5\u627e\u4e2d\u5171\u7ecf\u8fc7"+index+"\u6b21\u67e5\u627e,\u672a\u6210\u529f");break;
          91             case 1: System.out.println("\u5728\u672c\u6b21\u6298\u534a\u67e5\u627e\u4e2d\u5171\u7ecf\u8fc7"+index+"\u6b21\u67e5\u627e,\u6210\u529f");break;
          92         }
          93         System.exit(0);
          94     }
          95 }


          posted on 2006-08-27 17:02 wqwqwqwqwq 閱讀(649) 評論(0)  編輯  收藏 所屬分類: Data Structure && Algorithm
          <2006年8月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789




          常用鏈接

          留言簿(10)

          隨筆分類(95)

          隨筆檔案(97)

          文章檔案(10)

          相冊

          J2ME技術網站

          java技術相關

          mess

          搜索

          •  

          最新評論

          閱讀排行榜

          校園夢網網絡電話,中國最優秀的網絡電話
          主站蜘蛛池模板: 宁波市| 呼伦贝尔市| 芦溪县| 临洮县| 惠东县| 桓仁| 社旗县| 县级市| 绥中县| 合川市| 津南区| 益阳市| 临武县| 香格里拉县| 乌鲁木齐县| 肇源县| 广饶县| 商河县| 济阳县| 南靖县| 来安县| 德兴市| 余姚市| 泽州县| 简阳市| 闸北区| 阿拉善左旗| 牙克石市| 麟游县| 齐河县| 青铜峡市| 湘潭市| 霸州市| 江津市| 四平市| 新邵县| 呈贡县| 晋州市| 江山市| 德阳市| 陆良县|