用分治法实现二叉查找
程序员文章站
2022-05-24 07:54:44
...
/* * 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); } }
上一篇: 面试试题001-截取字符串
下一篇: 面试试题002--数字母的个数