#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;
}