← Back to Games

🔍 Searching Algorithms

Put the steps in the correct order

🔎 What is Searching?

Searching is the process of finding a specific item within a collection of data. Unlike sorting (which rearranges data), searching looks through existing data to locate a target value. Different search algorithms have different requirements and speeds.

Linear Search
Binary Search

➡️ Linear Search

Linear Search (also called Sequential Search) is the simplest search algorithm. It checks each element in the list one by one from the beginning until it finds the target or reaches the end.

How It Works

  1. Start at the first element (index 0)
  2. Compare the current element with the target value
  3. If they match, return the position — search successful!
  4. If not, move to the next element
  5. Repeat until found or end of list reached
  6. If end reached without finding, return "not found"

Key Characteristics

  • Works on both sorted and unsorted lists
  • Simple to implement and understand
  • No preprocessing of data required
  • Best for small lists or when searching only once

Example: Finding 7 in [3, 8, 2, 7, 1, 9]

Step 1: Check index 0: 3 ≠ 7, continue
Step 2: Check index 1: 8 ≠ 7, continue
Step 3: Check index 2: 2 ≠ 7, continue
Step 4: Check index 3: 7 = 7 ✓ FOUND at position 3!

Advantages & Disadvantages

✅ Advantages: Simple, works on unsorted data, no preprocessing needed.

❌ Disadvantages: Slow for large lists, checks every element in worst case.

🎯 Correct Order

📦 Steps

🎯 Binary Search

Binary Search is a highly efficient "divide and conquer" search algorithm. It works by repeatedly dividing the search space in half.

⚠️ Important: Binary Search ONLY works on SORTED lists! The data must be in order before you can use this algorithm.

How It Works

  1. Find the middle element of the list
  2. Compare the middle element with the target
  3. If they match, return the position — found!
  4. If target is smaller, search the LEFT half (discard right)
  5. If target is larger, search the RIGHT half (discard left)
  6. Repeat until found or search space is empty

Why Is It So Fast?

Each comparison eliminates half of the remaining elements. For a list of 1000 elements:

  • Linear Search: up to 1000 comparisons
  • Binary Search: maximum ~10 comparisons

Example: Finding 7 in [1, 3, 5, 7, 9, 11, 13]

Step 1: Middle = 7 (index 3). Compare: 7 = 7 ✓ FOUND!
(If searching for 3 instead...)
Step 1: Middle = 7. Target 3 < 7, search LEFT half [1, 3, 5]
Step 2: Middle = 3. Target 3 = 3 ✓ FOUND!

Advantages & Disadvantages

✅ Advantages: Very fast, efficient for large datasets, ideal for repeated searches.

❌ Disadvantages: List MUST be sorted first, more complex than linear search, sorting has its own cost.

🤔 When to Use Which?

Use Linear Search when: List is small, list is unsorted, you're only searching once.

Use Binary Search when: List is large, list is already sorted, you're searching multiple times.

🎯 Correct Order

📦 Steps