Binary Search vs Linear Search Real Cost Difference
📖 Scenario: You are working in a library system that keeps a sorted list of book IDs. You want to find if a specific book ID is available. You will compare two ways to search: linear search (checking one by one) and binary search (dividing the list to find faster).
🎯 Goal: Build a Go program that creates a sorted list of book IDs, sets a target book ID to find, implements both linear search and binary search to find the target, and prints the results showing which method found the book and at what index.
📋 What You'll Learn
Create a sorted slice of integers called
bookIDs with these exact values: 101, 203, 305, 407, 509, 611, 713, 815, 917, 1020Create an integer variable called
targetID and set it to 713Write a function called
linearSearch that takes bookIDs and targetID and returns the index of targetID or -1 if not foundWrite a function called
binarySearch that takes bookIDs and targetID and returns the index of targetID or -1 if not foundPrint the results of both searches with clear messages
💡 Why This Matters
🌍 Real World
Searching sorted lists quickly is important in many systems like libraries, databases, and online stores to find items fast.
💼 Career
Understanding linear and binary search helps in optimizing search operations and improving software performance in real-world applications.
Progress0 / 4 steps