| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx

几种查找算法Java实现

2
Java 算法 C/C++ 12385 次浏览
    /** 
     * 类名:SearchTest.java  
     * 说明: 几种查找方法 
     */  
    public class SearchTest {  
      
        /** 
         * 函数名称:binarySearch 
         * 说明:二分查找 时间复杂度O(logN) 
         */  
        //驱动例程  
          public static <AnyType extends Comparable<? super AnyType>> int  binary(AnyType[] a,AnyType x){  
            return binary(a,0,a.length-1,x);  
          }  
        //递归  
    /*    public static <AnyType extends Comparable<? super AnyType>> int binary(AnyType[] a,int low,int high,AnyType x){ 
            if(low > high) 
                return -1; 
            int mid = (low + high)/2; 
            if(a[mid].compareTo(x) > 0 ) 
                return binary(a,low,mid - 1,x); 
            else if(a[mid].compareTo(x) < 0 ) 
                return binary(a,mid+1,high,x); 
            else return mid; 
             
        }*/  
        //非递归  
        public static <AnyType extends Comparable<? super AnyType>> int binary(AnyType[] a,int low,int high,AnyType x){  
              
            while(low <= high){  
                int mid = (low + high)/2;  
                if(a[mid].compareTo(x) > 0)  
                    high = mid -1;  
                else if(a[mid].compareTo(x) < 0)  
                    low = mid + 1;  
                else  
                    return mid;  
            }  
            return -1;  
        }  
        /** 
         * 函数名称:main  
            * 说明:测试 
         */  
        public static void main(String[] args) {  
            // TODO 自动生成的方法存根  
            Integer[] a = new Integer[]{1,2,3,4,5,6};  
            int x = 3;  
            System.out.println(binary(a,x));  
        }  
      
    }  

2个答案

0

 丢下这几人肯定不可行,哪怕是自己再渴望逃出生天,但是后续风险实在太大,而且从感情角度来说,http://24097428.shop.cnlist.org/他也难以做到一下子就舍弃掉福伯两口子和打小跟着自己的瑞祥。

0

I am a beginner in java programming but i really love ot learn it. I was simply looking forward to most of the best ways to learn this. And i am also here to check what is velvet jobs , meanwhile got through your beautiful post. Very much helpful to me.