0% found this document useful (0 votes)
6 views5 pages

Search Algorithms in C: Linear & Binary

The document contains three C programs demonstrating different search algorithms: linear search, binary search, and recursive binary search. Each program includes a main function that initializes an array, prompts the user for an element to search, and displays the index of the found element or a message if not found. The linear search is implemented iteratively, while the binary search is implemented both iteratively and recursively.

Uploaded by

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

Search Algorithms in C: Linear & Binary

The document contains three C programs demonstrating different search algorithms: linear search, binary search, and recursive binary search. Each program includes a main function that initializes an array, prompts the user for an element to search, and displays the index of the found element or a message if not found. The linear search is implemented iteratively, while the binary search is implemented both iteratively and recursively.

Uploaded by

anmolupadhyay090
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>

int linear_search(int arr[], int n, int ele) {

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

if (arr[i] == ele) {

return i;

return -1;

int main() {

int arr[6] = {1, 3, 3, 4, 7, 9};

int ele = 4;

int n = 6;

int result = linear_search(arr, n, ele);

if (result != -1)

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

else

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

return 0;

}
#include <stdio.h>

int binarySearch(int arr[], int n, int ele) {

int s = 0, e = n - 1;

while (s <= e) {

int mid = (s + e) / 2;

if (arr[mid] == ele)

return mid;

else if (ele < arr[mid])

e = mid - 1;

else

s = mid + 1;

return -1;

int main() {

int n, ele;

printf("Enter number of elements (sorted order): ");

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

printf("Enter %d sorted elements: ", n);

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

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

printf("Enter element to search: ");

scanf("%d", &ele);

int result = binarySearch(arr, n, ele);

if (result == -1)

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

else

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

return 0;

}
#include <stdio.h>

// Recursive Binary Search

int b_search(int arr[], int low, int high, int ele) {

if (low > high)

return -1;

int mid = (low + high) / 2;

if (arr[mid] == ele)

return mid;

else if (ele < arr[mid])

return b_search(arr, low, mid - 1, ele);

else

return b_search(arr, mid + 1, high, ele);

int main() {

int arr[] = {10, 20, 30, 40, 50};

int n = 5;

int ele = 40;

int result = b_search(arr, 0, n - 1, ele);

if (result == -1)

printf("Element not found\n");

else

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

return 0;
}

You might also like