本文目錄一覽:
java 二分查找法
public class BinarySearch {
public static void main(String[] args) {
int[] a = { 2, 4, 6, 9 };
int key = 2;
BinarySearchM(a, key);
System.out.println(“The key is in ” + BinarySearchM(a, key));
}
public static int BinarySearchM(int[] list, int key) {
int low = 0;
int high = list.length – 1;
while (high = low) {
int mid = (low + high) / 2;
System.out.println(mid);
if (key list[mid])//mid 改為 list[mid]
high = mid – 1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}
return -low – 1;
}
}
用二分法查找(折半查找)java
二分查找也稱折半查找(Binary Search),它是一種效率較高的查找方法。但是,折半查找要求線性表必須採用順序存儲結構,而且表中元素按關鍵字有序排列。
二分查找優缺點
優點是比較次數少,查找速度快,平均性能好;
其缺點是要求待查表為有序表,且插入刪除困難。
因此,折半查找方法適用於不經常變動而查找頻繁的有序列表。
使用條件:查找序列是順序結構,有序。
過程
首先,假設表中元素是按升序排列,將表中間位置記錄的關鍵字與查找關鍵字比較,如果兩者相等,則查找成功;否則利用中間位置記錄將表分成前、後兩個子表,如果中間位置記錄的關鍵字大於查找關鍵字,則進一步查找前一子表,否則進一步查找後一子表。重複以上過程,直到找到滿足條件的記錄,使查找成功,或直到子表不存在為止,此時查找不成功。
利用循環的方式實現二分法查找
public class BinarySearch {
public static void main(String[] args) {
// 生成一個隨機數組 int[] array = suiji();
// 對隨機數組排序 Arrays.sort(array);
System.out.println(“產生的隨機數組為: ” + Arrays.toString(array));
System.out.println(“要進行查找的值: “);
Scanner input = new Scanner(System.in);
// 進行查找的目標值 int aim = input.nextInt();
// 使用二分法查找 int index = binarySearch(array, aim);
System.out.println(“查找的值的索引位置: ” + index);
}
/** * 生成一個隨機數組 *
* @return 返回值,返回一個隨機數組 */
private static int[] suiji() {
// random.nextInt(n)+m 返回m到m+n-1之間的隨機數 int n = new Random().nextInt(6) + 5;
int[] array = new int[n];
// 循環遍歷為數組賦值 for (int i = 0; i array.length; i++) {
array[i] = new Random().nextInt(100);
}
return array;
}
/** * 二分法查找 —循環的方式實現 *
* @param array 要查找的數組 * @param aim 要查找的值 * @return 返回值,成功返回索引,失敗返回-1 */
private static int binarySearch(int[] array, int aim) {
// 數組最小索引值 int left = 0;
// 數組最大索引值 int right = array.length – 1;
int mid;
while (left = right) {
mid = (left + right) / 2;
// 若查找數值比中間值小,則以整個查找範圍的前半部分作為新的查找範圍 if (aim array[mid]) {
right = mid – 1;
// 若查找數值比中間值大,則以整個查找範圍的後半部分作為新的查找範圍 } else if (aim array[mid]) {
left = mid + 1;
// 若查找數據與中間元素值正好相等,則放回中間元素值的索引 } else {
return mid;
}
}
return -1;
}}
運行結果演示:
由以上運行結果我們得知,如果要查找的數據在數組中存在,則輸出該數據在數組中的索引;如果不存在則輸出 -1 ,也就是打印 -1 則該數在數組中不存在,反之則存在。
四、利用遞歸的方式實現二分法查找
public class BinarySearch2 {
public static void main(String[] args) {
// 生成一個隨機數組 int[] array = suiji();
// 對隨機數組排序 Arrays.sort(array);
System.out.println(“產生的隨機數組為: ” + Arrays.toString(array));
System.out.println(“要進行查找的值: “);
Scanner input = new Scanner(System.in);
// 進行查找的目標值 int aim = input.nextInt();
// 使用二分法查找 int index = binarySearch(array, aim, 0, array.length – 1);
System.out.println(“查找的值的索引位置: ” + index);
}
/** * 生成一個隨機數組 * * @return 返回值,返回一個隨機數組 */
private static int[] suiji() {
// Random.nextInt(n)+m 返回m到m+n-1之間的隨機數 int n = new Random().nextInt(6) + 5;
int[] array = new int[n];
// 循環遍歷為數組賦值 for (int i = 0; i array.length; i++) {
array[i] = new Random().nextInt(100);
}
return array;
}
/** * 二分法查找 —遞歸的方式 * * @param array 要查找的數組 * @param aim 要查找的值 * @param left 左邊最小值 * @param right 右邊最大值 * @return 返回值,成功返回索引,失敗返回-1 */
private static int binarySearch(int[] array, int aim, int left, int right) {
if (aim array[left] || aim array[right]) {
return -1;
}
// 找中間值 int mid = (left + right) / 2;
if (array[mid] == aim) {
return mid;
} else if (array[mid] aim) {
//如果中間值大於要找的值則從左邊一半繼續遞歸 return binarySearch(array, aim, left, mid – 1);
} else {
//如果中間值小於要找的值則從右邊一半繼續遞歸 return binarySearch(array, aim, mid + 1, array.length-1);
}
}}
運行結果演示:
總結:
遞歸相較於循環,代碼比較簡潔,但是時間和空間消耗比較大,效率低。在實際的學習與工作中,根據情況選擇使用。通常我們如果使用循環實現代碼只要不是太繁瑣都選擇循環的方式實現~
java泛型 二分查找
以下代碼是關於對象的 二分查找 的例子,已經測試通過,執行即可。
Student 是基本比較對象類
Dichotomy 是二分法執行類
Test 是測試類
package com.dichotomy;
public class Student implements ComparableStudent {
private int id;
private String name;
private String idCard;
private String sex;
private String mobile;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
/**
* 排序控制
* @param o1 Student
* @param o2 Student
* @return int 返回 -1 向前移動, 1 向後移動, 0 不移動
* 這個方法需要自己進行調整,排序比較和二分查找時均使用此方法進行位置調整
* 比較時使用的key自己可以進行修改,不過要保證唯一性,否則查詢出來的值會不準確
*/
public int compareTo(Student o) {
//不同的執行次序決定排序和查找次序不同,可以同下面的調換一下
if(this.getId() o.getId()){
return -1;
} else if(this.getId() == o.getId()){
;
} else {
return 1;
}
//不同的執行次序決定排序和查找次序不同
int c = this.getIdCard().compareTo(o.getIdCard());
if(c != 0){
return c;
}
//不同的執行次序決定排序和查找次序不同
int n = this.getName().compareTo(o.getName());
if(n != 0){
return n;
}
return 0;
}
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append(this.getId()).append(“\t”);
sb.append(this.getName()).append(“\t”);
sb.append(this.getIdCard()).append(“\t”);
sb.append(this.getMobile()).append(“\t”);
sb.append(this.getSex());
return sb.toString();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/235934.html