Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Classic Algorithms: Searching

Lesson 42 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

The Search for Data

Searching is one of the most fundamental tasks in computing. Whether you are looking for a name in a contact list or a specific record in a database, you are using a search algorithm.

1. Linear Search (Sequential Search)

The simplest way to search. You start at the beginning of the array and check every single element until you find a match or reach the end.

  • Time Complexity: O(n) - In the worst case, you check every item.
  • Best for: Small, unsorted arrays.
int linearSearch(int arr[], int size, int target) {
    for(int i = 0; i < size; i++) {
        if(arr[i] == target) return i; // Found!
    }
    return -1; // Not found
}

2. Binary Search (Divide and Conquer)

Much faster than linear search, but it ONLY works on sorted arrays. It works by repeatedly dividing the search interval in half.

  • How it works:
  1. Check the middle element.
  2. If it's the target, done!
  3. If the target is smaller, repeat on the left half.
  4. If the target is larger, repeat on the right half.
  • Time Complexity: O(log n). This is incredibly fast. Finding an item in a list of 1 million items takes only about 20 steps!

Linear vs. Binary Search

FeatureLinear SearchBinary Search
Array RequirementUnsorted or SortedMust be Sorted
ComplexityO(n)O(log n)
ImplementationVery EasyModerate
Always sort your data if you plan to search through it many times. The one-time cost of sorting will be paid back by the lightning speed of binary search!