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

How to use Java binary search

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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

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