E
E
Egor2016-01-19 21:50:34
C++ / C#
Egor, 2016-01-19 21:50:34

How to do binary search in C?

It turns out an infinite loop, what to do? And how to find out if the number is found?

#include <stdio.h>
#define SIZE 10

int main (void){
  int array_1[SIZE], count_1, count_2, reserve, search, left, right, mid;
 
  for (count_1 = 0; count_1 < SIZE; count_1++){
    printf ("Enter array (%d): \t", SIZE - count_1);
    scanf ("%d", &array_1[count_1]);
  }  
  
  printf ("%s%10s \n", "Element", "Value");

  for (count_1 = 0; count_1 < SIZE; count_1++){
    printf ("%7d%10d \n", count_1, array_1[count_1]);
  }

  for (count_1 = 0; count_1 < SIZE - 1; count_1++){
    for (count_2 = 0; count_2 < SIZE - count_1 - 1; count_2++){
      if (array_1[count_2] > array_1[count_2 + 1]){
        reserve = array_1[count_2];
        array_1[count_2] = array_1[count_2 + 1];
        array_1[count_2 + 1] = reserve;
      }
    }
  }  

  printf ("%s%10s \n", "Element", "Value");
  
  for (count_1 = 0; count_1 < SIZE; count_1++){
    printf ("%7d%10d \n", count_1, array_1[count_1]);
  }

  printf ("Enter value: \t");
  scanf ("%d", &search);

  left = array_1[0];
  right = array_1[SIZE - 1];
  mid = (left + right) / 2;

  while (left <= right){
    if (search < array_1[mid]){
      left = mid - 1; 
    } else{
      right = mid + 1;  
    }
  }

  return 0;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2016-01-19
@Nemovok

It is quite logical that the cycle is infinite.
Firstly, initially left and right are for some reason not set by indexes, but by values ​​from the array. Secondly, mid must be calculated at each step of the loop. Thirdly, there is no check to find the desired value. Fourth, the left and right borders are shifted incorrectly inside the loop.
Use a debugger and step through the section of the program with control of all variables, or even better, do it in your mind, writing down the result of each step on paper.

E
Egor, 2016-01-20
@Nemovok

I don't know how correct this is, but it works =) Thank you.

#include <stdio.h>
#define SIZE 10

int main (void) {
  int arrayOne [SIZE], countOne, countTwo, reserve, search, left, right, mid;
  
  for (countOne = 0; countOne < SIZE; countOne++) {
    printf ("Enter array (%d): \t", SIZE - countOne);
    scanf ("%d", &arrayOne [countOne]);
  }

  printf ("%s%10s \n", "Element", "Value");

  for (countOne = 0; countOne < SIZE - 1; countOne++) {
    for (countTwo = 0; countTwo < SIZE - countOne - 1; countTwo++) {
      if (arrayOne [countTwo] > arrayOne [countTwo + 1]) {
        reserve = arrayOne [countTwo];
        arrayOne [countTwo] = arrayOne [countTwo + 1];
        arrayOne [countTwo + 1] = reserve;
      } 
    }
  }

  for (countOne = 0; countOne < SIZE; countOne++) {
    printf ("%7d%10d \n", countOne, arrayOne [countOne]);
  }

  printf ("Enter value: \t");
  scanf ("%d", &search);

  left = 0;
  right = SIZE - 1;

  while (left < right) {
    mid = (left + right) / 2;
    if (search <= arrayOne [mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }

  if (search == arrayOne [right]) {
    printf ("Ok! \n");
  } else {
    printf ("No! \n");
  }

  return 0;
}

A
abcd0x00, 2016-01-20
@abcd0x00

How to do binary search in C?

It's already done - the bsearch() function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question