ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at May 04 2019
·
0 comments

Instructions

Test suite

Solution

Implement a binary search algorithm.

Searching a sorted collection is a common task. A dictionary is a sorted list of word definitions. Given a word, one can find its definition. A telephone book is a sorted list of people's names, addresses, and telephone numbers. Knowing someone's name allows one to quickly find their telephone number and address.

If the list to be searched contains more than a few items (a dozen, say) a binary search will require far fewer comparisons than a linear search, but it imposes the requirement that the list be sorted.

In computer science, a binary search or half-interval search algorithm finds the position of a specified input value (the search "key") within an array sorted by key value.

In each step, the algorithm compares the search key value with the key value of the middle element of the array.

If the keys match, then a matching element has been found and its index, or position, is returned.

Otherwise, if the search key is less than the middle element's key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the search key is greater, on the sub-array to the right.

If the remaining array to be searched is empty, then the key cannot be found in the array and a special "not found" indication is returned.

A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time. A binary search is a dichotomic divide and conquer search algorithm.

To run the tests, run the command `dotnet test`

from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip`

property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using `exercism submit BinarySearch.cs`

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Wikipedia http://en.wikipedia.org/wiki/Binary_search_algorithm

```
// This file was auto-generated based on version 1.3.0 of the canonical data.
using System;
using Xunit;
public class BinarySearchTest
{
[Fact]
public void Finds_a_value_in_an_array_with_one_element()
{
var array = new[] { 6 };
var value = 6;
Assert.Equal(0, BinarySearch.Find(array, value));
}
[Fact(Skip = "Remove to run test")]
public void Finds_a_value_in_the_middle_of_an_array()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 6;
Assert.Equal(3, BinarySearch.Find(array, value));
}
[Fact(Skip = "Remove to run test")]
public void Finds_a_value_at_the_beginning_of_an_array()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 1;
Assert.Equal(0, BinarySearch.Find(array, value));
}
[Fact(Skip = "Remove to run test")]
public void Finds_a_value_at_the_end_of_an_array()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 11;
Assert.Equal(6, BinarySearch.Find(array, value));
}
[Fact(Skip = "Remove to run test")]
public void Finds_a_value_in_an_array_of_odd_length()
{
var array = new[] { 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634 };
var value = 144;
Assert.Equal(9, BinarySearch.Find(array, value));
}
[Fact(Skip = "Remove to run test")]
public void Finds_a_value_in_an_array_of_even_length()
{
var array = new[] { 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
var value = 21;
Assert.Equal(5, BinarySearch.Find(array, value));
}
[Fact(Skip = "Remove to run test")]
public void Identifies_that_a_value_is_not_included_in_the_array()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 7;
Assert.Equal(-1, BinarySearch.Find(array, value));
}
[Fact(Skip = "Remove to run test")]
public void A_value_smaller_than_the_arrays_smallest_value_is_not_found()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 0;
Assert.Equal(-1, BinarySearch.Find(array, value));
}
[Fact(Skip = "Remove to run test")]
public void A_value_larger_than_the_arrays_largest_value_is_not_found()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 13;
Assert.Equal(-1, BinarySearch.Find(array, value));
}
[Fact(Skip = "Remove to run test")]
public void Nothing_is_found_in_an_empty_array()
{
var array = Array.Empty<int>();
var value = 1;
Assert.Equal(-1, BinarySearch.Find(array, value));
}
[Fact(Skip = "Remove to run test")]
public void Nothing_is_found_when_the_left_and_right_bounds_cross()
{
var array = new[] { 1, 2 };
var value = 0;
Assert.Equal(-1, BinarySearch.Find(array, value));
}
}
```

```
ï»¿using System;
public static class BinarySearch
{
public static int Find(int[] input, int value)
{
if(input.Length == 0) return -1;
int minPoint = 0, maxPoint = input.Length -1;
do
{
int midPoint = (minPoint + maxPoint) / 2;
if (value > input[midPoint])
minPoint = midPoint +1;
else
maxPoint = midPoint -1;
if (input[midPoint] == value) return midPoint;
}
while(minPoint <= maxPoint);
return -1;
}
}
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,423 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments