C Searching Algorithms

Intermediate
12 min read
Updated: Nov 2026
1 example
6 guides

Definition and Usage

Searching finds an element (or its position) in a collection. Linear search checks items one by one and works on any array in O(n). Binary search repeatedly halves a sorted array and finds an item in O(log n). This section covers the search contract, linear search, iterative and recursive binary search, and how to choose between them.

Sorted data unlocks speed.

Binary search is only valid on sorted data. Sorting costs O(n log n), so it pays off when you search many times.

Learning Path

Work through the stages in order, or jump to a topic from the index below (6 tutorials).

C Examples

A quick taste of searching. Compile with gcc file.c -std=c11 -Wall -o out.

Example 1 — Search and return an index

The usual contract: the index, or -1 when absent.

C
#include <stdio.h>

static int find(const int a[], int n, int key) {
    for (int i = 0; i < n; i++)
        if (a[i] == key) return i;
    return -1;
}

int main(void) {
    int a[] = {10, 20, 30, 40, 50};
    printf("index of 40: %d\n", find(a, 5, 40));
    printf("index of 35: %d\n", find(a, 5, 35));
    return 0;
}

How It Works

  1. The function returns the position of the first match.
  2. It returns -1 because -1 is never a valid index.
  3. Callers must check for -1 before using the result.

C Searching Algorithms Topics

Every guide in this section with a one-line summary (6 tutorials).

Search methods

5 tutorials

Linear and binary search in C.

TopicDescriptionTutorial
Searching basicsDefine return values and choose between sequential and interval searching.Try it
Linear searchScan the array in order; find first, last, and count matches.Try it
Binary searchTrace how binary search narrows the range in O(log n).Try it
Iterative binary searchA reusable loop-based binary search plus a first-occurrence variant.Try it
Recursive binary searchExpress binary search as a function that calls itself on a smaller range.Try it

Comparison

1 tutorial

Choose a search strategy.

TopicDescriptionTutorial
Searching comparisonsCount comparisons for linear and binary search on the same data.Try it

Edge Cases & Pitfalls

Common mistakes when learning searching in C.

sorted

Binary search on unsorted data

It silently returns wrong answers. Sort first or use linear search.

overflow

Overflowing the midpoint

Compute mid = lo + (hi - lo) / 2, not (lo + hi) / 2.

neg1

Ignoring the -1 result

Using -1 as an index is out-of-bounds access.

Key Takeaways

  • Linear: any data, O(n).
  • Binary: sorted data, O(log n).
  • Return -1 for not found.

One line: Searching finds an element (or its position) in a collection.

Frequently Asked Questions

Linear search scans items in order and works on any data; binary search halves a sorted range each step and is much faster on large sorted arrays.
Yes, bsearch() in <stdlib.h> searches a sorted array using a comparison function.
Binary search is O(log n) versus O(n) for linear search, but it requires sorted data.

Next: Searching Basics in C

Define return values and choose between sequential and interval searching.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful