title: Problem in combined code of bubble sort and binary search of an array in C
body: <p>I was trying to sort a user input array using <em>bubble sort</em> and then do <em>binary search</em> to find a certain key.</p>
  
  <p>However each code works fine on its own, but when I combine them binary search doesn't work: when I enter the search key it sorts the arrays and gives output but the key is always not found. I tried to add a <code>break</code> statement after the first <code>if</code> in <code>while</code> but it didn't work either</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void) {
      int arr[100];
      int temp, size ,first, last, middle ,search,found,result;
  
      printf("Enter the array size: ");
      scanf("%d",&amp;size);
      printf("Enter the array elements: ");
  
      for(int i = 0 ; i &lt;size ;++i )
      {
          scanf("%d",&amp;arr[i]);
      }
      printf("Enter search value: ");
      scanf("%d", &amp;search);
  
      for(int i = 0 ; i&lt;size ; ++i)
          for(int j= 0; j&lt;size-1; j++)
          {
              if(arr[j+1]&gt;arr[j])
              {
                  temp = arr[j];
                  arr[j] = arr[j+1];
                  arr[j+1] = temp;
              }
          }
  
      printf("Elements of the array sorted in descending order: ");
      for(int i = 0;i &lt;size ;++i)
      {
          printf("%d ", arr[i]);
      }
  
      printf("\n");
      printf("Number of passes is: %d", size -1);
  
      first = 0;
      last = size- 1;
      found = 0;
  
      while (!found &amp;&amp;first &lt;= last) {
          middle = (first+last)/2;
  
          if ( search == arr[middle] )
          {
              found = 1 ;
              result = middle;
              break;
          }
          else if(search &lt; arr[middle])
          {
              last = middle - 1;
          }
  
          else
              first = middle +1;
      }
  
      if (found)
      {
          printf("\n");
          printf("%d found at location %d.\n", search, result);
      }
      else 
      {
          printf("\n");
          printf("%d is not found", search);
      }
  
      return 0;
  }
  </code></pre>