0
0
DSA Goprogramming~30 mins

Binary Search vs Linear Search Real Cost Difference in DSA Go - Build Both Approaches

Choose your learning style9 modes available
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, 1020
Create an integer variable called targetID and set it to 713
Write a function called linearSearch that takes bookIDs and targetID and returns the index of targetID or -1 if not found
Write a function called binarySearch that takes bookIDs and targetID and returns the index of targetID or -1 if not found
Print 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
1
Create the sorted list of book IDs
Create a slice of integers called bookIDs with these exact values: 101, 203, 305, 407, 509, 611, 713, 815, 917, 1020
DSA Go
Hint

Use bookIDs := []int{...} with the exact numbers inside the braces.

2
Set the target book ID to find
Create an integer variable called targetID and set it to 713
DSA Go
Hint

Use targetID := 713 inside the main function.

3
Write linearSearch and binarySearch functions
Write a function called linearSearch that takes bookIDs and targetID and returns the index of targetID or -1 if not found. Also write a function called binarySearch that takes bookIDs and targetID and returns the index of targetID or -1 if not found.
DSA Go
Hint

Use a for loop with range for linear search. For binary search, use low, high, and mid indexes to divide the list.

4
Print the search results
Print the results of both searches using fmt.Println. Print exactly these two lines: "Linear Search: Found at index" followed by the linear search result, and "Binary Search: Found at index" followed by the binary search result.
DSA Go
Hint

Call both search functions with bookIDs and targetID. Store results in variables and print with fmt.Println exactly as shown.