📖 Scenario: You have a sorted list of numbers representing book IDs in a library. You want to find if a specific book ID exists using a fast method.
🎯 Goal: Build a recursive binary search function in C to find a book ID in the sorted list.
📋 What You'll Learn
Create an array called books with these exact values: 2, 5, 8, 12, 16, 23, 38, 56, 72, 91
Create two integer variables low and high to represent the start and end indexes of the array
Write a recursive function binary_search that takes the array, low, high, and the target book ID as parameters
Print the result index returned by binary_search
💡 Why This Matters
🌍 Real World
Binary search is used in many software systems to quickly find data in sorted lists, like searching for a book in a library database.
💼 Career
Understanding binary search and recursion is fundamental for software developers working with efficient data retrieval and algorithm design.
Progress0 / 4 steps
1
Create the sorted array of book IDs
Create an integer array called books with these exact values: 2, 5, 8, 12, 16, 23, 38, 56, 72, 91
DSA C
Hint
Use int books[] = { ... }; to create the array with the exact numbers.
2
Set the low and high index variables
Create two integer variables called low and high. Set low to 0 and high to 9 (the last index of the array).
DSA C
Hint
Remember the first index is 0 and the last index is 9 for 10 elements.
3
Write the recursive binary search function
Write a recursive function called binary_search that takes parameters: int arr[], int low, int high, and int target. It should return the index of target if found, or -1 if not found. Use the binary search logic with recursion.
DSA C
Hint
Use the middle index to compare and call the function recursively on the left or right half.
4
Call the function and print the result
Call binary_search with books, low, high, and target 23. Print the returned index using printf.
DSA C
Hint
The index of 23 in the array is 5. Use printf("%d\n", result); to print it.
Practice
(1/5)
1. What is the main idea behind the binary search recursive approach when searching for an element in a sorted array?
easy
A. Check every element one by one from start to end
B. Divide the array into halves and search recursively in the half where the element can be
C. Sort the array first and then search linearly
D. Use a queue to store elements and search iteratively
Solution
Step 1: Understand binary search principle
Binary search works by dividing the sorted array into two halves and deciding which half to search next based on comparison with the middle element.
Step 2: Apply recursion in binary search
The recursive approach calls the same function on the half where the target element might be, reducing the search space each time.
Final Answer:
Divide the array into halves and search recursively in the half where the element can be -> Option B
5. Given a sorted array arr = {1, 3, 5, 7, 9, 11, 13}, you want to find the index of element 8 using recursive binary search. What will be the sequence of mid indices checked before concluding the element is not found?
hard
A. [3, 5, 4]
B. [3, 1, 0]
C. [3, 1, 2]
D. [3, 4, 5]
Solution
Step 1: Calculate first mid index
Initial low=0, high=6, mid = (0+6)/2 = 3, arr[3]=7 less than 8, search right half next.
Step 2: Calculate second mid index
New low=4, high=6, mid = (4+6)/2 = 5, arr[5]=11 greater than 8, search left half next.
Step 3: Calculate third mid index
New low=4, high=4, mid = (4+4)/2 = 4, arr[4]=9 greater than 8, search left half next low=4, high=3 ends recursion.
Final Answer:
[3, 5, 4] -> Option A
Quick Check:
Mid indices checked = 3, 5, 4 [OK]
Hint: Track low, high, mid updates carefully each recursion [OK]
Common Mistakes:
Not updating low and high correctly
Miscomputing mid index
Stopping search too early
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.