-2

I am new to C++ and was trying to implement the binary search even though codes don't give any error or warning it did not show any output in the console either. I tried my best to figure out but found no specific reason . can someone please help why is this happening ?? thank you.

    #include <bits/stdc++.h>

    using namespace std;

    int binary(int arr[],int key,int low,int high)
    {
        int mid;
       mid=(low+high)/2;

        while(high>=low)
        {
            if(arr[mid]==key)
                {
                 return mid;
                }

            else if(arr[mid]>key)
            {

                high=mid-1;
            }
            else
                low=mid+1;
        }
        return-1;
    }

    int main(void)
    {
       std::cout<<"hello there";   // to test if at least this one gets printed.
       int arr[]={1,4,5,7,8,9,10};
       int key=9;
       int index,low=0,high=6;
       index=binary(arr,key,low,high);
       std::cout <<"element is present at: "<< index << std::endl;
       return 0;

    }
  • 1
    have you tried running with a debugger and breakpoints? – Daniel A. White Apr 05 '20 at 03:18
  • I tried with different IDE DevC, Atom, also Online GDB. When I copy paste one of the source code it is giving output. But for this particular code console showing blank – Raviraj Gardi Apr 05 '20 at 03:26
  • Your binary search is wrong. If you run through what you've written on paper or in your head then you will see that you never change mid. – Nathan Wride Apr 05 '20 at 03:30
  • @DanielA.White Thanks I tried it, I found one logical error : I when I put mid=(low+high)/2 in to while loop it is giving output. But I am still confused why it didn't print "hello" statement in main() which has nothing to do with binary() function – Raviraj Gardi Apr 05 '20 at 03:31

1 Answers1

0

You're caught in an infinite loop because you're never changing mid within the actual while loop. You set it to (0 + 6) / 2 == 3 before the loop starts and that's it. You should have:

while (high >= low) {
    mid = (high + low) / 2; // line moved in from before while
    if (arr[mid] == key)
    : : : 

The way you have it now, low changes from 0 to 4 but because mid doesn't change, you'll never check any element other than the original mid. And low will always be set to the original mid + 1 so will sit at 4 forever.

The reason you never saw they first line of output is because, without a \n or some other flushing mechanism, it's still sitting in the output buffers waiting to be displayed. By default, standard output is line buffered if it can be ascertained to be pointing to an interactive device, otherwise it's fully buffered (see here for an explanation of this).

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • Thanks for taking the time , it is now showing the result but why it did not print the "Hello" statement which is before the Binary function. – Raviraj Gardi Apr 05 '20 at 03:35
  • @RavirajGardi, I've added that in the final paragraph. – paxdiablo Apr 05 '20 at 03:36
  • Thanks for taking the time, I tried with the changes you suggested by adding std::endl and it is showing the statement. but can please elaborate more on that last statement of yours. i think i am missing a very fundamental understanding of how things get printed in c++. – Raviraj Gardi Apr 05 '20 at 03:43
  • @RavirajGardi, that's really a *different* question, one that's been answered before. See https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin/4201325#4201325 for example. – paxdiablo Apr 05 '20 at 03:47
  • Thanks a lot . very helpful – Raviraj Gardi Apr 05 '20 at 03:50