Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java binary search

Shulou Source: shulou.com Published: 2022-06-03 05:59:00 11月17日 Update

This article mainly introduces "how to use Java binary search". In daily operation, I believe many people have doubts about how to use Java binary search problems. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Java binary search"! Next, please follow the editor to study!

A brief introduction to binary search

Basic idea: it is also called half search, which requires that the sequence to be searched is ordered. It is a fast search algorithm with a time complexity of O (logn), and requires that the data set be an ordered data set.

Use

Application scenario: it is generally used to find array elements, and the array must be sorted (usually ascending) before searching.

Steps:

1. Compare the value of the middle position with the keyword to be checked. If the value of the intermediate position is larger than the keyword to be checked, cycle through the search process in the first half.

2. If the value of the middle position is smaller than the keyword to be checked, cycle through the search process in the second half.

3. Until it is found, there are no keywords to be checked in the sequence.

Code example:

Public static int biSearch (int [] array,int a) {int lo=0; int hi=array.length-1; int mid While (lostart) {/ / compare while from back to front (end > start&&a [end] > = key) / / if nothing is smaller than the key value, compare the next one until there is a swap position less than the key value, and then compare the end-- from the back to the back If (a [end] start&& a [start] = key) {int temp = a [start]; a [start] = a [end]; a [end] = temp;} / / at this point the first loop comparison is over and the location of the key value has been determined. The values on the left are smaller than the key values, and the values on the right are larger than the key values, but the order of the two sides may also be different. Make the following recursive call} / / recursive if (start > low) sort (aQuinLowPerry 1); / / left sequence. The first index position to the key value index-1 if (endtj,tk=1

2. Sort the sequence k times according to the number of incremental sequences k.

3. For each sorting trip, according to the corresponding incremental ti, the sequence to be arranged is divided into several sub-sequences of length m, and each sub-table is sorted directly. Only when the increment factor is 1, the whole sequence is treated as a table, and the table length is the length of the whole sequence.

Code example:

Private void shellSort (int [] a) {int dk = a.dk 2; while (dk > = 1) {ShellInsertSort (a, dk); dk = dk/2;}} private void ShellInsertSort (int [] a, int dk) {/ / similar insertion sort, except that the insertion sort increment is 1, where the increment is dk, and replace 1 with dk

Tags: Sequence sort algorithm array element order data application minimum example node scene code location idea step introduction generation situation index Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker NVidia Shulou Tech Info MySQL Shulou Information