Linear Search Algorithm
📖 Scenario: You are helping a librarian find a specific book in a shelf. The shelf is a list of book titles. You will write a program to search for a book title by checking each book one by one until you find it.
🎯 Goal: Build a simple linear search program that looks for a given book title in a list of books and tells if it is found or not.
📋 What You'll Learn
Create an array called
books with exact titles: 'The Hobbit', '1984', 'Pride and Prejudice', 'To Kill a Mockingbird', 'The Great Gatsby'Create a variable called
searchTitle with the exact value '1984'Use a
for loop with variable index to go through booksInside the loop, use an
if statement to check if books[index] equals searchTitleCreate a variable called
found set to false before the loop and set it to true when the book is foundAfter the loop, print
'Found' if found is true, otherwise print 'Not Found'💡 Why This Matters
🌍 Real World
Searching for an item in a list is a common task, like finding a book on a shelf or a contact in a phone.
💼 Career
Understanding linear search helps in many programming jobs where you need to find data without special indexing or sorting.
Progress0 / 4 steps