|
binary search
hey i got confused with this and maybe someone can help me...
i was experimenting about sorting a randomly generated array of 100 integers with binary search.. however i have some trouble..
heres d code:
class iamangry
{
public int binarySearch(int arr[], int key, int n)
{
int low = 0;
int ri = (arr[n]+ 1);
int mid;
do{ mid = ((low + ri)/2);
if(key < arr[mid]){
ri = (mid - 1);
}
else low = mid + 1;
}
while (key == arr[mid]);
if(low < ri){
return -1;
}
else return mid;
}
public static void main(String[] args){
int x = 100;
int[]arr = binarySearch[x];
for(int n = 0; n < a.length; n++){
arr[n] = 1 + (int)(Math.random() *100);
}
for(int t=1; t<=100; t++){
System.out.print(""+arr[t]+" ");
}
}
}
the problem seems to be actually in: int[]arr = binarySearch[x]; ...
any help please? thanks
|