Binary search
Classes in java.util * Import;.
public class HW3 {
public static void main (String [] args) {
Scanner in = new new Scanner (System.in); // Enter the number you want to find
System.out.println ( "Please enter data printed: ");
int = in.nextInt Number ();
the random new new RD = the random (); // random in an array, Note: the range is designated when printing
int [] arr = new int [ 11];
for (int i = 0; i <11; i ++) // number of the array 11, the value assigned starting
{
ARR [I] = rd.nextInt (50); 0-49 // 11 // this array assignment
}
Arrays.sort (ARR); // binary search is because the number of columns used in order, so the first number in the sorted array
System.out.println (Arrays.toString (arr)); // the ordered array Print out
int index = Search (arr, number ); // call the search () method (binary search) to find
if (index == - 1) { // if the return is -1, the number is not found
System.out.println ( "No number found that !!!");
} if returned to the else {// index , it indicates that the number found
System.out.println ( "find the number of labeled:" + index);
}
}
Private Search static int (int [] ARR, int number) {// array will be traversed and to find the value assigned to the method
int start = 0; // define three variables, to reduce the range
int arr.length = End -. 1;
int Middle = 0;
the while (End> = Start) {// End > = start, described arrays are not checked finished, then continue to look
Middle = (Start + End) / 2;
IF (Number <ARR [Middle]) {
End = Middle -. 1;
} the else IF (Number> ARR [Middle] ) {
Start = Middle +. 1;
The else {}
return Middle; // if the number found, then the index returns
}
}
return -1; // if end> start, also show an array traversed to find the number is not found, then return -1;
}
}
operation result:
These are the code binary search implementation, in fact, already have Java binary search function can be called directly, using the method as follows:
. 1 Import Classes in java.util *. ; 2 public class HW3 is { . 3 public static void main (String [] args) { . 4 Scanner in = new new Scanner (the System.in); // enter the number to find . 5 System.out.println ( "Please enter the data to be printed:" ); . 6 int Number = in.nextInt (); . 7 the random RD = new new the random (); // random in an array, NOTE: when printing range is indicated . 8 int [] = ARR new new int [. 11 ]; . 9 for( Int I = 0; I <11; I ++) // for the number of array 11, starting allocation value 10 { 11 ARR [I] = rd.nextInt (50); // 0-49 // these 11 assignment array 12 is } 13 is Arrays.sort (ARR); // because a binary search is ordered number of columns, the number of the first sort of array 14 System.out.println (of Arrays.toString (ARR)); // Print sorted array 15 int index = Arrays.binarySearch (ARR, Number); // call to search () method (binary search) to find 16 IF (index <-1) { // if the return is -1, I did not find the number 17 System.out.println ( "No number found that !!!" ); 18 is } the else { // If the index is returned, it indicates that the number found . 19 System.out.println ( "number lookup at index : "+ index); 20 is } 21 is } 22 is 23 is }
Arrays.binarySearch (arr, number) method underlying code:
(Note that the above code is different is that this method is called to find value, if not find the return is negative, - (insertion point) - 1)
Arrays.binarySearch (arr, formindex, endindex, number) method underlying code: