📖 Scenario: You are working on a library system that needs to quickly find if a book ID exists in a sorted list of book IDs.To do this efficiently, you will use the binary search algorithm implemented with recursion.
🎯 Goal: Build a recursive binary search function in C# that checks if a given book ID is present in a sorted array of book IDs.
📋 What You'll Learn
Create a sorted integer array called bookIds with the exact values: 101, 203, 305, 407, 509, 611, 713
Create two integer variables start and end to represent the search range
Write a recursive function called BinarySearch that takes bookIds, start, end, and target as parameters and returns a boolean indicating if target is found
Call BinarySearch with the full range and the target book ID 407
Print "Found" if the book ID is found, otherwise print "Not Found"
💡 Why This Matters
🌍 Real World
Binary search is used in many applications like searching in databases, libraries, and any sorted data to quickly find information.
💼 Career
Understanding binary search and recursion is important for software developers to write efficient search algorithms and optimize performance.
Progress0 / 4 steps
1
Create the sorted array of book IDs
Create a sorted integer array called bookIds with these exact values: 101, 203, 305, 407, 509, 611, 713
DSA C#
Hint
Use int[] bookIds = { ... }; to create the array with the exact values.
2
Set up the start and end indices for the search
Create two integer variables called start and end. Set start to 0 and end to the last index of bookIds.
DSA C#
Hint
Set start to 0 and end to bookIds.Length - 1.
3
Write the recursive binary search function
Write a recursive function called BinarySearch that takes parameters int[] bookIds, int start, int end, and int target. The function should return true if target is found in bookIds between start and end, otherwise false. Use the binary search logic with recursion.
DSA C#
Hint
Use the middle index to compare with target. Call BinarySearch recursively on the left or right half.
4
Call the function and print the result
Call the BinarySearch function with bookIds, start, end, and the target value 407. Print "Found" if the function returns true, otherwise print "Not Found".
DSA C#
Hint
Call BinarySearch with the target 407. Use an if statement to print "Found" or "Not Found".
Practice
(1/5)
1. What is the main idea behind the Binary Search Recursive Approach when searching for a value in a sorted array?
easy
A. Use a queue to store elements and search iteratively.
B. Check every element one by one from start to end.
C. Sort the array first and then search linearly.
D. Divide the array into halves and search recursively in the half where the value can be.
Solution
Step 1: Understand binary search logic
Binary search works by dividing the sorted array into two halves and deciding which half to search next based on the middle element.
Step 2: Recognize recursive approach
The recursive approach calls the same search function on the half where the target value may exist, reducing the search space each time.
Final Answer:
Divide the array into halves and search recursively in the half where the value can be. -> Option D