mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
searching
binary search
-
a binary search operates on a contiguous sequence with a specified left and right index. this is called the search space.
-
binary searching is composed of 3 sections:
- pre-processing: sort if collection is unsorted
- binary search: using a loop or recursion to divide search sapce in half after each comparison
- post-processing: determine viable candidates in the remaining space
-
there are 3 "templates" when writing a binary search:
while left < right
, withleft = mid + 1
andright = mid - 1
while left < right
, withleft = mid + 1
andright = mid
, andleft
is returnedwhile left + 1 < right
, withleft = 1
andright = mid
, andleft
andright
are returned