Copy codeThe Code is as follows:
/**
* Half-lookup character position in the array (ordered list)
* @ Param array the retrieved array
* @ Param x the character to be searched
* @ Type int
* @ Returns: the position of the character in the array.-1 is not found.
*/
Function binarySearch (array, x ){
Var lowPoint = 1;
Var higPoint = array. length;
Var returnValue =-1;
Var midPoint;
Var found = false;
While (lowPoint <= higPoint )&&(! Found )){
MidPoint = Math. ceil (lowPoint + higPoint)/2 );
// Console. log (lowPoint + "=" + midPoint + "=" + higPoint );
If (x> array [midPoint-1]) {
LowPoint = midPoint + 1;
}
Else if (x <array [midPoint-1]) {
HigPoint = midPoint-1;
}
Else if (x = array [midPoint-1]) {
Found = true;
}
}
If (found ){
ReturnValue = midPoint;
}
Return returnValue;
}
/* Var array2 = [9,100,109,]; */
Var array2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
Console. log (binarySearch (array2, 'C '));