What are the different searching techniques in C language?

Searching techniques in C refer to algorithms used to find a specific element (key) within a collection of data. These techniques are fundamental in computer programming and data structures.

  • Successful search − The key element is found in the list
  • Unsuccessful search − The key element is not present in the list

C language provides two primary searching techniques −

  • Linear Search − Sequential search through elements
  • Binary Search − Divide-and-conquer approach for sorted arrays

Linear Search

Linear search is the simplest searching algorithm that checks each element sequentially until the target is found or the list ends.

Characteristics

  • Works on both sorted and unsorted arrays
  • Time complexity: O(n)
  • Space complexity: O(1)
  • Simple to implement but inefficient for large datasets

Syntax

int linearSearch(int arr[], int n, int key);

Example: Linear Search Implementation

#include <stdio.h>

int linearSearch(int arr[], int n, int key) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == key) {
            return i; /* Return index if found */
        }
    }
    return -1; /* Return -1 if not found */
}

int main() {
    int arr[] = {12, 45, 13, 67, 78};
    int n = 5;
    int key = 67;
    
    int result = linearSearch(arr, n, key);
    
    if (result != -1) {
        printf("Element %d found at index %d
", key, result); } else { printf("Element %d not found
", key); } return 0; }
Element 67 found at index 3

Binary Search

Binary search is an efficient algorithm that works on sorted arrays by repeatedly dividing the search space in half.

Characteristics

  • Requires sorted array
  • Time complexity: O(log n)
  • Space complexity: O(1) for iterative approach
  • Much faster than linear search for large datasets

Example: Binary Search Implementation

#include <stdio.h>

int binarySearch(int arr[], int n, int key) {
    int left = 0, right = n - 1;
    
    while (left <= right) {
        int mid = left + (right - left) / 2;
        
        if (arr[mid] == key) {
            return mid;
        } else if (arr[mid] < key) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1;
}

int main() {
    int arr[] = {12, 25, 37, 48, 59, 67, 78};
    int n = 7;
    int key = 48;
    
    int result = binarySearch(arr, n, key);
    
    if (result != -1) {
        printf("Element %d found at index %d
", key, result); } else { printf("Element %d not found
", key); } return 0; }
Element 48 found at index 3

Comparison

Aspect Linear Search Binary Search
Array Type Sorted/Unsorted Must be sorted
Time Complexity O(n) O(log n)
Space Complexity O(1) O(1)
Best for Small datasets Large datasets

Conclusion

Linear search is simple and works on any array, while binary search is efficient but requires sorted data. Choose linear search for small or unsorted arrays, and binary search for large sorted datasets.

Updated on: 2026-03-15T13:11:59+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements