- Related Questions & Answers
- Binary Search (Recursive and Iterative) in C Program
- Java Program for Binary Search (Recursive)
- Program for average of an array(Iterative and Recursive) in C++
- Count half nodes in a Binary tree (Iterative and Recursive) in C++
- Count full nodes in a Binary tree (Iterative and Recursive) in C++
- 8085 program for Binary search
- Python Program for Binary Search
- C Program for Iterative Merge Sort
- Find Length of a Linked List (Iterative and Recursive) in C++
- Count consonants in a string (Iterative and recursive methods) in C++
- First uppercase letter in a string (Iterative and Recursive) in C++
- Binary Search in C++ program?
- Program to check if an array is sorted or not (Iterative and Recursive) in C
- C++ Program to Compare Binary and Sequential Search
- C++ Program to Search for an Element in a Binary Search Tree

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The binary search algorithm is an algorithm that is based on compare and split mechanism. The binary Search algorithm is also known as *half-interval search, logarithmic search, or binary chop*. The binary search algorithm, search the position of the target value in a sorted array. It compares the target value with the middle element of the array. If the element is equal to the target element then the algorithm **returns the index** of the found element. And if they are not equal, the searching algorithm uses a half section of that array, Based on the comparison of the value, the algorithm uses either of the first-half ( when the value is less than the middle ) and the second half ( when the value is greater than the middle ). And does the same for the next array half.

Input: A[] = {0,2,6,11,12,18,34,45,55,99} n=55 Output: 55 at Index = 8

**For our array** -

We will compare 55, with the middle element of the array which is 18, which is less than 55 so we will use second-half of the array i.e. the array {24, 45, 55, 99}, again the middle is 55. Check the value of search element with it. And the value matched, then we will return the index of this value with is 8.

If the search element would be smaller than the middle than we would have used the first-half and go on until the element is found at the middle of the array.

To Implement the binary search we can write the code in two ways. these two ways defer in only the way we call the function that checks for the binary search element. they are:

**Using iterations**− this means using a loop inside the function that checks for the equality of the middle element.**Using**In this method, the function calls itself again and again with a different set of values.

#include<stdio.h> int iterativeBsearch(int A[], int size, int element); int main() { int A[] = {0,12,6,12,12,18,34,45,55,99}; int n=55; printf("%d is found at Index %d \n",n,iterativeBsearch(A,10,n)); return 0; } int iterativeBsearch(int A[], int size, int element) { int start = 0; int end = size-1; while(start<=end) { int mid = (start+end)/2; if( A[mid] == element) { return mid; } else if( element < A[mid] ) { end = mid-1; } else { start = mid+1; } } return -1; }

55 is found at Index 8

#include<stdio.h> int RecursiveBsearch(int A[], int start, int end, int element) { if(start>end) return -1; int mid = (start+end)/2; if( A[mid] == element ) return mid; else if( element < A[mid] ) RecursiveBsearch(A, start, mid-1, element); else RecursiveBsearch(A, mid+1, end, element); } int main() { int A[] = {0,2,6,11,12,18,34,45,55,99}; int n=55; printf("%d is found at Index %d \n",n,RecursiveBsearch(A,0,9,n)); return 0; }

55 is found at Index 8

Advertisements