Java實現(xiàn)二進(jìn)制搜索的方法有哪些?這個問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!
成都創(chuàng)新互聯(lián)公司擁有十年成都網(wǎng)站建設(shè)工作經(jīng)驗,為各大企業(yè)提供成都網(wǎng)站建設(shè)、做網(wǎng)站服務(wù),對于網(wǎng)頁設(shè)計、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、成都app開發(fā)、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、域名注冊等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)絡(luò)營銷經(jīng)驗,集策劃、開發(fā)、設(shè)計、營銷、管理等網(wǎng)站化運(yùn)作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項目的能力。
在Java中有兩種方法可以進(jìn)行二進(jìn)制搜索。

1.Arrays.binarysearch()適用于也可以是原始數(shù)據(jù)類型的數(shù)組。
import java.util.Arrays;
public class GFG {
public static void main(String[] args)
{
int arr[] = { 10, 20, 15, 22, 35 };
Arrays.sort(arr);
int key = 22;
int res = Arrays.binarySearch(arr, key);
if (res >= 0)
System.out.println(key + " found at index = "
+ res);
else
System.out.println(key + " Not found");
key = 40;
res = Arrays.binarySearch(arr, key);
if (res >= 0)
System.out.println(key + " found at index = "
+ res);
else
System.out.println(key + " Not found");
}
}輸出:
22 found at index = 3 40 Not found
2.Collections.binarysearch()適用于對象集合,如ArrayList和LinkedList。
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class GFG
{
public static void main(String[] args)
{
List<Integer> al = new ArrayList<Integer>();
al.add(1);
al.add(2);
al.add(3);
al.add(10);
al.add(20);
int key = 10;
int res = Collections.binarySearch(al, key);
if (res >= 0)
System.out.println(key + " found at index = "
+ res);
else
System.out.println(key + " Not found");
key = 15;
res = Collections.binarySearch(al, key);
if (res >= 0)
System.out.println(key + " found at index = "
+ res);
else
System.out.println(key + " Not found");
}
}輸出:
10 found at index = 3 15 Not found
如果輸入沒有排序怎么辦?
如果未對輸入列表進(jìn)行排序,則結(jié)果未定義。
如果有重復(fù)怎么辦?
如果有重復(fù),則無法保證找到哪一個。
Collections.binarySearch如何為LinkedList工作?
此方法在log(n)時間內(nèi)運(yùn)行,用于“隨機(jī)訪問”列表,如ArrayList。如果指定的列表沒有實現(xiàn)RandomAccess接口并且很大,則此方法將執(zhí)行基于迭代器的二進(jìn)制搜索,該搜索執(zhí)行O(n)鏈接遍歷和O(log n)元素比較。
兩個函數(shù)返回的負(fù)值的重要值是多少?
該函數(shù)返回搜索鍵的索引(如果它包含在數(shù)組中); 否則,( - (插入點) - 1)。插入點定義為鍵將插入到數(shù)組中的點:第一個元素的索引大于鍵,或者如果數(shù)組中的所有元素都小于指定鍵,則為a.length。請注意,當(dāng)且僅當(dāng)找到密鑰時,這可以保證返回值> = 0。
如何在Java中實現(xiàn)我們自己的二進(jìn)制搜索?
class BinarySearch
{
int binarySearch(int arr[], int l, int r, int x)
{
if (r>=l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = {2,3,4,10,40};
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr,0,n-1,x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " +
result);
}
}輸出:
Element found at index 3
感謝各位的閱讀!看完上述內(nèi)容,你們對Java實現(xiàn)二進(jìn)制搜索的方法有哪些大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前名稱:Java實現(xiàn)二進(jìn)制搜索的方法有哪些
分享網(wǎng)址:http://chinadenli.net/article36/pgpgpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、全網(wǎng)營銷推廣、品牌網(wǎng)站建設(shè)、微信公眾號、商城網(wǎng)站、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)