あなたがそれを改善することができるのであれば、私はJavaでコードを与えたと説明していません。
public class Test {
public static int binSearch(int array[], int key, int left, int right,boolean lb)
{
int mid = left + (right-left)/2;
if (key < array[mid])
return binSearch(array, key, left, mid-1,lb);
else if (key > array[mid])
return binSearch(array, key, mid+1, right,lb);
else if (key == array[mid]){
if(!lb){
if(key==array[mid+1]){
int ctr=mid+1;
while(key==array[++ctr]);
return ctr--;
}
else
return mid;
}
else{
if(key==array[mid-1]){
int ctr=mid-1;
while(key==array[--ctr]);
return ctr++;
}
else
return mid;
}
}
return -0; // Not Found
}
public static void main(String[] args) {
int a[]={1,2,4,4,5,8,12,15,15,23,54};
int start=binSearch(a, 4, 0, a.length,true);
int end=binSearch(a, 15, 0, a.length,false);
System.out.println(end-start+1);// number are include
}
}