数组。Java中的binarySearch()和示例|集1

数组。二进制搜索() 方法使用二进制搜索算法在给定数据类型的指定数组中搜索指定值。数组必须按 数组。排序() 方法,然后再进行此调用。如果未排序,则结果未定义。如果数组包含多个具有指定值的元素,则无法保证会找到哪个元素。让我们按如下方式浏览下面提供的插图。

null

插图:

Searching for 35 in byteArr[] = {10,20,15,22,35}will give result as 4 as it is the index of 35Searching for g in charArr[] = {'g','p','q','c','i'}will give result as 0 as it is the index of 'g'Searching for 22 in intArr[] = {10,20,15,22,35};will give result as 3 as it is the index of 22Searching for 1.5 in doubleArr[] = {10.2,15.1,2.2,3.5}will give result as -1 as it is the insertion point of 1.5Searching for 35.0 in floatArr[] = {10.2f,15.1f,2.2f,3.5f}will give result as -5 as it is the insertion point of 35.0Searching for 5 in shortArr[] = {10,20,15,22,35}will give result as -1 as it is the insertion point of 5

在Java中,在排序数组中查找元素是最简单、最有效的方法

语法:

public static int binarySearch(data_type arr, data_type key)

记得: 这里的数据类型可以是任何基本数据类型,比如字节、字符、双精度、整数、浮点、短、长,甚至对象。

参数:

  • 要搜索的数组
  • 要搜索的值

返回类型: 搜索键的索引(如果它包含在数组中);否则,((插入点)–1)。插入点定义为将键插入数组的点:第一个元素的索引大于键,或者如果数组中的所有元素都小于指定的键,则为a.length。注意,这保证了当且仅当找到键时,返回值将>=0。

需要记住以下几点:

  • 如果输入列表未排序,则结果未定义。
  • 如果有重复的,不能保证会找到哪一个。

如上所述,我们已经讨论过,我们也可以操作这个算法 数组。binarysearch()vs 收藏。二进制搜索() . 数组。binarysearch()也适用于原始数据类型的数组。 收藏 .binarysearch()适用于对象集合,如 ArrayList 链表 .

例1:

JAVA

// Java program to demonstrate working of Arrays.
// binarySearch() in a sorted array
// Importing Arrays class from
// java.util package
import java.util.Arrays;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing byte arrays
// to search over them
byte byteArr[] = { 10 , 20 , 15 , 22 , 35 };
char charArr[] = { 'g' , 'p' , 'q' , 'c' , 'i' };
int intArr[] = { 10 , 20 , 15 , 22 , 35 };
double doubleArr[] = { 10.2 , 15.1 , 2.2 , 3.5 };
float floatArr[] = { 10 .2f, 15 .1f, 2 .2f, 3 .5f };
short shortArr[] = { 10 , 20 , 15 , 22 , 35 };
// Using sort() method of Arrays class
// and passing arrays to be sorted as in arguments
Arrays.sort(byteArr);
Arrays.sort(charArr);
Arrays.sort(intArr);
Arrays.sort(doubleArr);
Arrays.sort(floatArr);
Arrays.sort(shortArr);
// Primitive datatypes
byte byteKey = 35 ;
char charKey = 'g' ;
int intKey = 22 ;
double doubleKey = 1.5 ;
float floatKey = 35 ;
short shortKey = 5 ;
// Now in sorted array array we will fetch and
// return elements/indiciesaccessing indexes to show
// array is really sorted
// Print commands where we are implementing
System.out.println(
byteKey + " found at index = "
+ Arrays.binarySearch(byteArr, byteKey));
System.out.println(
charKey + " found at index = "
+ Arrays.binarySearch(charArr, charKey));
System.out.println(
intKey + " found at index = "
+ Arrays.binarySearch(intArr, intKey));
System.out.println(
doubleKey + " found at index = "
+ Arrays.binarySearch(doubleArr, doubleKey));
System.out.println(
floatKey + " found at index = "
+ Arrays.binarySearch(floatArr, floatKey));
System.out.println(
shortKey + " found at index = "
+ Arrays.binarySearch(shortArr, shortKey));
}
}


输出

35 found at index = 4g found at index = 122 found at index = 31.5 found at index = -135.0 found at index = -55 found at index = -1

这种方法有多种变体,我们还可以指定要搜索的数组范围。我们将在以后的文章中讨论这一点,以及在对象数组中搜索。

例2:

JAVA

// Java Program to Illustrate binarySearch() method
//  of  Collections class
// Importing required classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating empty List
List<Integer> al = new ArrayList<Integer>();
// Adding elements to the List
al.add( 12 );
al.add( 53 );
al.add( 23 );
al.add( 46 );
al.add( 54 );
// Using binarySearch() method of Collections class
// over random inserted element and storing the
// index
int index = Collections.binarySearch(al, 23 );
// Print and display the index
System.out.print(index);
}
}


输出

2

本文由 猎捕 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享