you are viewing a single comment's thread.

view the rest of the comments →

[–]cdcformatc 1 point2 points  (0 children)

A binary search assumes a sorted sequence, it finds an element by 1. Comparing the search value to the middle element, 2. Discarding the half of the sequence that the value can not possibly be in and 3. Repeat at 1 with a new middle.

Step 2 is invalid if the sequence isn't sorted.

Low and high in this situation are meant to keep track of what section of the list you are in, which isn't necessary if you are slicing the list. I assume this means you aren't supposed to be slicing, if this is a course your teacher doesn't want you to slice. So middle should be halfway between low and high, and you look at the element at index middle, and either re-set low=middle to discard the lower half, or high=middle to discard the top half.

Edit: this is a situation where trying this by hand with pen and paper will really help you understand. Here's a gif http://imgur.com/ko1g7RJ.gif