Programming Question (Python) - Boolean Binary Search

In summary, the code goes as follows:Set first to 0Set last to length-1Set found to FALSEWHILE (first <= last AND NOT found)
  • #1
NaughtyBear
17
1
So I am in my Intro to CS course and they are going over Binary searches via an algorithm to search for simple things. The code goes as this:

Python:
Set first to 0
Set last to length-1
Set found to FALSE

WHILE (first <= last AND NOT found)
             Set middle to (first+last)/2

             IF (item equals data[middle])
                    Set found to TRUE
             ELSE
                     IF (Item < data[middle])
                           Set last to middle-1
                     ELSE
                            Set first to middle+1

Return found

This code is odd to me. Because the table next to it says many things. But the three columns of this table are First, Last, Middle and Comparison. Under these are 4 rows. In the first row it is 0, 10, 5 and cat < dog. The first table it is based off of says "Length=11" and its one column with 10 rows. In this order, (ant [0], cat [1], chicken [2], cow [3], deer [4], dog [5], fish [6], goat [7], horse [8], rat [9], snake [10]. With these, I am not understanding how the code is able to generate those search values and hope someone can understand this more than I.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
For a binary search algorithm to work the list you are searching must be in order.

In your example the animal names are the list you're searching and they are in alphabetical order.

So the while loop starts in the middle of the list and has three possible conditions:
1) item equals middle of the list --> we're done we found a match
2) item is less than the middle item --> next step is to search the lower half of the list
3) item is greater than the middle item --> next step is to search the upper half of the list

And of course there's the possibility that the item isn't in the list so that FOUND is false.

Laslty, I edited your post to use code tags and even though your search algorithm is in pseudo code I selected the "python" source language for it to make it easier to read.
 
  • #3
how would you do that array in python guessing that middle should be just dog not a array?
 
  • #4
Are you asking how to do that with a list, without slicing up the array into sublists? Hmmm. You could do something likeminimum = 0
maximum = len(mylist)
middle = (maximum-minimum)/2
if middle is it, done. If middle is less than search term, then
minimum = middle
middle = (maximum-minimum)/2
if middle is greater than search term:
maximum = middle
middle = (maximum-minimum)/2

You loop through all this until you find it. Of course you need to put in a way to stop if not found, which I think would be of the form
if(maximum != middle)
middle = (maximum-minimum)/2
else return "not found "
 
  • #5
wheres your iteration to loop thru the program?

n/m "if middle is it, done. If middle is less than search term, then"
 
  • #6
thanks for the reply, still learning here :smile:
 
  • #7
I just inserted it as code so you can actually see it.
 
  • #8
Folks, please take some time to read PF rules!

You cannot do someone's homework for them and post it.
 
  • #9
looks right, but not to nit pick but is there really the need for so many variables, don't know how I'd do it but I guess if you didn't know the length of the array your way is best.

I got the mit ocw lectures for python and a py2.1 bible, the last thing I did in python was learn how to use the import math header and try to put together an algo to compute pi (unsuccessfully), will get back to programming later. :-p
 

Related to Programming Question (Python) - Boolean Binary Search

What is a Boolean Binary Search?

A Boolean Binary Search is a type of search algorithm used in computer programming to find a specific value in a sorted array. It works by repeatedly dividing the array in half and determining which half the value may be located in, until the value is found or it is determined that the value does not exist in the array.

What is the purpose of using a Boolean Binary Search?

The purpose of using a Boolean Binary Search is to efficiently search for a specific value in a sorted array. It has a time complexity of O(log n) which is significantly faster than a sequential search with a time complexity of O(n).

How do you implement a Boolean Binary Search in Python?

To implement a Boolean Binary Search in Python, you first need to have a sorted array and the value you want to search for. Then, you can use a while loop to continuously divide the array in half and determine which half the value may be located in. This process is repeated until the value is found or the search reaches the end of the array.

What is the difference between a Boolean Binary Search and a sequential search?

The main difference between a Boolean Binary Search and a sequential search is the time complexity. A Boolean Binary Search has a time complexity of O(log n) while a sequential search has a time complexity of O(n). This means that a Boolean Binary Search is much more efficient for searching large arrays.

When is it appropriate to use a Boolean Binary Search?

A Boolean Binary Search is appropriate to use when you have a sorted array and you need to efficiently search for a specific value. It is commonly used in applications such as searching through large databases or finding a specific item in a sorted list.

Similar threads

  • Programming and Computer Science
Replies
7
Views
551
  • Programming and Computer Science
Replies
3
Views
742
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
1
Views
715
  • Programming and Computer Science
Replies
1
Views
999
  • Programming and Computer Science
Replies
1
Views
927
  • Programming and Computer Science
Replies
3
Views
974
  • Programming and Computer Science
Replies
2
Views
924
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top