1. Explanation of the dichotomy method
The core idea of the dichotomy method is the movement of the index, and the search speed increases geometrically.
Binary search method, returns the index of the array element found, if not found, returns -1
2, Example
Binary search positioning parameters The position of the value in the array
Scene description:
Find its subscript range in the array based on a parameter value, for example: 2 is in the array {0, 1, 3, 5} The interval is {1, 2}
package com.study.collection; import java.util.Arrays; /** * @auth zhangmj * @date 2019/2/12 9:14 */ public class ExampleList<T> { public static void main(String[] args) { int[] intArray = {0,1,2,10,15,20,25,29,31,36,39,40,42,43,46,50,55,60,63,66,70}; int num =2; int[] resultArray = getPostionByTwoPoint(intArray, num); System.out.println(Arrays.toString(resultArray)); } private static int[] getPostionByTwoPoint(int[] intArray, int num) { // 判断 if(intArray == null || intArray.length == 0){ throw new RuntimeException("数组不能为空"); } // 定义最小和区间 if(intArray[0] > num || intArray[intArray.length - 1] < num){ throw new RuntimeException("不在数组范围之内"); } int middle = 0; int low = 0; int high = intArray.length - 1; // 定义首尾特殊的情况 if(intArray[low] == num){ int[] resultArray = {low, low}; return resultArray; }else if(intArray[high] == num){ int[] resultArray = {high, high}; return resultArray; } int i = 1; // 数在中间的情况 while(low < high){ System.out.println("查找第 " + i + " 次"); middle = (low + high + 1)/2; if(intArray[middle] == num){ int[] resultArray = {middle, middle}; return resultArray; }else if(intArray[middle] > num){ // num 在 low 和 middle 之间 int previous = middle - 1; if(previous > low && intArray[previous] < num){ int[] resultArray = {previous, middle}; return resultArray; } high = middle; }else if(intArray[middle] < num){ int latter = middle + 1; if(latter < high && intArray[latter] > num){ int[] resultArray = {middle, latter}; return resultArray; } low = middle; } i++; } throw new RuntimeException("定位异常"); } }
The above is the detailed content of How to use binary method to find the position of array elements in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft