Lab Project # 1 – Binary Search with Selection Sort
Binary search is an advanced version of a linear search algorithm. Binary search provides a
best optimum solution to search for an element.
List of values have to be arranged in ascending order or descending order before applying the
Binary search logic. Use Selection sort logic for the sorting.
Binary search logic:
Let’s consider our list has below sorted values and we want to search for an element 45.
(23, 45, 67, 87, 98)
Get the center element from the array and in this case it is 67.
Compare center element with the key element i.e.; in this case compare 67 with 45
Since the center element is greater than the key element, we’re sure that the key element is
in the first half of the list of elements because the array has already been sorted.
If the center element is less than the key element, the element is in the second half of the list
of elements.
Consider the list of values as either first half or second half and repeat the above steps until
element is found or all the array of elements are checked.
Since the Binary search logic works by splitting the list into two parts, this search is named as
“Binary search” (Binary means two).
You can either prompt the user to enter an array of any size or just use a static size and have
the user prompt for the key to search.