1

I am learning python through this website.

It's a fantastic resource, but when he explains binary searches I am left a little confused.

This is the code he supplies to search through a text file of super-villain names:

file = open("super_villains.txt")
name_list = []
for line in file:
 line = line.strip()
 name_list.append(line)
file.close()
# Binary search
key = "Morgiana the Shrew"
lower_bound = 0
upper_bound = len(name_list)-1
found = False
while lower_bound <= upper_bound and not found:
 middle_pos = (lower_bound+upper_bound) // 2
 if name_list[middle_pos] < key:
 lower_bound = middle_pos + 1
 elif name_list[middle_pos] > key:
 upper_bound = middle_pos - 1
 else:
 found = True
if found:
 print("The name is at position", middle_pos)
if not found:
 print("The name was not in the list.")

This is the part that has left me confused: if name_list[middle_pos] < key: How could python possibly know if a certain position in the index is greater or lesser than the position of the key, without already knowing the position of the key we're looking for?

This feels like a dumb question, but I don't understand how you can compare two positions in an array when we don't know one of them.

asked Sep 21, 2015 at 21:22
3
  • 3
    According to this code, key isn't a position in an array, it's a string: key = "Morgiana the Shrew". Commented Sep 21, 2015 at 21:25
  • "Key" is also the term used for the piece of data upon which a sequence is sorted. It should not be confused with its usage as the value used to index a list or a dictionary. Commented Sep 21, 2015 at 21:28
  • 1
    Binary searches only work on sorted datasets so it might be an assumption left out of the explanation. Commented Sep 21, 2015 at 21:28

3 Answers 3

2

One key thing for binary searches is that it is searching through a sorted list, in this case, I believe that it is assuming that is in sorted alphabetically.

With this in mind, that if statement is actually comparing strings, not numbers which means that it is looking whether the current key is before or after the current middle pos. Then once it finds out, it will half the positions until it has found the position of the key.

answered Sep 21, 2015 at 21:26
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, that makes sense. He didn't mention that the list had to be sorted. So the > and < operators will alphabetically compare letters? I didn't know that either.
2

name_list[middle_pos] is going to be some value such as Ratigan the Rat. In your example key is Morgiana the Shrew.

The comparison name_list[middle_pos] < key will decide whether Ratigan is before Morgiana. As he isn't (alphabetically), we then narrow our search to the range of names before him, and chop it in half. Then ask the question again, and narrow the range.

You keep narrowing the range until there is only one value left. It is either correct or not. If not then Morgiana isn't in the list at all. Or we've found her index.

answered Sep 21, 2015 at 21:40

1 Comment

Great! I understand how binary search works in theory, I just didn't know that A: the list was alpabetized, and B: the < and > operators compared letters as well as numbers. Thanks!
1

It assumes that the list name_list is in sorted order. Then if the name located at middle_pos occurs alphabetically before the name we are searching for (key in this case) we know we can skip all the names from lower_bound to middle_pos.

answered Sep 21, 2015 at 21:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.