📖 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 array of page numbers.
📋 What You'll Learn
Create a sorted array of integers called pages with the exact values: 2, 5, 8, 12, 16, 23, 38, 56, 72, 91
Create an integer variable called target and set it to 23
Write a function called binary_search that takes the array, its size, and the target, and returns the index of the target or -1 if not found
Print the index returned by binary_search
💡 Why This Matters
🌍 Real World
Binary search is used in many real-world applications like searching in phone books, dictionaries, or databases where data is sorted.
💼 Career
Understanding binary search helps in optimizing search operations in software development, improving performance in data retrieval tasks.
Progress0 / 4 steps
1
Create the sorted array of page numbers
Create an integer array called pages with these exact values: 2, 5, 8, 12, 16, 23, 38, 56, 72, 91.
DSA C
Hint
Use int pages[] = { ... }; to create the array with the exact numbers.
2
Set the target page number
Create an integer variable called target and set it to 23 inside the main function.
DSA C
Hint
Inside main, write int target = 23;.
3
Write the binary search function
Write a function called binary_search that takes int arr[], int size, and int target as parameters. Use a divide and conquer approach with low, high, and mid variables to find the target. Return the index if found, or -1 if not found.
DSA C
Hint
Use a while loop with low and high to narrow down the search. Calculate mid and compare arr[mid] with target.
4
Call binary_search and print the result
Call binary_search with pages, its size 10, and target. Store the result in an integer variable called index. Then print index using printf.
DSA C
Hint
Call binary_search(pages, 10, target), save to index, then print index with printf("%d\n", index);.
Practice
(1/5)
1. What is the main idea behind Binary Search as a divide and conquer algorithm?
easy
A. It searches every element one by one until the target is found.
B. It divides the search range in half each time to find the target efficiently.
C. It sorts the array before searching for the target.
D. It uses recursion to add all elements in 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 search next.
Step 2: Identify the efficiency of halving the search space
By halving the search space each time, it quickly narrows down the target location.
Final Answer:
It divides the search range in half each time to find the target efficiently. -> Option B
Quick Check:
Divide and conquer = halving search space [OK]
Hint: Binary search halves the search space each step [OK]
Common Mistakes:
Thinking binary search checks every element
Confusing binary search with sorting
Believing binary search sums elements
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 safe middle index calculation
Using low + (high - low) / 2 prevents integer overflow when low and high are large.
Step 2: Compare with other options
int mid = (low + high) / 2; can overflow if low + high exceeds integer limit; options B and C are incorrect formulas.
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
Subtracting incorrectly in mid calculation
Multiplying low and high instead of adding
3. What will be the output of the following C code snippet?
int arr[] = {2, 4, 6, 8, 10};
int low = 0, high = 4, target = 6;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
printf("%d", mid);
break;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}