This section lists the programs covered under Searching Techniques in C. Click on a program title to jump directly to its detailed explanation.
Write a C program that uses a non-recursive function to search for a key value in a list of integers using linear search.
n.n integers into an array.linearSearch(a, n, key) to search for the key.
#include <stdio.h>
int linearSearch(int a[], int n, int key) {
int i;
for (i = 0; i < n; i++) {
if (a[i] == key)
return i + 1; // Return position (1-based index)
}
return -1; // Key not found
}
int main() {
int a[50], n, key, pos, i;
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Enter key element to search:\n");
scanf("%d", &key);
pos = linearSearch(a, n, key);
if (pos != -1)
printf("Element found at position %d\n", pos);
else
printf("Element not found\n");
return 0;
}
Enter number of elements:
5
Enter array elements:
10 25 30 15 40
Enter key element to search:
15
Element found at position 4
Linear search scans the array from the first element to the last, comparing each element with the key. As soon as a match is found, the function returns the position; if the end of the array is reached with no match, it returns −1.
This method is simple and works on both sorted and unsorted arrays, but it may be slow for large lists because every element might need to be checked in the worst case.
Write a C program that uses a non-recursive function to search for a key value in a sorted list of integers using binary search.
n.n integers into an array in sorted order.binarySearch(a, n, key).low = 0 and high = n − 1.low <= high:
mid = (low + high) / 2.a[mid] equals the key, return mid + 1 (position).a[mid], move the search range to the left half by setting high = mid − 1.low = mid + 1.main(), display the position if found, otherwise display “not found”.
#include <stdio.h>
int binarySearch(int a[], int n, int key) {
int low = 0, high = n - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (a[mid] == key)
return mid + 1; // Return position (1-based index)
else if (key < a[mid])
high = mid - 1; // Search in left half
else
low = mid + 1; // Search in right half
}
return -1; // Key not found
}
int main() {
int a[50], n, key, pos, i;
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter sorted array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Enter key element to search:\n");
scanf("%d", &key);
pos = binarySearch(a, n, key);
if (pos != -1)
printf("Element found at position %d\n", pos);
else
printf("Element not found\n");
return 0;
}
Enter number of elements:
6
Enter sorted array elements:
5 10 15 20 25 30
Enter key element to search:
20
Element found at position 4
Binary search requires the array to be sorted and works by repeatedly halving the search range. At each step, the middle element is compared with the key, and only the left or right half is kept for further searching, which greatly reduces the number of comparisons.
Because the search interval is divided by two on each iteration, binary search has much better performance than linear search on large sorted arrays, with time complexity proportional to log2(n).