Binary Searching Earl Paine Stu Schwartz Earl Paine Stu Schwartz © Copyright 2005, used by...
-
Upload
sibyl-brown -
Category
Documents
-
view
215 -
download
0
Transcript of Binary Searching Earl Paine Stu Schwartz Earl Paine Stu Schwartz © Copyright 2005, used by...
Binary SearchingBinary Searching
Earl Paine
Stu Schwartz
Earl Paine
Stu Schwartz
© Copyright 2005, used by permission
Who we areWho we are
Combining curriculums The art of collaboration
Combining curriculums The art of collaboration
The problemThe problem
You have 10 alphabetized names along with the last 4 digits of their social security number.
You input a name and the program will search for it. If found, the SS# 4 digits will appear If not found, the user will be asked the person’s SS# 4
digits which will be added to the list.
You have 10 alphabetized names along with the last 4 digits of their social security number.
You input a name and the program will search for it. If found, the SS# 4 digits will appear If not found, the user will be asked the person’s SS# 4
digits which will be added to the list.
A sequential search algorithmA sequential search algorithm
Enter the name you wish to find: “name” Compare name to all names in list until name is
“greater than” the current name in the list
If name is there, report the SS#.
Enter the name you wish to find: “name” Compare name to all names in list until name is
“greater than” the current name in the list
If name is there, report the SS#.
Ex: Barnes
1 Adams 42322 Arras 51163 Barnes 76514 Cox 99015 Flynn 92546 James 33397 Norman 54128 Paine 65129 Schwartz 7742
10 Woods 1091
Sequential search algorithm (cont’d)Sequential search algorithm (cont’d)
If name is not there, we now have the insertion point (insert).
If name is not there, we now have the insertion point (insert).
Ex: Barlow
Ask for 4-digit SS# of name
1 Adams 42322 Arras 51163 Barnes 76514 Cox 99015 Flynn 92546 James 33397 Norman 54128 Paine 65129 Schwartz 7742
10 Woods 1091
Sequential search algorithm (cont’d)Sequential search algorithm (cont’d) Push all list names beyond insert one
position down.
Push all list names beyond insert one position down.
1. Adams 1425
2. Arras 7623
3. Barnes 9001
4. Cox 5419
…
500. Zorro 4234
1. Adams 1425
2. Arras 7623
3. Barnes 9001
4. Barnes 9001
5. Cox 5419
…
501. Zorro 4234
Position insert is now available for name and SS#
Increment the counter containing the number of data
Note: the “push” process must start at the bottom of the list so that data is not lost.
Problem with Sequential SearchProblem with Sequential Search Inefficiency Inefficiency
1. On the average, it will take 5 searches to find a name (assuming no bias in choice of name)
2. If name is not found, it will take 10 searches to ascertain that information. A list of 500 names requires 500 searches.
Note: in this age of speed, 500 searches can be done in a microsecond. It is hard to justify a need to generate a more efficient method to save such a small amount of time. It should be mentioned that if there were 10 million names with the process continuing over and over, the need for efficiency is much more pronounced. Hence a more efficient method is needed.
The binary searchThe binary search
We start with the same premises. 10 names and name.
We also introduce 3 new integer variables: low, high and target.
low is initially set equal to 1. high is initally set equal to 11. target will be the average of high and low.
We start with the same premises. 10 names and name.
We also introduce 3 new integer variables: low, high and target.
low is initially set equal to 1. high is initally set equal to 11. target will be the average of high and low.
Binary search (cont’d)Binary search (cont’d)
If target is a decimal, the decimal part of it is chopped off.
We always compare name to the name in the target position. If they match, we printout the SS#. We are done.
If target is a decimal, the decimal part of it is chopped off.
We always compare name to the name in the target position. If they match, we printout the SS#. We are done.
Binary search (cont’d)Binary search (cont’d)
If they don’t match, we find if name is greater than target name. If so, we know that name is further down in the list and we change: low = target.
Otherwise we know that name is alphabetically before the target name and
we change: high = target
If they don’t match, we find if name is greater than target name. If so, we know that name is further down in the list and we change: low = target.
Otherwise we know that name is alphabetically before the target name and
we change: high = target
Binary search (cont’d)Binary search (cont’d)
Continue the process target = (low + high)/2 - integer value
We are done (name not found) when high = low.
If the name is not found, we push all names from the target position down as before insert the new name into target, and increment the counter of names.
Continue the process target = (low + high)/2 - integer value
We are done (name not found) when high = low.
If the name is not found, we push all names from the target position down as before insert the new name into target, and increment the counter of names.
Binary search example 1Binary search example 11 Adams 42322 Arras 51163 Barnes 76514 Cox 99015 Flynn 92546 James 33397 Norman 54128 Paine 65129 Schwartz 7742
10 Woods 1091
name = “Norman”
Initial: low=1, high=11, target = 6
Pass 1: low=6, high = 11, target = 8
Pass 2: low=6, high = 8, target = 7
It took 3 comparisons to find Norman. Sequentially, it would have taken 7.
Binary search example 2Binary search example 2
1 Adams 42322 Arras 51163 Barnes 76514 Cox 99015 Flynn 92546 James 33397 Norman 54128 Paine 65129 Schwartz 7742
10 Woods 1091
name = “Donald”
Initial: low=1, high=11, target = 6
Pass 1: low=1, high = 6, target = 3
Pass 2: low=3, high = 6, target = 4
Pass 3: low=4, high = 6, target = 5
Pass 4: low=4, high = 5, target = 4
Pass 5: low=4, high = 4: STOP - name not found
Every name from position target + 1 and below must be pushed down so name can be inserted into target.
Efficiency of binary searchEfficiency of binary search
The maximum number of searches x necessary to find a name is the smallest integer that satisfies the inequality 2x > 10 or x = 4.
If n represents the number of names, the maximum number of searches x necessary to find a name is the smallest integer that satisfies the inequality 2x > n.
The maximum number of searches x necessary to find a name is the smallest integer that satisfies the inequality 2x > 10 or x = 4.
If n represents the number of names, the maximum number of searches x necessary to find a name is the smallest integer that satisfies the inequality 2x > n.
2x > n
log (2x) > log n
x log 2>log n
The maximum number of searches is the smallest integer greater than log n/log 2
Efficiency of binary searchEfficiency of binary search
# of namesMaximum sequential
searches necessaryMaximum binary
searches necessary10 10 4
100 100 71,000 1,000 105,000 5,000 13
10,000 10,000 1450,000 50,000 16
100,000 100,000 171,000,000 1,000,000 20
10,000,000 10,000,000 241,000,000,000 1,000,000,000 30
With the incredible speed of today’s computers, a binary search becomes necessary only when the number of names is large.