[go: up one dir, main page]

0% found this document useful (0 votes)
70 views3 pages

1 Program For Recursive Binary & Linear Search.

The document contains a C program that implements recursive linear and binary search algorithms. It allows users to input an array of elements and choose a search method to find a specified key. The program outputs the index of the found element or indicates if the element is not found.

Uploaded by

ashutosht23-ai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views3 pages

1 Program For Recursive Binary & Linear Search.

The document contains a C program that implements recursive linear and binary search algorithms. It allows users to input an array of elements and choose a search method to find a specified key. The program outputs the index of the found element or indicates if the element is not found.

Uploaded by

ashutosht23-ai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Program for Recursive Binary & Linear Search.

#include <stdio.h> // Include standard input/output library

// -------- Recursive Linear Search Function --------

int recursiveLinearSearch(int arr[], int size, int key, int index) {

if (index >= size) // Base case: if index is out of bounds

return -1; // Return -1, indicating element not found

if (arr[index] == key) // If the current element matches the key

return index; // Return the current index

return recursiveLinearSearch(arr, size, key, index + 1); // Recursive call for next index

// -------- Recursive Binary Search Function --------

int recursiveBinarySearch(int arr[], int low, int high, int key) {

if (low > high) // Base case: if range is invalid

return -1; // Return -1, element not found

int mid = low + (high - low) / 2; // Calculate middle index

if (arr[mid] == key) // If the middle element is the key

return mid; // Return the index

else if (key < arr[mid]) // If key is smaller, search in left half

return recursiveBinarySearch(arr, low, mid - 1, key);

else // If key is greater, search in right half

return recursiveBinarySearch(arr, mid + 1, high, key);

// -------- Main Function --------


Program for Recursive Binary & Linear Search.
int main() {

int arr[100]; // Declare array with max size 100

int n, i, key, choice, result;

// Input number of elements

printf("Enter number of elements: ");

scanf("%d", &n);

// Input elements

printf("Enter %d elements (for binary search, enter sorted elements):\n", n);

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

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

// Input key to search

printf("\nEnter element to search: ");

scanf("%d", &key);

// Display menu for search method

printf("Choose Search Method:\n");

printf("1. Recursive Linear Search\n");

printf("2. Recursive Binary Search (Array must be sorted)\n");

printf("Enter your choice: ");

scanf("%d", &choice);

// Perform search based on choice

switch (choice) {

case 1:

// Call linear search starting from index 0

result = recursiveLinearSearch(arr, n, key, 0);

break;
Program for Recursive Binary & Linear Search.
case 2:

// Call binary search between index 0 and n-1

result = recursiveBinarySearch(arr, 0, n - 1, key);

break;

default:

printf("Invalid choice.\n");

return 0;

// Print result

if (result == -1)

printf("Element not found.\n");

else

printf("Element found at index %d.\n", result);

return 0; // Return 0 to indicate successful execution

You might also like