C program for Recursive Binary & Linear Search
Here you will get and learn the program for recursive binary & linear search using C programming language.
How to Program for Recursive Binary & Linear Search
Recursive Binary Search and Recursive Linear Search are two different algorithms used for searching elements within a sorted list or array.
Recursive Binary Search
Binary search is a divide-and-conquer algorithm that works by repeatedly dividing the search interval in half. It compares the target value to the middle array element. The search is complete after the target value matches the middle element.
If the target value is smaller than the middle element, the search moves to the array’s lower half. If the target value is higher, the search will continue on the upper half. This operation is performed recursively until the target value is discovered or the search interval is empty.
Recursive Binary Search Program in C
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 48 49 50 | // C program to implement Recursive Binary Search #include <stdio.h> int BinarySearchElement(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return BinarySearchElement(arr, l, mid - 1, x); return BinarySearchElement(arr, mid + 1, r, x); } return -1; } int main(void) { int arr[7]; int x,result=0; printf("Enter 7 Numbers to input : "); for(int i=0;i<7;i++) { scanf("%d",&arr[i]); } printf("Enter Number To Search : "); scanf("%d",&x); int n = sizeof(arr) / sizeof(arr[0]); result = BinarySearchElement(arr, 0, n - 1, x); (result == -1); if (result==-1) printf("\n\nElement is not present in array\n\n"); else printf("\n\nElement is present at index %d",result); return 0; } |
Output 1
Enter 7 Numbers to input : 10 20 30 50 40 80 70
Enter Number To Search : 30
Element is present at index 2
Output 2
Enter 7 Numbers to input : 10 20 30 50 40 80 70
Enter Number To Search : 35
Element is not present in array
Recursive Linear Search
Linear search is a simple search algorithm that sequentially checks each element of the list until it finds the target value or reaches the end of the list. Recursive linear search applies the same logic but in a recursive manner.
Recursive Linear Search Program in C
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 | //C program to implement Recursive Linear Search #include <stdio.h> using namespace std; // Recursive function to find x in arr[] int LinearSearchElement(int arr[], int size, int x) { size--; if (size < 0) { return -1; } if (arr[size] == x) { return size; } return LinearSearchElement(arr, size, x); } int main() { int arr[7]; int x, result=0; printf("Enter 7 Numbers to input : "); for(int i=0;i<7;i++) { scanf("%d",&arr[i]); } printf("Enter Number To Search : "); scanf("%d",&x); int size = sizeof(arr) / sizeof(arr[0]); result = LinearSearchElement(arr, size, x); if (result != -1) printf("Element %d is present at index %d ", x , result); else printf("\nElement %d is not present in the array",x); return 0; } |
Output 1
Enter 7 Numbers to input : 1 2 3 4 5 6 7
Enter Number To Search : 5
Element 5 is present at index 4
Output 2
Enter 7 Numbers to input : 1 2 3 4 5 6 7
Enter Number To Search : 9
Element 9 is not present in the array
Read Also
Check out our DAA Lab Manual and Programs