Back to Articles

Posted on: June 1, 2025
Last updated: June 1, 2025
algorithms
computer science
beginners
search
programming

Beginner's Guide to Search Algorithms

Hey there!

Have you ever lost your keys and had to search for them? Or maybe you've tried to find a specific word in a huge, heavy dictionary? In both of these moments, you're doing something computers do all the time: searching. But instead of searching blindly, computers use super smart, super-fast methods called search algorithms.

These algorithms are everywhere! They're the hidden engines behind how Google finds your search results in milliseconds, how your phone instantly pulls up a contact, or even how characters find their way around in your favorite video games. Pretty cool, right?

Understanding how these algorithms work is a massive step in becoming a solid developer. It's a cornerstone of computer science, and honestly, once you get it, you'll start seeing "search" in everything. Don't sweat it, though – we're going to break them down into bite-sized, easy-to-understand pieces.

Ready to dive in? Let's go!


So, What Exactly is a Search Algorithm?

Think of a search algorithm as a precise recipe for finding a specific item within a collection of stuff. You've got a bunch of items (like a list of numbers or names), and you need to locate one particular thing. The algorithm tells you exactly how to do that, step by step.

The big goal? Always the same: find what you're looking for as quickly and efficiently as possible!

Today, we're going to shine a spotlight on two of the most fundamental search algorithms. They're simple, but they'll teach you a ton about why how you search makes all the difference in your code's performance:

  1. Linear Search (or Sequential Search)
  2. Binary Search

1. Linear Search: The "Check Everything" Approach

Let's say you've got a playlist of your favorite songs, but they're all mixed up – no alphabetical order, nothing. You want to find "Bohemian Rhapsody." How would you do it?

You'd probably start from the very first song, listen for a second to see if it's "Bohemian Rhapsody." Nope? Okay, next song. You'd keep going, one by one, until you either hit your target or run out of songs.

Guess what? That's Linear Search in a nutshell!

How it Works

  1. Start at the beginning of your list.
  2. Check each item one by one.
  3. If you find the target, return its index.
  4. If not found after checking everything, return -1.

When to Use It

  • The list is small.
  • The list is unsorted.
  • Simplicity matters more than speed.

Performance: Big O Notation – O(N)

In the worst case, Linear Search checks every item:

  • 10 items = 10 checks
  • 1,000,000 items = 1,000,000 checks
    Its time grows linearly with the size of the input.

function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      return i; // Found it!
    }
  }
  return -1; // Not found
}

const movies = ["Interstellar", "Inception", "Dune", "Arrival", "Tenet"];
console.log(linearSearch(movies, "Inception")); // Output: 1
console.log(linearSearch(movies, "Blade Runner")); // Output: -1

def linear_search(arr, target):
    for i in range(len(arr)):
        if arr[i] == target:
            return i  # Found it!
    return -1  # Not found

movies = ["Interstellar", "Inception", "Dune", "Arrival", "Tenet"]
print(linear_search(movies, "Inception"))  # Output: 1
print(linear_search(movies, "Blade Runner"))  # Output: -1

2. Binary Search: The Smart Approach (with a Catch!)

Now imagine looking up a word in a dictionary. Do you start on page 1 and flip every page? Of course not. You flip to the middle, adjust based on the word you're looking for, and quickly narrow it down.

That’s Binary Search – a super fast divide-and-conquer method. But here's the catch: it only works on sorted data.

How it Works

  1. Look at the middle item.
  2. If it matches, you're done.
  3. If the target is greater, search the right half.
  4. If the target is smaller, search the left half.
  5. Repeat until found or list is empty.

When to Use It

  • The list is sorted.
  • The dataset is large.
  • You want the fastest search possible.

Performance: Big O Notation – O(log N)

Each step halves the list:

  • 100 items → ~7 checks
  • 1,000,000 items → ~20 checks Much faster than Linear Search as the list grows!

function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;

  while (left <= right) {
    let mid = Math.floor((left + right) / 2);

    if (arr[mid] === target) return mid;
    else if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }

  return -1;
}

const sortedNumbers = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91];
console.log(binarySearch(sortedNumbers, 23)); // Output: 5
console.log(binarySearch(sortedNumbers, 10)); // Output: -1

def binary_search(arr, target):
    left = 0
    right = len(arr) - 1

    while left <= right:
        mid = (left + right) // 2

        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1

    return -1

numbers = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
print(binary_search(numbers, 23))  # Output: 5
print(binary_search(numbers, 10))  # Output: -1

Wrapping Up: Why This Stuff Matters

Knowing search algorithms teaches you to:

  1. Think efficiently – Optimize how your code handles large data.
  2. Choose wisely – Know when simple is fine (Linear) vs. when fast matters (Binary).
  3. Build stronger foundations – These are stepping stones to more advanced topics.

Next time you search in a list or see a search bar, think about the smart logic working underneath. That could be your code one day!


🚀 Next Steps

  • Try writing these from scratch without copying!
  • Look into Graph Search: Breadth-First Search (BFS) and Depth-First Search (DFS).
  • Explore Hash Tables – an insanely fast way to search by jumping right to the answer!
  • Curious how search ties into data structures? Check out Linked Lists in Python – a beginner-friendly intro to one of the building blocks behind search and organization!