C Program for Binary Search (Recursive and Iterative)?

CServer Side ProgrammingProgramming

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

Explanation

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.

Example

#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;
}

Output

55 is found at Index 8

Example

#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;
}

Output

55 is found at Index 8
raja
Published on 19-Aug-2019 08:17:19
Advertisements