Title: Binary Searching
1Binary Searching
By Meldie A. Apag
2Binary search is a procedure for searching an
element in an array. The array has to be sorted
in ascending order.
The basic idea is to compare the middle element
of the array with the element to be found.
3- The comparison can yield three results
- If the middle element is the same as the
- element to be found. In this case the
- search is successful and returns the index
- (field position) of the element to be
- found.
- -If the middle element is smaller than the
- element to be found, the search is then
- continued recursively to the right of the
- element.
4-the middle element is greater than the element
to be found, the search is then continued
recursively to the left of the element. Thus,
the size of the sublist being searched is
reduced by one half.
5Binary Search
- Algorithm
- Set the list to be the whole list.
- Find the middle value of the list.
- If the middle value is equal to the target then
we declare victory and stop. - If the middle item is less than the target, then
we set the new list to be the upper half of the
old list and we repeat from step 2 using the new
list. - If the middle value is greater than the target,
then we set the new list to be the bottom half of
the list, and we repeat from step 2 with the new
list.
6Pseudocode
- Initialize Found to false, First to 1 and Last to
n. - While not Found and First lt Last do the ff
- a. Calculate Loc (First Last) /2.
- b. If Item lt X loc then
- Set Last equal to Loc 1. / first half /
- Else if Item gt X loc then
- Set First equal to Loc 1. / second half
/ - Else
- Set Found to true.
7Binary Search in C Code
int BinarySearch(IntArrayType IntArray, int Low,
int High, int Target) int Mid,
Difference while (Low lt High)
Mid (Low High) / 2
Difference IntArrayMid - Target if
(Difference 0) // IntArrayMid
Target return Mid
else if (Difference lt 0) //
IntArrayMid lt Target Low
Mid 1 else High Mid - 1
return -1 // If reach here,
Target was not found.
8Binary Search in Java Code
1 int data 2 int size 3 4 public boolean
binarySearch(int key) 5 6 int low 0 7 int
high size - 1 8 9 while(high gt low) 10
int middle (low high) / 2
Line 1 tells us that we have an array of
integers called data. An array is just an ordered
list of values, just like the list we talked
about in our algorithm. An integer is a positive
or negative whole number.
91 int data 2 int size 3 4 public boolean
binarySearch(int key) 5 6 int low 0 7 int
high size - 1 8 9 while(high gt low) 10
int middle (low high) / 2
line 2 size tells us the number of items that we
have in the list. lines 4, 5 These lines tell
us that the code between line 5 and 22 performs
one task, and give the name binarySearch to the
task. key is the target item that we will search
for in data. The word boolean tells us that
linearSearch will return true if it finds the key
in the list, and it will return false if the key
is not in the list.
101 int data 2 int size 3 4 public boolean
binarySearch(int key) 5 6 int low 0 7 int
high size - 1 8 9 while(high gt low) 10
int middle (low high) / 2
line 6 low is the variable that tells us where
the beginning of the remaining list is, and we
give it an initial value of 0. line 7 high is
the variable that tells us where the end of the
remaining list is,and we give it an initial value
of the last thing in the list. line 9 This line
tells us to keep going until low is bigger than
or equal to high. line 10 middle we calculate so
we can divide the list into two pieces.
1111 if(datamiddle key) 12 return true 13
14 if(datamiddle lt key) 15 low middle
1 16 17 if(datamiddle gt key) 18 high
middle - 1 19 20 21 return false 22
line 11, 12, 13 We found the key, so we return
true. line 14, 15, 16 If the item in the middle
of the list is less than our key, we should look
for the key in the top half of the list, so we
calculate a new value for low. line 17, 18, 19
If the item in the middle of the list is greater
than our key, we should look for the key in the
bottom half of the list, so we calculate a new
value for high. line 21 If we get to this line
then we know that the key is not in the list so
we return false.