Algorithm: a binary search

com.atguigu Package; 

public class the Main {
public static void main (String [] args) {
// binary search
@ premise: the array must be ordered by looking
int [] arr = new int [ ] {- 98, - } 34,2,34,54,66,79,105,210,333;

int dest = -3;
int head = 0; // first initial index
int = End-arr.length. 1;

Boolean = isFlag to true;
the while (head <= End ) {

int = Middle (End head +) / 2;

IF (dest == ARR [Middle]) {
System.out.println ( "find the specified element position" Middle +);
isFlag = to false;
BREAK;
} IF the else (ARR [Middle]> dest) {
End = Middle-. 1;
} the else {
Middle = +. 1 head;
}
}
IF (isFlag) {
System.out.println ( "Unfortunately, not found");
}

}
}

Guess you like

Origin www.cnblogs.com/helloworld0903/p/11456552.html