二分搜寻法(搜寻原则的代表)

如果搜寻的数列已经有排序,应该尽量利用它们已排序的特性,以减少搜寻比对的次数,这是搜寻的基本原则,二分搜寻法是这个基本原则的代表。

public class BinarySearch {   
 public static int search(int[] number, int des) {       
  int low = 0;        
  int upper = number.length - 1;        
  while(low <= upper) {            
   int mid = (low+upper) / 2;            
   if(number[mid] < des)                
    low = mid+1;            
   else if(number[mid] > des)                
    upper = mid - 1;            
   else                
   return mid;        
   }        
   return -1;    
  }       
   
  public static void main(String[] args) {       
   
  int[] number = {1, 4, 2, 6, 7, 3, 9, 8};               
   
  QuickSort.sort(number);               
   
  int find = BinarySearch.search(number, 3);               
   
  if(find != -1)            
   
     System.out.println("找到数值于索引" + find);        

                else            
                    System.out.println("找不到数值");    
   }
   
 }

你可能感兴趣的:(二分)