Click here to Skip to main content
15,913,349 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public static void main(String[] args) 
    {
     int arr[]={1,5,8,14,15,18};
     int start=0,end=arr.length-1,target=14;
     while(start<=end)
     {
         int mid=(start+end)/2;
         if(target<arr[mid])
             end=mid-1;
         else if(target>arr[mid])
             start=mid+1; 
         else if(target==arr[mid])
             System.out.print(mid);      
     }
     
    }

here i get answer as 33333333333........so on where as same loop using function i get exact answer exaplain please//
public static void main(String[] args) 
    {
     int arr[]={1,5,8,14,15,18};
     int target=14;
     int ans=binarysearch(arr,target);
     System.out.print(ans);
    }
   static int binarysearch(int arr[],int target)
   {
       int start=0,end=arr.length;
       while(start<=end)
       {
           int mid=(start+end)/2;
           if(target<arr[mid])end=mid-1;
           if (target>arr[mid])start=mid+1;
           else 
               return mid;
       }
       return -1;
   }

CAN ANY BODY EXPLAIN PLEASE......

What I have tried:

I Tried this code in java please explain how this possible.
Posted
Updated 31-Jul-22 8:32am
Comments
[no name] 31-Jul-22 10:08am    
if you can't use a debugger, it is enough to insert some println() in your code to inform of the actual state during execute

Look at your code: in the function version, when you find a match, you exit and return it which exits the loop.
In the non-function version, you print, but continue the loop. without changing any variables - so you get the same result the next time round, and never exit the loop.

To be honest, a couple of minutes or less with the debugger would have shown you exactly what was happening.
 
Share this answer
 
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
compare these two logics

target=1
mid= 3
if (1 < 14) true
if (1 > 14) false
else
 return 3 //mid, wrong answer
--------

target=1
mid= 3
if (1 < 14) true
else // next

mid=1
if (1 < 5) true
else // next

mid=0
if (1 < 1) false
else
if (1 > 1) false
else
 return 0 //mid, ok
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900