## C Program For Binary Search using Recursion

Learn How To Find an Element in 1-Dimensional Array using Binary Search using Recursion in C Programming Language. It is important that we should know **How A For Loop Works** before getting further with the C Program Code.

**Note: The prerequisite for Binary Search is the Elements in the Array must be in Sorted Order.**

#### Recursive Binary Search Algorithm Analysis

The worst case scenario of Binary Searching is when the element is not present in the Array. The best case of this algorithm is when the element to be searched is present at the middle position in the Array. The Runtime complexity of Binary Search Algorithm using Recursion is **O(log n)**. The Binary Search Algorithm is preferred only when the data is static. Binary Search is not suitable to be implemented in Linked Lists as direct access is not allowed in **Linked Lists**.

**Must Read: C Program For Binary Search using Functions**

#### C Program For Binary Search using Recursion Method

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <stdio.h> int RecursiveBinarySearching(int arr[], int low, int high, int element) { int middle; if(low > high) { return -1; } middle = (low + high) / 2; if(element > arr[middle]) { RecursiveBinarySearching(arr, middle + 1, high, element); } else if(element < arr[middle]) { RecursiveBinarySearching(arr, low, middle - 1, element); } else { return middle; } } int main() { int count, element, limit, arr[50], position; printf("\nEnter the Limit of Elements in Array:\t"); scanf("%d", &limit); printf("\nEnter %d Elements in Array: \n", limit); for(count = 0; count < limit; count++) { scanf("%d", &arr[count]); } printf("\nEnter Element To Search:\t"); scanf("%d", &element); position = RecursiveBinarySearching(arr, 0, limit - 1, element); if(position == -1) { printf("\nElement %d Not Found\n", element); } else { printf("\nElement %d Found at Position %d\n", element, position + 1); } return 0; } |

#### Output

In case you get any Compilation Errors with this C Program To Search Array Element using Binary Search using Recursion or if you have any doubts about it, let us know about it in the Comment Section below.

I feel Binary Search using Functions and For Loop is much more easier than Recursive Method.

Yes. Easy things are always easy to understand. Iterations are easy to understand and code but Recursion is also not that bad. It is hard to understand but it is interesting.

It’s actually easier but if you will follow the actual binary search algorithm it will make more sense with a recursive approach.

Binary Searching in C is reallh an interesting solution.

Binary Searching in C is really an interesting solution.