Sei sulla pagina 1di 5

Sarah Bundy

Applying the Binary Search Algorithm to a Set of Integers


Introduction
In programming, algorithms are very important. Algorithms are the steps that a program takes to
complete an operation. The binary search algorithm is an algorithm that is efficient when
searching for a particular value within a set of values in a sorted set. The algorithm repeatedly
divides the set in half and compares the middle value to the target value. This algorithm is
extremely useful and can be implemented in various programs.

Purpose
These instructions explain the concept of a binary search algorithm by providing a step-by-step
walkthrough of how to apply this algorithm to a given set of twenty-four random integers
between zero and one hundred. After completing the instructions, the user should understand the
binary search algorithm and be able to apply it to any set.

Note: The instructions assume that the readers know how to put integers into numerical order,
can compare the values of two integers, and can perform basic addition and division.

Materials:

Blank paper
A writing utensil

Unsorted Set of Integers:

Integer 10 50 100 12 17 2 47 22 1 33 93 74 91 34 94 80 5 79 54 88 61 11 77 15
Value
Index 0 1 2
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Note: The index keeps track of the location of the value within the set. In Computer Science,
indexes start at zero and continue until one less than the amount of values in the set.

Target Value: 10

Sarah Bundy
1. Draw the following table on your paper.
Integer
Value
Index 0

5 6

8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

2. List the integer values in numerical order from lowest to highest, since the numbers are
not sorted in the correct order. Do not change the order of the indexes.
Integer 1 2 5 10 11 12 15 17 22 33 34 47 50 54 61 74 77 79 80 88 91 93 94 100
Value
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Note: Putting the numbers in numerical order from highest to lowest will yield the same
results.

3. Set the minimum value to the value at index zero and the maximum value to the value at
index twenty-three.
Integer 1
2 5 10 11 12 15 17 22 33 34 47 50 54 61 74 77 79 80 88 91 93 94 100
Value
Index 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Min
Max

4. Divide the set in half by finding the average of the minimum and maximum indexes by
adding them together and dividing by 2.
(0 + 23) / 2 = (23) / 2 = 11
Note: If the average is a decimal, round down.
Integer 1
2 5 10 11 12 15 17 22 33 34 47
50 54 61 74 77 79 80 88 91 93 94 100
Value
Index 0
1 2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21 22 23
Min
Half
Max

Sarah Bundy
5. Compare the value at the halfway point, index eleven, to the target value to see which is
greater.
47 > 10

6. Since forty-seven is greater than ten and the set is sorted, ten must be located in the first
half of the set. Set maximum to one less than the middle index, eleven.
Integer 1
2 5 10 11 12 15 17 22 33 34
Value
Index 0
1 2 3 4 5 6 7 8 9 10
Min
Max
Note: We know that twelve is not in the second half of the set, so we can just focus on
the first half.

7. Divide the set in half by finding the average of the minimum and maximum indexes by
adding them together and dividing by 2.
(0 + 10) / 2 = (10) / 2 = 5
Integer 1
2 5 10 11 12
15 17 22 33 34
Value
Index 0
1 2 3 4 5
6 7 8 9 10
Min
Half
Max

8. Compare the value at the halfway point, index 5, to the target value to see which is
greater.
12 > 10

9. Since twelve is greater than ten and the set is sorted, ten must be located in the first half
of the set. Set maximum to one less than the middle index, five.
Integer 1
2 5 10 11
Value
Index 0
1 2 3 4
Min
Max

Sarah Bundy

10. Divide the set in half by finding the average of the minimum and maximum indexes by
adding them together and dividing by 2.
(0 + 4) / 2 = (4) / 2 = 2
Integer 1
2 5
10 11
Value
Index 0
1 2
3 4
Min
Half
Max

11. Compare the value at the halfway point, index 2, to the target value to see which is
greater.
10 > 5

12. Since ten is greater than five and the set is sorted, ten must be located in the second half
of the set. Set minimum to one more than the middle index, two.
Integer 10
11
Value
Index 3
4
Min Max

13. Divide the set in half by finding the average of the minimum and maximum indexes by
adding them together and dividing by 2.
(3 + 4) / 2 = (7) / 2 = 3
Integer 10
11
Value
Index 3
4
Min and Half Max

Sarah Bundy
14. Compare the value at the halfway point, index 3, to the target value to see which is
greater.
10 = 10

15. Since ten is equal to ten, we have found the target value.

Note: The binary search method can be used to search for any target value in a sorted set. The
minimum and maximum indexes and the half of the set that we focus on will change depending
on the values in the set, the target value, and the size of the set.

Note: The number of times that steps are repeated will vary depending on the location of the
target value in the set. If the target value is not in the set, the process will repeat until the
minimum and maximum indexes refer to the same value.

Potrebbero piacerti anche