Binary Search, binary search
Binary Search, as its name implies, is a component search. When binary lookup is used, data must be sorted, which is the prerequisite for Binary lookup.
The basic implementation of this algorithm is as follows:
1. Sort data first
2. Determine the intermediate position mid = (first + last)/2;
3. compare the key of the keyword to be searched with the keyword of the intermediate position. returns if the key is equal to the mid value. if the key is large (in ascending order), the keyword to be searched is between mid and last; otherwise, it is between first and mid.
4. continue to search for intermediate elements in the above method until they are found.
1 import java. util. arrays; 2 public class ArraysDome_1 {3 public static void main (String [] args) {4 int array [] = {2, 31, 434,11, 323,44, 10}; 5 Arrays. sort (array); 6 System. out. println (Arrays. toString (array); // output the sorted Series 7 System. out. println ("Search Result:" + see (array, 11); // call the search method and output result 8} 9 private static int see (int [] arr, int I) {10 int mid = (arr. length-1)/2; // The first obtained mid11 while (mid <arr. length & mid> = 0) {// cycle condition: the mid search range cannot exceed the array range 12 if (arr [mid]> I) {// if the data to be pointed to is greater than the keyword, go backward. Otherwise, go forward until mid13 mid --; 14} else if (arr [mid] <I) is found and returned) {15 mid ++; 16} else if (arr [mid] = I) {17 return mid; 18} 19} 20 return-1; // if not,-121} 22 23} is returned}
This article from: http://www.cnblogs.com/zzsakurazz/ reproduced at will, please indicate the source
Binary Search
There are 13 in total, starting from 0, and the maximum value is 12. The analysis is based on the bipartite principle as follows:
First time: 12/2 = 6, (6) = 45 <90
Second time: (7 + 12)/2 = 9, (9) = 77 <90
Third time: (10 + 12)/2 = 11, (11) = 95> 90
Fourth: (11-1) + 10)/2 = 10, (10) = 90. The search is successful.
Binary Search
There are 13 in total, starting from 0, and the maximum value is 12. The analysis is based on the bipartite principle as follows:
First time: 12/2 = 6, (6) = 45 <90
Second time: (7 + 12)/2 = 9, (9) = 77 <90
Third time: (10 + 12)/2 = 11, (11) = 95> 90
Fourth: (11-1) + 10)/2 = 10, (10) = 90. The search is successful.