📖 Scenario: You have a sorted list of book page numbers where a special note is hidden. You want to find the page number quickly using a method that splits the search area in half each time.
🎯 Goal: Build a C++ program that uses binary search to find a target page number in a sorted list of page numbers.
📋 What You'll Learn
Create a sorted vector of integers called pages with exact values
Create two integer variables left and right to mark the search range
Implement the binary search logic using a while loop and divide the search range
Print the index of the target page if found, or -1 if not found
💡 Why This Matters
🌍 Real World
Binary search is used in many applications like searching in phone books, dictionaries, or any sorted data to find information quickly.
💼 Career
Understanding binary search helps in software development roles where efficient data searching and algorithm optimization are important.
Progress0 / 4 steps
1
Create the sorted list of page numbers
Create a std::vector<int> called pages with these exact values: 2, 5, 8, 12, 16, 23, 38, 56, 72, 91
DSA C++
Hint
Use std::vector<int> pages = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}; to create the sorted list.
2
Set up the search range variables
Create two integer variables called left and right. Set left to 0 and right to the last index of pages (which is 9).
DSA C++
Hint
Set left to 0 and right to 9 because the vector has 10 elements indexed 0 to 9.
3
Implement the binary search logic
Create an integer variable target and set it to 23. Use a while loop with the condition left <= right. Inside the loop, calculate mid as (left + right) / 2. If pages[mid] equals target, break the loop. If pages[mid] is less than target, set left to mid + 1. Otherwise, set right to mid - 1. Also create an integer variable result initialized to -1 and update it to mid when the target is found.
DSA C++
Hint
Use a while loop to repeatedly check the middle element and adjust left or right until you find the target or the range is empty.
4
Print the search result
Print the value of result using std::cout. This will show the index of the target page if found, or -1 if not found.
DSA C++
Hint
Use std::cout << result << std::endl; to print the index.
Practice
(1/5)
1. What is the main idea behind Binary Search as a divide and conquer algorithm?
easy
A. It divides the search space into two halves and discards one half each time.
B. It searches every element one by one until it finds the target.
C. It sorts the array before searching for the element.
D. It uses recursion to add elements to the array.
Solution
Step 1: Understand divide and conquer in binary search
Binary search splits the array into two halves and decides which half to continue searching based on the middle element.
Step 2: Identify the main action in binary search
It discards the half where the target cannot be, reducing the search space quickly.
Final Answer:
It divides the search space into two halves and discards one half each time. -> Option A
Quick Check:
Divide and conquer = split and discard half [OK]
Hint: Binary search halves the search space each step [OK]
Common Mistakes:
Thinking binary search checks every element
Confusing binary search with linear search
Assuming binary search sorts the array
2. Which of the following is the correct syntax to calculate the middle index in binary search in C++?
easy
A. int mid = (low + high) / 2;
B. int mid = (low * high) / 2;
C. int mid = low + (high - low) / 2;
D. int mid = (low - high) / 2;
Solution
Step 1: Understand why to avoid overflow
Using (low + high) / 2 can cause overflow if low and high are large integers.
Step 2: Use safe middle calculation
Calculating mid as low + (high - low) / 2 avoids overflow by subtracting before adding.
Final Answer:
int mid = low + (high - low) / 2; -> Option C
Quick Check:
Safe mid calculation = low + (high - low)/2 [OK]
Hint: Use low + (high - low)/2 to avoid overflow [OK]
Common Mistakes:
Using (low + high)/2 causing overflow
Using subtraction incorrectly in mid calculation
Multiplying low and high instead of adding
3. What will be the output of the following C++ code snippet?
int binarySearch(int arr[], int low, int high, int x) {
if (low > high) return -1;
int mid = low + (high - low) / 2;
if (arr[mid] == x) return mid;
else if (arr[mid] > x) return binarySearch(arr, low, mid - 1, x);
else return binarySearch(arr, mid + 1, high, x);
}
int main() {
int arr[] = {2, 4, 6, 8, 10};
int n = 5;
int result = binarySearch(arr, 0, n - 1, 6);
std::cout << result;
return 0;
}