Summary: This section shows the basic method of learning a new algorithm by the example of binary search, and studies the principle, use case, necessity (simulating actual situation) and performance of the new algorithm.
Focus:
1. Two points to find:
Importjava.util.Arrays; Public classBinarySearch { Public Static intRankintKeyint[] a) {intLo = 0; inthi = a.length-1; while(Lo <=hi) { //the key being found either does not exist or must exist in the A[lo. Hi] intMid = lo + (Hi-lo)/2; if(Key <A[mid]) Hi= Mid-1; Else if(Key >A[mid]) Lo= Mid + 1; Else returnmid; } return-1; } Public Static voidMain (string[] args) {int[] A = {1, 3, 5, 7, 9, 11}; intKey = 5; System.out.println (rank (key, a)); Key= 8; System.out.println (rank (key, a)); }}
2. It is not possible to solve large-scale white list problems without efficient algorithms such as binary search or merge sorting.
Algorithm (4th edition)-1.1.102 Points Find