Programming Question (Python) - Boolean Binary Search

Click For Summary

Discussion Overview

The discussion revolves around the implementation and understanding of a binary search algorithm in Python, particularly focusing on how the algorithm operates with a list of animal names sorted in alphabetical order. Participants explore the mechanics of the algorithm, including the conditions for searching and the structure of the code.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant presents a pseudo code for a binary search algorithm and expresses confusion about how it generates search values based on a given table of animal names.
  • Another participant clarifies that the list must be ordered for binary search to function and explains the three conditions that dictate the search process.
  • A participant inquires about implementing the binary search without slicing the array into sublists, suggesting a method to find the middle value and adjust the search bounds.
  • Another participant questions the absence of iteration in the proposed code and highlights the need for a loop to continue searching until the item is found or confirmed absent.
  • One participant expresses gratitude for the responses while acknowledging their ongoing learning process.
  • A participant emphasizes the importance of adhering to forum rules regarding homework assistance.
  • Another participant comments on the complexity of variable usage in the algorithm and shares their own experience with learning Python and attempting to create an algorithm for computing pi.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of an ordered list for binary search and the basic mechanics of the algorithm. However, there are differing views on the implementation details and the number of variables used, indicating that the discussion remains unresolved regarding the optimal approach to coding the algorithm.

Contextual Notes

Some participants express uncertainty about the iteration process in the algorithm, and there are unresolved questions about the efficiency and clarity of variable usage in the proposed solutions.

NaughtyBear
Messages
17
Reaction score
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
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.
 
how would you do that array in python guessing that middle should be just dog not a array?
 
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 "
 
wheres your iteration to loop thru the program?

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

You cannot do someone's homework for them and post it.
 
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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
55
Views
7K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K