C Program To Perform Binary Search On 10 Elements Rating: 6,6/10 6461 votes

C Program to implement Binary Search Tree Traversal. Get_node() function will allocate memory dynamically and allocate one node. If below condition is satisfied then we can say that we are going to create first node of the tree. (i.e Tree is empty and this created node is very first node). Enter the elements in ascending order: 4 7 8 11 21. Enter the number to be search: 11. The number is found. Write a c program for linear search. Write a c program for binary search. Write a c program for binary search using recursion. Big list of c program examples. Email This BlogThis! Share to Twitter Share to Facebook Share to. Is there a possibility of the program being able to tell the user if he is attempting to perform such a search (of something that's not in the array)? I am assuming it is not possible as it is a basic flaw in the binary search algorithm. Please enlighten me. This C Program accepts the sorted array and does search using Binary search. Binary search is an algorithm for locating the position of an item in a sorted array. A search of sorted data, in which the middle position is examined first. C Program for Binary Search Tree Creation and Traversals. Binary Search Tree is a Tree which has the following properties, 1.The left sub tree of a node contains smaller nodes than a root node. 2.The right sub tree of a node contains greater nodes than a root node. 3.Both the left and right sub trees must also be binary search trees.

  1. Program To Perform Binary Search
  2. Binary Search Java
Active5 years, 8 months ago

C Program to find an Element using Binary Search. C Program to Perform Arithmetic Operations Using Switch. C Program to Print Elements of Array Using Pointers.

I've found the following code online,

My function has a specified prototype(meaning that it has a set number of arguments that cannot be altered) this is what I have so far

Does my implementation look correct so far? I can't seem to figure out how to implement the final else statement.


7 Answers

high and low represent the bounds of subarray in which continue the research. If you analyze the code you'll notice that if target is smaller that a[middle] you'll have to continue the research in the first half of the array (in fact it calls binary_search passing the same low bound but, as a superior bound, the actual middle-1). On the other side, if target is greater that a[middle] you'll have to continue the research in the second half of the array (from middle+1 to high). Of course, if target is equal to a[middle] you've finished.

9,2086 gold badges38 silver badges74 bronze badges

The trick to writing a recursive anything:

  1. Figure out how it should end.
  2. Figure out how it should look one step before it ends.
  3. Figure out how it should look two steps before it ends, and how moving from #2 to #1 is exactly the same as moving from #3 to #2.

Step #1:

If the number at the beginning of the search range is the desired number, return true.

If the end of the search range is the same as the beginning of the search range, and the number in the search range is not the desired number, return false.

Step #2:

If the search range has a length of two, split it into two one element search ranges, and search the range that might contain the required number.

Step #3:

If the search range has a length of more than two, split it into two roughly equal search ranges, and search the range that might contain the required number.

(which combining the two would look like)

If the search range has a length of two or more elements, split it into two roughly equal ranges, check the highest (last) number in the 'lower' range, if the number is equal to or less than that number, search the lower range; otherwise, search the higher range.

This technique will not return you an optimum solution unless you select an optimum way to solve the problem; however, it will return you a correct solution (provided you do not make any true blunders).

Now the code

When reading code online, you have a big disadvantage. You don't do any of the above three steps, so you really have to go about solving the problem backwards. It is akin to saying, I have a solution, but now I have to figure out how someone else solved it (assuming that they didn't make any mistakes, and assuming that they had the exact same requirements as you).

Edwin BuckEdwin Buck
58.1k5 gold badges79 silver badges112 bronze badges

The high and low variables represent the current range you are searching. You usually start with the beginning and end of the array, and then determine if the value is in the first or second half, or exactly in the middle. If it is in the middle, you return that point. If it is below the middle, you search again (recursively), but now only in the lower half. If it is above the middle, you search the upper half. And you repeat this, each time dividing up and narrowing the range. If you find the value, you return, otherwise, if the range is so narrow that it is empty (both low and end high indexes are the same), you didn't find it.

Rudy VelthuisRudy Velthuis
26.2k4 gold badges39 silver badges79 bronze badges

High and low are upper and lower bounds on the candidate indices of the array. In other words, they define the portion of the subarray in which it is possible for the search target to exist. Since the size of the subarray is cut in half each iteration, it is easy to see that the algorithm is O(log n).

19.4k3 gold badges28 silver badges61 bronze badges

On your current code, first of all, n should not be in parentheses (it doesn't make a difference, but it confuses me).

Next up, if it's meant to be returning the index in the array, your code doesn't do that, it returns 1. Judging by the prototype, you might consider a non-recursive approach, but this can work fine if you add the right values on to each return.

You can figure out the other statement. Just draw a picture, figure out where the pointers should be, and code them up. Here's a start:

Actually, you probably don't want to be including your middle value. Finally, make sure to take in to account lists of length zero, one, two and three. And please write unit tests. This is probably one of the most often incorrectly implemented algorithms.


I have tried to resolve your problem and this below code is really work . But what is the condition to escape recursion if value that want to be searched not lies in array

15.9k3 gold badges46 silver badges114 bronze badges
Hakun NguyenHakun Nguyen
4,13612 gold badges30 silver badges36 bronze badges

Not the answer you're looking for? Browse other questions tagged c or ask your own question.

/* C program for binary search: This code implements binarysearch in */

/* C language. It can only be used for sorted arrays, but it'sfast as */

/* compared to linear search. If you wish to use binary searchon an */

/* array which is not sorted then you must sort it using somesorting */

/* technique say merge sort and then use binary search algorithmto */

/* find the desired element in the list. If the element to besearched */

/* is found then its position is printed. */




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

printf('Enter number of elementsn');


printf('Enter %d integersn', n);

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


printf('Enter value to findn');


first = 0;

last = n - 1;

middle = (first+last)/2;

while( first <= last )


if ( array[middle] < search )

first = middle + 1;

else if ( array[middle] search )


printf('%d found at location %d.n', search, middle+1);




last = middle - 1;

middle = (first + last)/2;


if ( first > last )

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

return 0;


Binary search program in c using for loop?

C program for Performing Binary Search http://thingsoverflow.com/?p=211

How do you write a c program to convert binary to decimal using stack?

How do you write a c program to convert decimal no to binary no using stack in c++

Program of binary search?

A binary search function is built into the c libraries. Look up bsearch.

Write a program of binary heap in c or c language?

How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement

Write an program that accepts an input as a decimal number and coverts in into binary representation?

write an algoriths/program that accepts an input as a decimal number and converts it into binary representation in c.

Best first search program in c?

The best search programs to attempt writing in C are the following: Linear search (simplest), Binary search (faster) Hash search (fastest).

Write a C program that takes a binary file as input and finds error check using different mechanisms?

write a c program that takes a binary file as input and finds error check using different mechanisms.

How to write a program to implement a binary search tree in C?

It is possible, you'll have to use a structure like this: typedef struct TreeElement { struct TreeElement *left, *right; int data; } TreeElement;

You want to download c plus plus program free?

C++ is a language, not a program. If you are trying to write C++ programs you can download gcc, a free compiler, which will convert your C++ code into executable binary code.

Write a c program to convert binary number to decimal number?

How is this a question? Sounds like you should do more of your homework offline.

How you can run a c program in Linux 2.4?

if it is n already compiled binary program: ./program-name if it is a code, gcc program-code.c -o program-name if gcc is not installed, on debian: search for a deb package and install it, or, apt-get install gcc on redhat: search for an rpm package and install it.

Program to perform Fibonacci search in c language?

Explain the different searching techniques in c?

C program to create a binary tree?

Write a 'c' program using command line arguments to search for a word in a file and replace it with the specified word?

this program waste program ra chantiga, dont waste ur time

Incoming Search Terms: • Winner movie songs • Winner songs download • Winner movie mp3 songs masstamilan • Winner high quality songs • Winner mp3 songs 320kbps • Winner starmusiq • Winner songs rar/zip download • Winner songs download starmusiq • Winner songs download starmusiqcc.com • Winner songs download tamiltunes • Winner songs download saavn gaana itunes • Engirunthai Naan Mannil mp3 song download • Enthan Uyir Thozhiye mp3 song download • Eye Um Eye mp3 song download • Kozhi Kokkara mp3 song download • Mathapoo mp3 song download • Mudhal Murai mp3 song download. Himesh reshammiya all mp3 songs free download.

Write a C program to display a pascal triangle?

write a c program to display * * * * * * * * * * * * * * * *

Write a c program for queue operation?

How is a C program rum?

C-source program doesn't rum, you have to compile and link it. The executable rums like any other binary program.

Write a program to maintain medical shop in c language?

Ask via an internet search engine; it will find vendors for you.

How do you write a c program in javascript?

Uh.. You write a C program in C, not in JavaScript.. They're not related in any way.

Write a c program to check whether a number is binary or not?

#include<stdio.h> void main() { int n; clrscr(); printf('enter a no.'); scanf('%d',&n); if(n%100n%101) printf('yes binary'); else printf('not binary'); getch(); } }

What are the file modes in c language?

r- read w- write r+- read and write w+- write and read a- append rb- read in binary wb- write in binary

Write a program in c plus plus the binary search algorithm construction and operators?

Binary search can be achieved with a sorted array. By recursively checking the middle element of the array, you can reduce the array by half until you either locate the element or you have an empty an array. This achieves the same end as a binary tree but with less memory overhead. The middle element represents the root, with its left node pointing to the middle element of the left portion of the array and…

How c program gets stored in the memory?

Write a c program for creating a virus?

Write a c program to print the 100 to 1 nos?

Write a C program to draw a sphere in computer graphics?

Write a C program to draw a sphere in computer graphics?

Write a c program to find simple interest?

write a program in c to calculate simple interest for 5 years

How do you write a C program which runs independent of the compiler No need to install the compiler?

Any computer program does not need compiler at the time of execution. Compiler is needed to convert a high-level language program into an intermediate machine code. When you run a program, the executable binary runs, and that is what you get after compilation, etc. Summary: your Q is incorrect. Please read about compiler, and linker to understand the concept. You cannot write c or c++ code and run it without a compiler. a compiler translates…

Write a c program to find the binary addition of two 8 bit numbers?

C program for magic square?

Where do we write main function in a c program?

How do you write a c program to analysis a circuit?

There is no need to write a C program for circuit analysis; there are lots of packages out there that already do that (for example, Spice).

Write a c program to find Volume and surface area of cube?

Write a c program to compute the surface area and volume of a cube

C program which accepts in order and preorder traversal outputs of a binary tree as input and prints the corresponding binary tree?

How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.

How do you write a C program to check whether the number is odd or even Ps-using DEV complier?

To write a C program to determine if something is odd or even you need to be a programmer. To write a program in C is complicate and only done by programmers.

What is binary search in data structure using c?

a tree which has atmost two nodes is called binary tree binary search tree is a binary tree which satisfies the following 1.every node in tree must be distinct 2.values in right subtree > value at root 3.values in left subtree < value at root 4.left,right subtrees must be binary search trees

Single pass assembler program in c?

A C++ program will be able to be used for a single pass assembler program in C. This program can be used to write many different types of C++ programming.

Write c programs to display 10 line biodata?

A C++ program can be used to write C programs that will display 10 lines of biodata. Many types of C programming can be written with a C++ program.

How do you write a c program to find maximum and minimum of a set of numbers?

How do you write a c program to find maximum and minimum number in a given array?

Write Client and server program in C language using UDP?

Write and run a client and a server program in C-language using UDP

Write a program to illustrate the usage of pointers with arrarys and functions?

* * * * * * * * * * write the c++ program and show me brifily?

How do you write socket program in c?

How do you write a program in embedded c?

What is the Difference between c and shell program execution?

The shell interprets the script, while the C-compiler generates a binary executable.

C program for linked list?

What is linked list? Write a program to represent a linked list in C.

Binary Search Java

How do you write a c program that prints a box an oval an arrow and a diamond?

What are instructions written in computer language that tells the computer what to do?

Computer language, or coding language, can be in a variety of different formats. These 'languages' may include C, C++, C#, Java, Ruby, Python, and others. Many of these languages are converted into binary code (the code that computers actually understand) before they are run. Binary code is not readable by humans, but computer languages are. That is why most people write instructions in a programming languages and then use another program to convert that code…

Coments are closed
Scroll to top