Arrays.binarySearch(二分法检索)
来源:互联网 发布:茄子换机软件 编辑:程序博客网 时间:2024/05/20 17:39
Arrays.binarySearch用法小析
二分法检索(binary search)又称折半检索,二分法检索的基本思想是设字典中的元素从小到大有序地存放在数组(array)中
binarySearchs方法的声明如下所示
public static int binarySerach(Xxx a[],Xxx key)
java.util.Arrays.binarySearch(int[], int, int, int)
java.util.Arrays.binarySearch(double[], int, int, double)
java.util.Arrays.binarySearch(long[], long)
java.util.Arrays.binarySearch(float[], int, int, float)
java.util.Arrays.binarySearch(int[], int)
java.util.Arrays.binarySearch(byte[], int, int, byte)
java.util.Arrays.binarySearch(byte[], byte)
java.util.Arrays.binarySearch(long[], int, int, long)
java.util.Arrays.binarySearch(short[], int, int, short)
java.util.Arrays.binarySearch(char[], char)
java.util.Arrays.binarySearch(short[], short)
java.util.Arrays.binarySearch(char[], int, int, char)
java.util.Arrays.binarySearch(Object[], int, int, Object)
java.util.Arrays.binarySearch(float[], float)
java.util.Arrays.binarySearch(double[], double)
java.util.Arrays.binarySearch(Object[], Object)
API文档
Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the sort(int[]) method) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.
Parameters:
a - the array to be searched
key - the value to be searched forReturns:
index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.使用二分搜索法来搜索指定的 Xxx型数组,以获得指定的值。必须在进行此调用之前对数组进行排序(通过 sort(Xxx[]) 方法)。如果没有对数组进行排序,则结果是不确定的。如果数组包含多个带有指定值的元素,则无法保证找到的是哪一个。
- 参数:
a - 要搜索的数组
key - 要搜索的值 - 返回:
如果它包含在数组中,则返回搜索键的索引;否则返回 (-(插入点) - 1)。插入点 被定义为将键插入数组的那一点:即第一个大于此键的元素索引,如果数组中的所有元素都小于指定的键,则为 a.length。注意,这保证了当且仅当此键被找到时,返回的值将 >= 0。
import java.util.Arrays;public class test { public static void main(String[] args){ int[]a={1,2,3,4,6,8,52,3}; Arrays.sort(a); for(int i=0;i<8;i++){ System.out.print(a[i]+" "); } System.out.println(); int x1 =Arrays.binarySearch(a, 7); int x2 =Arrays.binarySearch(a, 7); System.out.print("x1="+x1+" "); System.out.println("x2="+x2); }}
运行结果为x1=4 为位置下标
x2=-6 为 (-(插入点) - 1)
- 插入点:被定义为将键插入数组的那一点:即第一 个大于此键的
元素索引
Arrays.sort(int[] a)
Sorts the specified array into ascending numerical order.
Implementation note: The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.Parameters:
a - the array to be sorted
- Arrays.binarySearch(二分法检索)
- Arrays.binarySearch()
- Arrays.binarySearch();
- Arrays.binarySearch
- 使用二分法Arrays.binarySearch()与list.contains()进行元素比较的效率分析
- 使用二分法Arrays.binarySearch()与list.contains()进行元素比较的效率分析
- 使用二分法Arrays.binarySearch()与list.contains()进行元素比较的效率分析
- 定义二分法查找数组中的数,实现arrays.binarySearch()int类型的功能。
- BinarySearch 二分法查询
- 二分法查找(BinarySearch)
- 二分法查找. BinarySearch
- BinarySearch二分法查找
- JAVA Arrays.binarySearch
- Arrays.binarySearch用法
- JAVA Arrays.binarySearch
- JAVA Arrays.binarySearch
- 二分检索 BinarySearch
- 你了解Arrays.binarySearch()方法的返回值吗?
- hdu2768 Cat vs. Dog--最大匹配数
- java基础之集合
- POJ 2503 动态建树
- android逆向6
- 关于JVM与DVM,这些你可能不知道
- Arrays.binarySearch(二分法检索)
- 棋子翻转---- 美团2016研发工程师在线编程题
- ERROR: must be superuser to COPY to or from a file
- app审核被拒
- golang 通过gin框架获取未知的参数
- android逆向7
- 用Parcelable接口传递Bitmap
- HDFS inotify:Linux inotify机制在HDFS中的实现
- arm应用程序之文件读写操作差异open与fopen