- #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:
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.
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: