zoukankan      html  css  js  c++  java
  • 二分查找递归和非递归版

     1 public class twosep {
     2 
     3     public static void main(String[] args) {
     4         twosep ts = new twosep();
     5         int arr[] = {1,2,3,6,8,9,11,23,28,56,59,61,62,63,66,68,70,78,79,91,92};
     6         ts.nofind(1, arr);
     7 
     8     }
     9     public void find(int num,int[] arr,int min,int max){
    10         int mid = (min + max)/2;
    11         if(num==arr[mid]){
    12             System.out.println("你查找的位置为"+mid);
    13         }else if(num>arr[mid]){
    14             min = mid + 1;
    15             find(num,arr,min,max);
    16         }else{
    17             max = mid - 1;
    18             find(num,arr,min,max);
    19         }
    20     }
    21     public void nofind(int num,int[] arr){
    22         int low=0;
    23         int high = arr.length;
    24         while(low<=high){
    25             int mid = (low+high)/2;
    26             if(num==arr[mid]){
    27                 System.out.println("你查找的值位置为"+mid);
    28                 break;
    29             }else if(num>arr[mid]){
    30                 low = mid+1;
    31             }else{
    32                 high = mid -1;
    33             }
    34         }
    35     }
    36 }
  • 相关阅读:
    百度之星初赛 A
    百度之星 初赛 BC
    2016 百度之星资格赛
    codeforces 749
    codeforces 785
    HDU 4617
    网络流 poj 2195
    网络流 poj 3436 poj 3281
    codeforces 780 C
    idea激活
  • 原文地址:https://www.cnblogs.com/LiSuSpAu/p/6000951.html
Copyright © 2011-2022 走看看