Binary search algorithm

Source: Internet
Author: User

The binary search algorithm is a more frequent one in an ordered array.  If you do not use a binary algorithm to traverse an array directly, compare each element with a time complexity of O (n). The binary lookup algorithm is better, however, because the time complexity of the lookup is O (LGN), such as the array {1,2,3,4,5,6,7,8,9}. You need to find the element, and the binary lookup algorithm executes it in the order: First step: Find the middle element, which is 5. Because of 5<6, 6 must be in the array element after 5, then the {6,7,8,9} lookup; second step: Find the median of {6,7,8,9}, and select 7.  Because of 7>6, 6 should be in the 7 left array element, then only 6, that is found. The two-point lookup method is to keep the array in half-split, comparing the intermediate elements with the target values at each time. Case One:
package Cn.imooc.java2;public class Test02 {//defines a static method, static int Binarysera         CH (int[] array, int key) {//define the leftmost int of the array subscript left = 0;         Defines the right side of the array subscript (the subscripts are all starting from 0, so you need-1) int. = Array.length-1;             while (left <= right) {int mid = (left + right)/2;             Determine if the middle value is the target value if (array[mid] = = key) {return mid;             } else if (Array[mid] < key) {left = mid + 1;             } else {right = Mid-1;     }} return-1;          }//test with a target value of 7 public static void Main (string[] args) {Test02 s = new Test02 ();                Int[] b={1,5,6,7,8,9,10,12};     System.out.println (S.binaryserach (b, 7)); }}
Note: Each time you move the left and right pointers, you need +1 or 1 on the base of the mid to prevent a dead loop and the program will run correctly. and the judgment condition in the code must be while (left <= right), otherwise the judgment condition is incomplete, for example: array[3] = {1, 3, 5}; The key to be found is 5, which is not found under (Low < high). Because low and high are equal, point to element 5, but at this point the condition is not established and does not enter the while (). case Two: Find the first equal element
Package Cn.imooc.java2;public class Test02 {     //Find first equal element     static int findfirstequal (int[] array, int key) {         int left = 0;         int right = Array.length-1;         while (left <= right) {             int mid = (left + right)/2;             if (Array[mid] >= key) {Right                 = mid-1;             }             else {left                 = mid + 1;             }         }         if (left < Array.Length && Array[left] = = key) {             return left;         }                 return-1;     }     public static void Main (string[] args) {          Test02 s = new Test02 ();          Int[] b={1,5,6,7,7,7,7,8,9,10,12};                   System.out.println (S.findfirstequal (b, 7));}     }

  

case Three: Finding the last equal element
Package Cn.imooc.java2;public class Test02 {     static int findlastequal (int[] array, int. key) {         int left = 0;         int right = Array.length-1;         while (left <= right) {             int mid = (left + right)/2;             if (Array[mid] <= key) {left                 = mid + 1;             }             else {Right                 = mid-1;             }         }         if (right >= 0 && array[right] = = key) {             return right;         }         return-1;     }     public static void Main (string[] args) {          Test02 s = new Test02 ();          Int[] b={1,5,6,7,7,7,7,8,9,10,12};                   System.out.println (S.findlastequal (b, 7));}     }

  

case FOUR: Find the last element that is equal to or less than key. That is, there are several elements that are equal to finding the key value, returning the rightmost element subscript, or, if there is no element equal to the key value, the rightmost element below the key is returned.
Package cn.imooc.java2; Public classTest02 {Static intFindlastequalsmaller (int[] Array,intkey) {         intleft =0; intright = Array.Length-1;  while(Left <=Right ) {             intMid = (left + right)/2; if(Array[mid] >key) { Right= Mid-1; }             Else{ Left= Mid +1; }         }         returnRight ; }      Public Static voidMain (string[] args) {Test02 s=NewTest02 (); int[] b={1,5,6,7,7,7,7,8,9,Ten, A}; System. out. println (S.findlastequalsmaller (b, One)); }}

Case Five: Find the first element that is equal to or greater than key. That is, there are several elements that are equal to finding the key value, returning the leftmost element subscript of those elements, or, if there is no element equal to the key value, the leftmost element that is greater than key is subscript.
Package Cn.imooc.java2;public class Test02 {     static int findfirstequallarger (int[] array, int. key) {         int left = 0;         int right = array.length-1;         while (left <= right) {             int mid = (left + right)/2;             if (Array[mid] >= key) {Right                 = mid-1;             }             else {left                 = mid + 1;             }         }         return left;     }     public static void Main (string[] args) {          Test02 s = new Test02 ();          Int[] b={1,5,6,7,7,7,7,8,9,10,12};                   System.out.println (S.findfirstequallarger (b, one));}     }

  

Case SIX: Find the first element greater than key, that is, return the leftmost element greater than key subscript.
Package Cn.imooc.java2;public class Test02 {     static int findfirstlarger (int[] array, int. key) {         int left = 0;         int right = Array.length-1;         while (left <= right) {             int mid = (left + right)/2;             if (Array[mid] > key) {right                 = mid-1;             }             else {left                 = mid + 1;             }         }         return left;     }     public static void Main (string[] args) {          Test02 s = new Test02 ();          Int[] b={1,5,6,7,7,7,7,8,9,10,12};                   System.out.println (S.findfirstlarger (b, one));}     }

  

Binary search algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.