0
  bool binary_search (int target, int array[])
  { 
       int end =  length-1;
    int start = 0;  
    //   7          0           14
    //  11          8           14
    //   9         10      +    8    =
    //             8       +    8
      int middle  = (start+ end)/2;


   do
   {   //also changed end by your request
        int end =  length-1;
        int start = length;  
        //   7          0           14
        //  11          8           14
        //   9         10      +    8    =
        //             8       +    8
          int middle  = (start+ end)/2; 
        // 

        else if array[middle] == array[target])
        {
            return true;
        }        //11          7
                 //
        else if ( array[target] > array[middle] )
        {         //8
            int start = middle+1;
                //          8             13
                if (array[middle] == array[target])
                {       
                   return true;
                }    
        }                  
        else if(array[target] < array[middle])
        {
          //10
        }                
 }
 while (end - start > 0 )
     return false;
}

// This was the code I was going to post.My ide was messy and got the wrong code. The while loop was wrong though but I corrected it from your suggestion.

bool binary_search (int target, int array[],int length) {

    do
    {         int end =  length-1;
                   int start = 0;  
    //   7          0           14
    //  11          8           14
    //   9         10      +    8    =
    //             8       +    8
      int middle  = (start+ end)/2;


        if length[middle] == length[target])
        {
            return true;
        }        //11          7
                 //
        if ( length[target] > length[middle] )
        {         //8
            int start = middle+1;
                //          8             13
                if (length[middle] == length[target])
                {       
                   return true;
                }    
        }                  
        else if(length[target] < length[middle])
        {
          //10      
            end = middle - 1;

                {    
                   return false;
                }
        }                
    }            
    while (  array[end_num]- array[start_num] > 0)//notice error here because pointed it out.
     return false;        
  } 
0

Your code will not work, there are many errors, I will indicate only the most serious: The end condition of while loop is incorrect, you must go through the whole array the correct thing is,

while (end - start >= 0 )

The variables end and start, refer to the array indices, not to the values of the array itself, therefore the initial values should be the first and last index,

    int end = length; (perhaps lenght - 1)
    int start = 0;

We can not use an else-if condition without an earlier if condition, Else is always secondary to an if, if the condition in if is not fulfilled, it executes the part of the code corresponding to the else block, it does not make sense an else alone.

else if ( array[target] > array[middle] )
        {         //8
            int start = middle+1;
                //          8             13
                if (array[middle] == array[target])
                {       
                   return true;
                }    
        } 

The internal condition if, is unnecessary since we check in the first outermost condition that this block is only executed if they are different, here we will only change the indices of the ends, nor should we declare the variable int start again in the block else -if , Because we are actually creating a new variable (although with the same name) as the global variable start, this new variable is only visible within the given block and is destroyed when we leave it, thus ceasing to exist, the consequence is that Global variable will not change, and we always have the same extremes. I hope this helps to get you started.

2
  • Thanks I put the wrong code accidentally.
    – Tom55555
    Aug 20 '17 at 18:55
  • So does it work now? @MARS
    – Tom55555
    Aug 22 '17 at 0:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .