Binary Search In C Program Using Recursion

Binary Search in C Program
Binary Search in C Program

Binary Search In C Program Using Recursion. If you are looking for a binary search in C with recursion example, this C programming tutorial will help you to learn how to write a program for binary search in C. Just go through this C programming example to learn about binary search, we are sure that you will be able to write a C program for binary search using recursion.

Binary Search In C

Binary Search in C – Table of Contents

What is Binary Search Algorithm?

Binary search is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the sorted array, if they are unequal, the half in which the target cannot lie is eliminated and the search continues for the remaining half until it is successful. Binary search is designed for fast searching.

Binary Search in C Using Recursion

Learn how to write a  program for binary search in c using recursion. Writing a binary search C program can be done using various techniques but here in this program, we show how to write a binary search program in c using recursion in a proper way. Happy coding.

Binary Search in C Program Using Recursion – Source Code

You can copy paste the below binary search program in c compiler to check how the source code work. Or write your own Binary Search C Program Using recursion with the help of this below c program for binary search.

/* BINARY SEARCH PROGRAM IN C USING RECURSION - BINARYSEARCH.C */

#include<stdio.h>

#include<stdlib.h>

#define size 10

 

int binsearch(int[], int, int, int);

 

int main() {

   int num, i, key, position;

   int low, high, list[size];

 

   printf("\nEnter the total number of elements");

   scanf("%d", &num);

 

   printf("\nEnter the elements of list :");

   for (i = 0; i < num; i++) {

      scanf("%d", &list[i]);

   }

 

   low = 0;

   high = num - 1;

 

   printf("\nEnter element to be searched : ");

   scanf("%d", &key);

 

   position = binsearch(list, key, low, high);

 

   if (position != -1) {

      printf("\nNumber present at %d", (position + 1));

   } else

      printf("\n The number is not present in the list");

   return (0);

}

 

// Binary search function for binary search

int binsearch(int a[], int x, int low, int high) {

   int mid;

 

   if (low > high)

      return -1;

 

   mid = (low + high) / 2;

 

   if (x == a[mid]) {

      return (mid);

   } else if (x < a[mid]) {

      binsearch(a, x, low, mid - 1);

   } else {

      binsearch(a, x, mid + 1, high);

   }

}

C PROGRAM FOR Binary search – OUTPUT

After you compile and run the above binary search program in c using recursion, your C compiler asks you to enter elements for the sorted array to perform the binary search. After you enter elements, the program will be executed and give output.

Enter the total number of elements: 7
Enter the elements of list : 10 21 32 43 54 65 76
Enter element to be searched: 32
Number present at 3

C PROGRAMMING Tutorials

  1. Bubble Sort in C Using Pointers
  2. Bubble Sort Program in C Using Recursion
  3. Bubble Sort Program in C Using Array
  4. Bubble Sort Program in C Using Function
  5. Bubble Sort Program in C Using Linked List
  6. Stack Push Pop Program in C Using Arrays
  7. Factorial Program in C Using Pointers
  8. Factorial Program in C Using While Loop
  9. C Program For Factorial Using For Loop
  10. Factorial Program in C Using Recursion Function
  11. C Program To Reverse a String Using Pointers
  12. C Program To Swap Two Numbers Using Two Variables
  13. C Program To Swap Two Numbers Using Three Variables
  14. C Program For Prime Numbers – Check  a Number is Prime or Not
  15. C Program To Reverse a String with Using Function
  16. C Program to Reverse a String without Using Function
  17. C Program to Reverse a Sting Using Recursion
  18. C Program For Palindrome String
  19. C Program For Palindrome Numbers
  20. C Program To Reverse a String with Using Function
  21. C Program To Reverse a String without Using Function
  22. C Program To Reverse a String Using Recursion

LEAVE A REPLY

Please enter your comment!
Please enter your name here