13th Mar 2018, 1:16 PM
Bahubali
Bahubali - avatar
3 ответов
+ 4
Here's your code corrected: import java.util.Scanner; class TestClass { static void fun(int arr[],int a) { int n=arr.length; int l=0,u=n-1,m; while(l<=u) { m=(l+u)/2; if(arr[m]==a) { System.out.println("Found "+a+" at index "+m); break; } else { if(arr[m]<a) l=m+1; else u=m-1; } } } public static void main(String args[]) { int n; Scanner s=new Scanner(System.in); int ar[]={1,23,45,67,68,82,87,88,89,96,123}; int find=88; fun(ar,find); } } Hth, cmiiw
13th Mar 2018, 2:16 PM
Ipang
+ 3
I don't understand what is your code logic. But i guess you want to find certain number which is 88? However, in your code, 'm' never be equal with a. Because 1st: you set 'm' value to some value (l+u) divided by 2 which will get much smaller value. 2nd: I think 'm' can never be greater than arr[m], which is much greater than current 'm' value. 3rd: You didn't do enough incresement to 'u'. That's all my guess. Here what i thought it should be. May be wrong. Sorry if i'm misunderstanding. https://code.sololearn.com/cXf6fin4fsAU/?ref=app
13th Mar 2018, 2:21 PM
Sylar
+ 1
thank u i got my error..
13th Mar 2018, 3:13 PM
Bahubali
Bahubali - avatar