0
0
DSA Goprogramming~30 mins

Why Binary Search and What Sorted Order Gives You in DSA Go - See It Work

Choose your learning style9 modes available
Why Binary Search and What Sorted Order Gives You
📖 Scenario: Imagine you have a list of book titles sorted alphabetically on a shelf. You want to find if a certain book is there quickly without checking every book one by one.
🎯 Goal: You will create a sorted list of book titles, set a target book to find, use binary search to check if the book is in the list, and print the result.
📋 What You'll Learn
Create a sorted slice of strings called books with these exact titles: "Algorithms", "Data Structures", "Go Programming", "Networking", "Operating Systems"
Create a string variable called target and set it to "Go Programming"
Write a function called binarySearch that takes a slice of strings and a target string, and returns a boolean indicating if the target is found using binary search
Use the binarySearch function to check if target is in books
Print "Found" if the book is in the list, otherwise print "Not Found"
💡 Why This Matters
🌍 Real World
Binary search is used in many apps and systems to quickly find data like names, files, or numbers when the data is sorted.
💼 Career
Understanding binary search and sorted data is important for software developers to write efficient search features and optimize performance.
Progress0 / 4 steps
1
Create the sorted list of books
Create a slice of strings called books with these exact titles in this order: "Algorithms", "Data Structures", "Go Programming", "Networking", "Operating Systems"
DSA Go
Hint

Use books := []string{...} with the exact titles in the given order.

2
Set the target book to find
Create a string variable called target and set it to "Go Programming"
DSA Go
Hint

Use target := "Go Programming" to set the book you want to find.

3
Write the binary search function
Write a function called binarySearch that takes a slice of strings called arr and a string target, and returns a boolean. Use binary search logic to check if target is in arr. Use variables low, high, and mid for indexes.
DSA Go
Hint

Use a loop with low and high indexes, calculate mid, compare arr[mid] with target, and adjust low or high accordingly.

4
Use binary search and print the result
Use the binarySearch function to check if target is in books. Print "Found" if true, otherwise print "Not Found".
DSA Go
Hint

Call binarySearch(books, target). Use fmt.Println("Found") if true, else fmt.Println("Not Found").