Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of Javabinarysearch method?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the principle of Javabinarysearch method? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

This article mainly introduces the detailed explanation of the principle of Java binarysearch method, which is introduced in great detail through the sample code, which has a certain reference value for everyone's study or work. Friends who need it can refer to it.

First, array sorting requires the import java.util.Arrays class

Binarysearch has two uses, one is to search in the entire array, the other is to search in a specified range, in fact, they are all the same, and the former can be regarded as a special case of the latter.

Usage 1

BinarySearch (Object [], Object key)

Object is the target array, and key is the target value, which requires that the target array must be sorted, otherwise the correct result cannot be found.

A query is nothing more than two results, one is that the target value is in the target array, and the other is not in the

Therefore, the return value is also divided into positive and negative, and if the target value is in the target array, the subscript is returned (if there are multiple identical ones, it is impossible to determine which one is found, because it is a binary search)

If the target value is not in the array, return-(subscript + 1 of the first element greater than the target value). Similarly, if all values in the array are smaller than the target value, return-(array length + 1)

Example:

Import java.util.Arrays;public class number {public static void main (String [] args) {int a [] = new int [] {1,3,4,6,8,9}; int x1 = Arrays.binarySearch (a, 5); int x2 = Arrays.binarySearch (a, 4); int x3 = Arrays.binarySearch (a, 0); int x4 = Arrays.binarySearch (a, 10); System.out.println (x1 + "+ x2 +" + x3 + "+ x4);}

Output:

-4 2-1-7

It can also be understood this way:

Output corresponding to the corresponding position.

Usage 2

BinarySearch (Object [], int fromIndex, int toIndex, Object key)

Two int variables are added to represent the beginning and end of the interval. Search in the interval from fromindex (inclusive) to toindex (not included). The return value is similar to the previous usage 1, except that-(toindex+1) is returned if all the values in the interval are smaller than the target value. If the value in the interval is larger than the target value, return-(fromindex+1). It is not difficult to understand if you think about it. Usage 1 is equivalent to usage 2 where fromindex is 0 and toindex is the length of the array.

The answer to the question about the principle of the Javabinarysearch method is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report