Hello Everyone,

I want to know about more binary search in C programming and their various variation and how its work? I am sharing sharing code. Can anyone tell me is it right? Requirements ask for using binary search on an unsorted array, then it needs to be sorted first before using the binary search algorithm on it or anyone tell me where i can explore more about binary search in C program.

The code mentioned below assumes that the input numbers follow an ascending order!

include

int main()

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements:\n");

scanf("%d",&n);

printf("Enter %d integers:\n", n);

for (c = 0; c < n; c++)

  scanf("%d",&array[c]); 

printf("Enter the value to find:\n");

scanf("%d", &search);

first = 0;

last = n - 1;

middle = (first+last)/2;

  if (array[middle] < search)

     first = middle + 1;    

  else if (array[middle] == search) {

     printf("%d is present at index %d.\n", search, middle+1);

     break;

  }

  else

     last = middle - 1;

  middle = (first + last)/2;

if (first > last)

  printf("Not found! %d is not present in the list.\n", search);

return 0;

Hi Arun,

In this code having comiplation error:
main.c: In function ‘main’:
main.c:15:1: error: expected declaration specifiers before ‘printf’
printf("Enter number of elements:\n");
^~
main.c:16:1: error: expected declaration specifiers before ‘scanf’
scanf("%d",&n);
^~~~~
main.c:17:1: error: expected declaration specifiers before ‘printf’
printf("Enter %d integers:\n", \n);
^~
main.c:17:32: error: stray ‘\’ in program
printf("Enter %d integers:\n", \n);
^
main.c:18:1: error: expected declaration specifiers before ‘for’
for (c = 0; c < n; c++)
^~~
main.c:18:13: error: expected declaration specifiers before ‘c’
for (c = 0; c < n; c++)
^
main.c:18:20: error: expected declaration specifiers before ‘c’
for (c = 0; c < n; c++)
^
main.c:20:1: error: expected declaration specifiers before ‘printf’
printf("Enter the value to find:n");
^~
main.c:22:1: error: expected declaration specifiers before ‘scanf’
scanf("%d", &search);
^~~~~
main.c:24:1: error: expected declaration specifiers before ‘first’
first = 0;
^~~~~
main.c:26:1: error: expected declaration specifiers before ‘last’
last = n - 1;
^~~~
main.c:28:1: error: expected declaration specifiers before ‘middle’
middle = (first+last)/2;
^~
main.c:30:3: error: expected declaration specifiers before ‘if’
if (array[middle] < search)
^~
main.c:32:3: error: expected declaration specifiers before ‘else’
else if (array[middle] == search) {
^~~~
main.c:36:3: error: expected declaration specifiers before ‘else’
else
^~~~
main.c:38:3: error: expected declaration specifiers before ‘middle’
middle = (first + last)/2;
^~
main.c:39:1: error: expected declaration specifiers before ‘if’
if (first > last)
^~
main.c:42:1: error: expected declaration specifiers before ‘return’
return 0;
^~
main.c:13:40: error: declaration for parameter ‘array’ but no such parameter
int c, first, last, middle, n, search, array[100];
^~~~~
main.c:13:32: error: declaration for parameter ‘search’ but no such parameter
int c, first, last, middle, n, search, array[100];
^~
main.c:13:29: error: declaration for parameter ‘n’ but no such parameter
int c, first, last, middle, n, search, array[100];
^
main.c:13:21: error: declaration for parameter ‘middle’ but no such parameter
int c, first, last, middle, n, search, array[100];
^~
main.c:13:15: error: declaration for parameter ‘last’ but no such parameter
int c, first, last, middle, n, search, array[100];
^~~~
main.c:13:8: error: declaration for parameter ‘first’ but no such parameter
int c, first, last, middle, n, search, array[100];
^~~~~
main.c:13:5: error: declaration for parameter ‘c’ but no such parameter
int c, first, last, middle, n, search, array[100];
^
main.c:42:1: error: expected ‘{’ at end of input
return 0;

For More information run it here: https://www.onlinegdb.com/online_c_compiler

For proper code of Binary Search in C Go with this post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.