The binary method of searching
Dichotomy search (binary search), also known as binary retrieval, the basic idea of binary retrieval is that the elements in the dictionary are stored in an array in an orderly way from small to large,
First, the given value key is compared with the key code (key) of the element in the middle of the dictionary, and if it is equal, the retrieval succeeds;
Otherwise, if the key is small, then the binary method is searched in the first half of the dictionary;
If key is large, the binary search is continued in the second half of the dictionary.
In this way, half of the search interval is reduced by one comparison, so it goes on until the search succeeds or the retrieval fails.
An even number takes the middle 2 of either of them as intermediate elements
Binary retrieval is an efficient retrieval method, which requires the dictionary to be sorted by key code in the sequential table.
Note: Refer to Baidu encyclopedia http://baike.baidu.com/link?url= Txujyud9fggzlbhkfloymtefemdwwpvmg9owjf4rueuss6mstqvtw1jhmjdmbmoqauef6inggpwhx704af188_
Look at the code
public class arraystructures {private int[] thearray = new int[50]; Private int arraysize = 10;public void generaterandomarray () {for (int i =0; i< arraysize;i++) {thearray[i] = (int) (Math.random () *10 + 10);}} Public void printarray () {Stringbuffer sb = new stringbuffer ("-");for (int i = 0; i<arraysize; i++) {sb.append ("-----");} String septalline= sb.tostring (); System.out.println (Septalline);for (int i = 0; i<arraysize; i++) { System.out.print ("| " + i + " ");} System.out.println ("|"); System.out.println (Septalline);for (int i = 0; i<arraysize; i++) { System.out.print ("| " + theArray[i] + " ");} System.out.println ("|"); System.out.println (septalline);} Public void insertionSort () {for (int i=1; i<arraysize; i++) {int j= i;int insertvalue = thearray[i];while (j>0 && thearray[j-1]>insertvalue) {theArray[j]= theArray[ j-1];j--;} Thearray[j]=insertvalue;}} Public void binarysearchforvalue (int value) {Int lowindex = 0;int highindex = arraysize -1;while (Lowindex<= highindex) {int middleindex = (lowIndex +highindex)/2;if (thearray[middleindex] < value) lowIndex = middleIndex + 1;else if (Thearray[middleindex] > value) highIndex = middleIndex - 1;else {system.out.println ("the value " + value + " at Index " + middleindex); return;}} System.out.println ("the value " + value + " is not in this Array "); Public static void main (StrinG[] args) {// generate an array system.out.println ("Create an array, and fill in random value "); Arraystructures as = new arraystructures ();// set random value in array as.generaterandomarray ();// print original array as.printarray (); System.out.println (); System.out.println ("Insertion sort"); As.insertionsort (); As.printarray (); System.out.println (); System.out.println ("Binary search"); As.binarysearchforvalue (14); System.out.println ();}}
Output Results
create an array, and fill in random Value---------------------------------------------------| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |---------------------------------------------------| 11 | 12 | 13 | 14 | 18 | 14 | 19 | 11 | 17 | 19 |---------------------------------------------------insertion Sort---------------------------------------------------| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |---------------------------------------------------| 11 | 11 | 12 | 13 | 14 | 14 | 17 | 18 | 19 | 19 |--------------------------------------------------- Binary searchthe value 14 at index 4
This article is from the "10314466" blog, please be sure to keep this source http://10324466.blog.51cto.com/10314466/1659522
Data structures and algorithms-005 array sorting binary search