I wrote the following code for binary search in java:
import java.util.Arrays;
class BinarySearch {
public static int binarySearch(double[] arr, double x, int high, int low) {
int mid=(high+low)/2;
if(high==low || low==mid || high==mid) {
return -1;
}
if(arr[mid]<x) {
return binarySearch(arr, x, high, mid);
}
else if(arr[mid]>x) {
return binarySearch(arr, x, mid, low);
}
else if(arr[mid]==x) {
return mid;
}
return -1;
}
public static void main(String args[]) {
int n=1000;
double array[] = new double[n];
for (int i=0; i<100; i++) {
for (int k = 0; k < n; k++) {
double r = Math.random();
r = r * 100;
r = Math.round(r);
r = r / 100;
array[k] = r;
}
Arrays.sort(array);
double search = Math.random();
search = search * 100;
search = Math.round(search);
search = search / 100;
int result=binarySearch(array, search, n, 0);
if (result == -1)
System.out.println(search +" befindet sich nicht im Array.");
else
System.out.println(search+" befindet sich im Array an der Stelle "+(result)+".");
}
}
}
I would like to see the number of comparisons that a binary search must perform in order to find a number, but I do not know how to implement it. I have already done a cycle to see the average value of comparisons, but I do not know how to get the number of comparisons.
source
share