⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 search.java

📁 二分查找的应用
💻 JAVA
字号:

import java.util.Arrays;
public class Search {

	
	 int comparisons=0;
	 private Search() {}
	 	 
	 public static Person binary(Person[ ] collection, String key) {
		 
	   if (collection != null) {
       int left =0;
       int right = collection.length-1;
        
       while ( (left <= right) ) {
          int mid = (left + right) /2;
          int c = key.compareTo(collection[mid].getFirstName());
           if (c<0) {
        	   right=mid-1;
                 }
           else if (c>0){
             left = mid + 1;
          }
          else
          {
        	  return collection[mid];
          }
       }
    }

    return null;
 }

	
		 
	 public static Person sequential(Person[ ] collection, String key){
	      if (collection!= null) {
	 	         for (int i = 0; i < collection.length; i++) {
	              if (collection[i].getFirstName()== key) {
	               // found the key
	               return collection[i];  
	            }
	         }
	      }

	      return null;
	   }
	 
	  
	   // return the number of comparisons made in most recent search
	   public int getComparisons() {
	      return comparisons;
	   }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -