📖 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 array of book titles, set a target book title to find, implement binary search to check if the book is in the list, and print the result.
📋 What You'll Learn
Create a sorted array of 7 book titles in alphabetical order
Create a variable called target with the exact book title to search
Write a function called binary_search that takes the array, its size, and the target title, and returns the index if found or -1 if not
Print the index returned by binary_search
💡 Why This Matters
🌍 Real World
Binary search is used in many apps and systems to quickly find data like names, files, or records when the data is sorted.
💼 Career
Understanding binary search and sorted data is essential for software developers to write efficient search functions and optimize performance.
Progress0 / 4 steps
1
Create a sorted array of book titles
Create a sorted array of 7 book titles called books with these exact titles in this order: "Algorithms", "Data Structures", "Design Patterns", "Operating Systems", "Programming", "Software Engineering", "Systems Programming".
DSA C
Hint
Use an array of strings with the exact titles in alphabetical order.
2
Set the target book title to search
Create a variable called target and set it to the exact string "Programming".
DSA C
Hint
Use a string pointer variable named target with the exact value.
3
Write the binary search function
Write a function called binary_search that takes char *arr[], int size, and char *target. Use a loop with variables low, high, and mid to find target in arr using strcmp. Return the index if found, or -1 if not found.
DSA C
Hint
Use strcmp to compare strings and adjust low and high accordingly.
4
Print the result of binary search
Call binary_search with books, 7, and target. Store the result in index. Print index using printf with format "%d\n".
DSA C
Hint
Call binary_search and print the returned index.
Practice
(1/5)
1. Why is it important for a list to be sorted before applying binary search?
easy
A. Because binary search can only search in lists with odd number of elements
B. Because binary search works only on lists with duplicate elements
C. Because binary search divides the list and compares middle elements to find the target efficiently
D. Because binary search requires the list to be reversed
Solution
Step 1: Understand binary search working
Binary search works by repeatedly dividing the sorted list and comparing the middle element to the target value.
Step 2: Importance of sorted order
Sorted order allows us to decide which half to discard after each comparison, making search efficient.
Final Answer:
Because binary search divides the list and compares middle elements to find the target efficiently -> Option C
Quick Check:
Sorted list needed for binary search = Because it divides and compares middle elements [OK]
Hint: Binary search needs sorted list to halve search space fast [OK]
Common Mistakes:
Thinking binary search works on unsorted lists
Assuming binary search needs duplicates
Believing binary search requires odd length lists
2. Which of the following is the correct syntax to start a binary search loop in C for an array arr of size n?
easy
A. int low = 1, high = n - 1; while (low <= high) { ... }
B. int low = 0, high = n - 1; while (low <= high) { ... }
C. int low = 0, high = n; while (low < high) { ... }
D. int low = 1, high = n; while (low < high) { ... }
Solution
Step 1: Identify correct index range for array
Array indices in C start at 0 and end at n-1, so low = 0 and high = n-1.
Step 2: Correct loop condition for binary search
Loop continues while low is less than or equal to high to cover all elements.
Final Answer:
int low = 0, high = n - 1; while (low <= high) { ... } -> Option B
Quick Check:
Array indices 0 to n-1 and loop while low <= high [OK]
Hint: Array indices start at 0; loop while low ≤ high [OK]
Common Mistakes:
Starting low at 1 instead of 0
Using high = n instead of n-1
Using < instead of <= in loop condition
3. What will be the output of the following C code snippet?
int arr[] = {2, 4, 6, 8, 10};
int low = 0, high = 4, mid;
int target = 6;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == target) {
printf("Found at index %d", mid);
break;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
medium
A. Found at index 1
B. Found at index 3
C. Not found
D. Found at index 2
Solution
Step 1: Initialize variables and first mid calculation
Since arr[mid] == target, print "Found at index 2" and break loop.
Final Answer:
Found at index 2 -> Option D
Quick Check:
Target 6 found at index 2 [OK]
Hint: Calculate mid as (low+high)/2 and compare to target [OK]
Common Mistakes:
Calculating mid incorrectly
Off-by-one errors in index
Printing wrong index
4. Identify the error in this binary search code snippet and choose the correct fix:
int arr[] = {1, 3, 5, 7, 9};
int low = 0, high = 5, mid;
int target = 7;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == target) {
printf("Found at %d", mid);
break;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
medium
A. Change high to 4 instead of 5
B. Change low to 1 instead of 0
C. Change mid calculation to mid = (low - high) / 2
D. No error, code is correct
Solution
Step 1: Check array index bounds
Array size is 5, so highest index is 4. Setting high=5 causes out-of-bounds access.
Step 2: Fix high initialization
Set high = 4 to stay within valid indices and avoid runtime errors.
Final Answer:
Change high to 4 instead of 5 -> Option A
Quick Check:
Array indices 0 to 4, high must be 4 [OK]
Hint: High index must be last valid index (size-1) [OK]
Common Mistakes:
Setting high to array size instead of size-1
Changing low incorrectly
Incorrect mid calculation
5. Given a sorted array arr = {1, 3, 5, 7, 9, 11, 13}, you want to find the smallest element greater than or equal to 8 using binary search. Which approach correctly finds this element?
hard
A. Modify binary search to move high = mid - 1 when arr[mid] >= 8, else low = mid + 1
B. Use normal binary search to find 8 directly; if not found, return -1
C. Sort the array again before binary search
D. Use linear search instead of binary search
Solution
Step 1: Understand the problem
We want the smallest element >= 8, which may not be exactly 8 in the array.
Step 2: Modify binary search for lower bound
When arr[mid] >= 8, move high to mid - 1 to find smaller candidates; else move low to mid + 1.
Step 3: After loop, low points to smallest element >= 8
This approach finds the correct element (9 at index 4).
Final Answer:
Modify binary search to move high = mid - 1 when arr[mid] >= 8, else low = mid + 1 -> Option A
Quick Check:
Lower bound binary search finds smallest element ≥ target [OK]
Hint: Adjust binary search to find lower bound by moving high on >= [OK]
Common Mistakes:
Using normal binary search without modification
Sorting array again unnecessarily
Choosing linear search over binary search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.