Get the App
SLTechnology News&Howtos  ›  Development  › 

How to search for AJPFX by dichotomy

Shulou Source: shulou.com Published: 2022-06-02 03:15:39 10月03日 Update

How to carry out AJPFX dichotomy search, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Package com.heima.array

Public class Demo2_Array {

/ * *

* * A: case demonstration

* Array advanced binary search code

* B: notes

* binary lookup cannot be used if the array is unordered.

* because if you sort, but when you sort, you have changed my original element index.

, /

Public static void main (String [] args) {

Int [] arr = {11, 22, 33, 44, 55, 55, 66, 77}

System.out.println (getIndex (arr, 22))

System.out.println (getIndex (arr, 66))

System.out.println (getIndex (arr, 88))

}

/ *

* binary search

* 1. Return value type, int

* 2, parameter list int [] arr,int value

, /

Public static int getIndex (int [] arr, int value) {

Int min = 0

Int max = arr.length-1

Int mid = (min + max) / 2

While (ARR [mid]! = value) {/ / when the intermediate value is not equal to the value you are looking for, start a circular search

If (arr [mid]

< value) { //当中间值小于了要找的值 min = mid + 1; //最小的索引改变 }else if (arr[mid] >

Value) {/ / when the intermediate value is greater than the value you are looking for

Max = mid-1; / / maximum index change

}

Mid = (min + max) / 2; / / regardless of the maximum or minimum change, the intermediate index will change accordingly

If (min > max) {/ / if the minimum index is greater than the maximum index, there is no possibility of finding it

Return-1; / / return-1

}

}

Return mid

}

}

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Index maximum minimum array help sort dichotomy original clear advanced item code element content parameter possibility for this article novice time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology Shulou Tech Info MariaDB Xiaomi