在 Java 中,binarySearch() 是一種幫助使用二分搜尋演算法從多個元素中搜尋特定鍵元素的方法。為了執行此操作,元素必須按升序排序。如果沒有排序,可以使用Arrays.sort(arr)方法進行排序。否則,結果被認為是未定義的。 與線性搜尋相比,二分搜尋被認為更快。因此,二分查找的時間複雜度為 O(log n)。此外,binarySearch() 方法可以從 java.util.Arrays 套件中實例化。有關 binarySearch() 方法的更多詳細資訊將在以下部分中討論。
文法:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
public static int binarySearch(Object[] a, Object key)
這裡參數a和key分別是要找的陣列和要找的值。
binarySearch() 方法傳回正在搜尋的關鍵元素的索引。在未找到關鍵元素的情況下,將傳回原本要插入的關鍵元素的插入點。如果搜尋的關鍵元素與陣列中的其他元素不可比較,則會拋出稱為 ClassCastException 的例外。
BinarySearch() 方法在 Java 中如何運作?
讓我們看看這個方法在 Java 中是如何運作的:
- 假設k是要搜尋的關鍵元素。將 k 與陣列的中間元素進行比較。
- 如果 k 與中間位置的元素匹配,則必須傳回中間索引。
- 否則,如果k比中間位置的元素高,那麼k只能在中間元素的右邊找到。
- 否則可以在中間元素的左邊找到。
在 Java 中實作 BinarySearch() 的範例
以下是一些使用 BinarySearch() 方法的程式範例。
範例#1
代碼:
import java.util.Arrays; public class BinarySearchExample { public static void main(String[] args) { //create a byte array byte ba[] = {05, 10, 15, 20, 25, 30}; //create a character array char ca[] = {'a', 'n', 's', 'p', 'v', 'i', 'd'}; //create an integer array int ia[] = { 10, 20, 15, 22, 35}; //create a double array double da[] = {10.1 , 15.34 , 22.25, 13.5}; //create a float array float fa[] = {13.2f, 25.1f , 22.2f , 43.5f }; //sort all the arrays that created above Arrays.sort(ba); Arrays.sort(ca); Arrays.sort(ia); Arrays.sort(da); Arrays.sort(fa); //enter the key elements that has to be searched in the array byte bKey = 15; char cKey = 'i'; int iKey = 22; double dKey = 15.34; float fKey = 22.2f; System.out.println("Element "+ bKey + " is found at the position of " + Arrays.binarySearch(ba,bKey) ); System.out.println("Element "+ cKey + " is found at the position of " + Arrays.binarySearch(ca,cKey) ); System.out.println("Element "+ iKey + " is found at the position of " + Arrays.binarySearch(ia,iKey) ); System.out.println("Element "+ dKey + " is found at the position of " + Arrays.binarySearch(da,dKey) ); System.out.println("Element "+ fKey + " is found at the position of " + Arrays.binarySearch(fa,fKey) ); } }
輸出:
上述程式中使用 Arrays 對陣列進行排序後,建立了字元、整數、浮點、雙精確度和位元組等不同類型的陣列。 Sort() 方法中宣告了需要在陣列中搜尋的元素。然後使用 Arrays.binarySearch() 方法列印搜尋到的元素的索引。
假設給定一個陣列中不存在的關鍵元素;輸出是什麼?
為了找到這一點,讓我們更改關鍵元素的程式碼,如下所示。
位元組 bKey = 15;
char cKey = ‘i’;
int iKey = 89;
雙 dKey = 15.34;
浮動 fKey = 22.2f;
也就是說,iKey=89 不存在於陣列中,那麼輸出將顯示如下。
我們可以看到,位置列印為-6。這是因為,如果搜尋某個元素但未找到,則如果該元素存在,則將傳回索引的負值。即,int ia[] = { 10, 20, 15, 22, 35} 是給定的陣列。如果存在 89,則數組將為 int ia[] = { 10, 20, 15, 22, 35, 89};
可以清楚地看到索引將為 6。由於原始數組中不存在該索引,因此在上面的輸出中傳回了該特定索引的負值。
範例#2
二分查找也可以藉助遞歸來完成,如下圖所示。
代碼:
//sample class class BinarySearchExample{ public static int binarySearch(int a[], int f, int l, int k){ //if last element is greater than or equal to first element if (l>=f) { //find the mid int m = f + (l - f)/2; //if the key element that is searching is found in middle position, return mid position if (a[m] == k) { return m; } //if key element is less than element in middle position, search the left <u>subarray</u> if (a[m] > k){ return binarySearch(a, f, m-1, k); } //if key element is greater than the element in middle position, search the right <u>subarray</u> else{ return binarySearch(a, m+1, l, k); } } return -1; } public static void main(String args[]){ //initialise the array int a[] = {34,45,54,68,79}; int k = 68; int l = a.length-1; //store the position in variable res int res = binarySearch(a,0,l,k); if (res == -1) System.out.println("Sorry!! Can't find the element....!"); else System.out.println("Element is found at the position: "+res); } }
輸出:
在上面的程式中,首先建立了一個數組,並聲明了要尋找的元素。使用binarySearch()方法,可以找出關鍵元素的位置。 假設沒有找到該元素,則會列印一則訊息「Sorry !!!Can't find the element」。
結論
binarySearch() 是一種 Java 方法,可協助使用二分搜尋演算法在陣列中的多個可用元素中尋找特定的關鍵元素。本文檔詳細解釋了此方法的工作原理和範例。
推薦文章
這是 Java 中 BinarySearch() 的指南。在這裡,我們討論 BinarySearch() 方法在 Java 中的工作原理以及程式碼實作的範例。您也可以閱讀我們其他推薦的文章以了解更多資訊 –
- JavaScript 數學函數
- Java 中的版面配置
- Java 編譯器
- Java 中的合併排序
以上是Java 中的二分查找()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)