Binary Search O(log2n)
■A binary search looks for an
item in a list using a divide-
and-conquer strategy
7
8.
Binary Search
◻Binary searchalgorithm assumes that the items in
the array being searched are sorted
◻We start by dividing the array into 2 parts and look
look for the item in the middle of the array in a
binary search
◻If the item we are searching is less than the item in
the middle, we know that the item won’t be in the
second half of the array. So we look in the 1st
half
◻Once again we examine the middle element
◻The process continues with each comparison cutting
in half the portion of the array until we reach the
location the item might be
9.
• Determine whether22 is in the list
Binary Search Algorithm Example
Array list with twelve (12) elements
21 is less than 22, so we
look in the 2nd
half