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 in C Using Recursion
- Binary Search in C Program Using Recursion – Source Code
- Binary Search in C Program Using Recursion – Output
- C Programming Tutorials
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
- Bubble Sort in C Using Pointers
- Bubble Sort Program in C Using Recursion
- Bubble Sort Program in C Using Array
- Bubble Sort Program in C Using Function
- Bubble Sort Program in C Using Linked List
- Stack Push Pop Program in C Using Arrays
- Factorial Program in C Using Pointers
- Factorial Program in C Using While Loop
- C Program For Factorial Using For Loop
- Factorial Program in C Using Recursion Function
- C Program To Reverse a String Using Pointers
- C Program To Swap Two Numbers Using Two Variables
- C Program To Swap Two Numbers Using Three Variables
- C Program For Prime Numbers – Check a Number is Prime or Not
- C Program To Reverse a String with Using Function
- C Program to Reverse a String without Using Function
- C Program to Reverse a Sting Using Recursion
- C Program For Palindrome String
- C Program For Palindrome Numbers
- C Program To Reverse a String with Using Function
- C Program To Reverse a String without Using Function
- C Program To Reverse a String Using Recursion