Binary search is also called half search. The basic idea of binary search is to assume that the elements in the dictionary are stored in an array (array) in an orderly manner from small to large.
First, the given value key and the middle value of the dictionary are Compare the keys of the elements at the position. If they are equal, the retrieval is successful;
Otherwise, if the key is small, continue the binary search in the first half of the dictionary;
If the key is large, then search in the second half of the dictionary Continue the binary search in .
In this way, the search interval is reduced by half after one comparison, and this continues until the search is successful or fails.
Take any one of the middle 2 elements from an even number as the middle element
Binary retrieval is a more efficient retrieval method, which requires the dictionary to be sorted by key in the sequence table.
java code
Returns to the location successfully, returns a negative number on failure
package ForTest; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Arithmetic{ public static<T extends Comparable<? super T> > int BinarySearch(List<T> array, int start, int end, T key) { int low; int high; int guess; if(array == null || start>end || start > array.size()-1 || end < 0) { return -1; } start = start < 0 ? 0 : start; low = start-1; end = end > array.size()-1 ? array.size()-1 : end; high = end+1; while (high - low > 1) { guess = ((high - low)>>1) + low; if (array.get(guess).compareTo(key) < 0) low = guess; else high = guess; } if (high == end +1 ) { return ~(end +1 ); } else if (array.get(high).compareTo(key) == 0) { return high; } else { return ~high; } } public static<T extends Comparable<? super T> > int BinarySearch(T[] array, int start, int end, T key) { List<T> stooges = Arrays.asList(array); return Arithmetic.BinarySearch(stooges, start, end, key); } public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Integer> a = new ArrayList<Integer>(); Float[] b = new Float[100]; for(int i=0; i<100; i++) { a.add(i); b[i] = (float) i; } System.out.println(""+Arithmetic.BinarySearch(a,0,1000,200)); System.out.println(""+Arithmetic.BinarySearch(b,0,100,2.f)); } }
For more articles related to the dichotomy of the algorithm, please pay attention to 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
