Introduction
You have stumbled upon a group of mathematicians who are also singer-songwriters.
They have written a song for each of their favorite numbers, and, as you can imagine, they have a lot of favorite numbers (like 0 or 73 or 6174).
You are curious to hear the song for your favorite number, but with so many songs to wade through, finding the right song could take a while.
Fortunately, they have organized their songs in a playlist sorted by the title — which is simply the number that the song is about.
You realize that you can use a binary search algorithm to quickly find a song given the title.
Instructions
Instructions
Your task is to implement a binary search algorithm.
A binary search algorithm finds an item in a list by repeatedly splitting it in half, only keeping the half which contains the item we’re looking for.
It allows us to quickly narrow down the possible locations of our item until we find it, or until we’ve eliminated all possible locations.
Binary search only works when a list has been sorted.
The algorithm looks like this:
- Find the middle element of a sorted list and compare it with the item we’re looking for.
- If the middle element is our item, then we’re done!
- If the middle element is greater than our item, we can eliminate that element and all the elements after it.
- If the middle element is less than our item, we can eliminate that element and all the elements before it.
- If every element of the list has been eliminated then the item is not in the list.
- Otherwise, repeat the process on the part of the list that has not been eliminated.
Here’s an example:
Let’s say we’re looking for the number 23 in the following sorted list: [4, 8, 12, 16, 23, 28, 32].
- We start by comparing 23 with the middle element, 16.
- Since 23 is greater than 16, we can eliminate the left half of the list, leaving us with
[23, 28, 32].
- We then compare 23 with the new middle element, 28.
- Since 23 is less than 28, we can eliminate the right half of the list:
[23].
- We’ve found our item.
Dig Deeper
Loop with switch
Loop with a switch
// Package binarysearch is a small library for finding an element in an ordered sequence
package binarysearch
// SearchInts searches for an element in an ordered sequence and returns its index or -1 if not found
func SearchInts(list []int, key int) int {
for left, right := 0, len(list); left != right; {
mid := (left + right) / 2
switch {
case list[mid] == key:
return mid
case key < list[mid]:
right = mid
case key > list[mid]:
left = mid + 1
}
}
return -1
}
This approach starts by initializing a for loop.
The left is initialized to 0 and the right is initialized to the length of the slice of ints passed in.
The loop keeps iterating while left does not equal right.
The middle value is set by left plus right divided by 2.
For example, if left is 0 and right is 10, then the middle is calculated to 5.
if left is 6 and right is 10, then the middle is calculated to 8.
A switch with no condition is used to check the value of the element whose index in the slice of ints is the middle value.
-
If the element at the index of the middle value matches the value being searched for, then the middle value is returned.
-
If the value being searched for is less than the element at the index of the middle value, then right is set to the middle value
so that the next iteration will look at lower numbers.
-
Otherwise, the value being searched is greater than the element at the index of the middle value, so left is set to the middle value
plus one so that the next iteration will look for higher numbers.
If left and right are changed during the iterations so that they equal other, then the value being searched for is not in the slice of ints.
The loop exits and -1 is returned from the function.
Source: Exercism go/binary-search