logo CodeStepByStep logo

BinarySearch

Language/Type: C# recursion return
Related Links:

Write a recursive method named BinarySearch that accepts a sorted list of integers and an integer target value and uses a recursive binary search algorithm to find and return an index at which that target value is found in the array. If the target value is not found in the list, return -1. The following code shows some example calls and their expected return values:

// index    0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
List list = new List {-4,  2,  7, 10, 15, 20, 22, 25, 30, 36, 42, 50, 56, 68, 85, 92, 103};

int index = BinarySearch(list, 42);   // 10
int index = BinarySearch(list, 66);   // -1

You should assume that the list's elements are already sorted; you do not need to handle the case of an unsorted list. Your method must be recursive and must use a binary search algorithm. Do not use loops or auxiliary data structures.

Method: Write a C# method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.