欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用分治法实现二叉查找

程序员文章站 2022-05-24 07:50:20
...
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myjava;
public class BinarySearch {

    static void find(int[] A, int low, int high, int num) {
       
        if (high - low <= 0 ) {
           if(A[high] == num)
           System.out.println("find it");
        return;
        }
         find(A, low, (low + high) / 2, num);
        find(A, (low + high) / 2 + 1, high, num);
    
    }
    static boolean find_bool(int[] A, int low, int high, int num){
   if (high - low <= 0) {
            if (A[high] == num) {
                System.out.println("find it");
                return true;
            }
            else {
                return false;}
        } else {
            return find_bool(A, low, (low + high) / 2, num) & find_bool(A, (low + high) / 2 + 1, high, num);
        }
    
    }
        

    public static void main(String[] args) {
        int[] A = {1, 2, 3, 12, 34, 4, 56, 11, 31, 5};
         BinarySearch.find(A, 0, 9, 1);
      

    }
}