Understanding Linear Search — My Learning Journey

kasumbi philkasumbi phil
2 min read

Today, I continued my deep dive into data structures by learning one of the most basic searching algorithms: Linear Search.


Linear Search is a simple technique used to find a specific value in a list or array.

It works like this:

  • Start at the beginning of the list.

  • Go through each item one by one.

  • If you find the number you're looking for, return its position (index).

  • If it’s not found, give a message like “Item not found”.

It’s just like flipping through pages of a notebook to find a word — you check each page until you find it.


🧪 My Python Implementation

Here’s the code I wrote:

def linear_search(arr, n):
    l = len(arr)

    for i in range(l):
        if arr[i] == n:
            return i

    return "Item not found"

arr = [23, 36, 48, 89, 59, 3, 8, 9, 1]
print(linear_search(arr, 1))  # Output: 8

✅ What I Learned

  • Linear search is beginner-friendly and doesn’t require sorting.

  • It’s best for small lists or unsorted data.

  • It returns the index of the item if found.

  • If the item isn’t found, we can return or print a helpful message.


🚀 Up Next

I’m excited to try out Binary Search next — a much faster method, but it needs the list to be sorted.

0
Subscribe to my newsletter

Read articles from kasumbi phil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

kasumbi phil
kasumbi phil