0
0
Intro to Computingfundamentals~3 mins

Why Searching algorithms (linear, binary) in Intro to Computing? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could find anything in a huge list almost instantly, without checking every item?

The Scenario

Imagine you have a huge stack of books on your desk, and you want to find a specific one. You start from the top and check each book one by one until you find it.

The Problem

This manual search is slow and tiring, especially if the book is near the bottom. You might lose track or make mistakes, wasting time and effort.

The Solution

Searching algorithms like linear and binary search help computers find items quickly and accurately. Linear search checks items one by one, while binary search smartly cuts the search area in half each time, making the process much faster.

Before vs After
Before
for item in list:
    if item == target:
        return True
return False
After
left, right = 0, len(list) - 1
while left <= right:
    mid = (left + right) // 2
    if list[mid] == target:
        return True
    elif list[mid] < target:
        left = mid + 1
    else:
        right = mid - 1
return False
What It Enables

These algorithms let computers find information fast, even in huge collections, saving time and effort.

Real Life Example

When you search for a contact on your phone, the phone uses these algorithms to quickly find the right name from thousands of contacts.

Key Takeaways

Manual searching is slow and error-prone.

Linear search checks items one by one.

Binary search quickly narrows down the search area.