This document describes a binary search algorithm to find the term 40 in a sorted array of numbers. It shows the steps of setting the beginning and end indexes, calculating the mid-point, comparing the mid-point value to the search term, and adjusting the beginning or end index accordingly. The algorithm finds that the term 40 is located at position 5 in the array.
1 of 1
Download to read offline
More Related Content
Binary Search
1. Sorted array
Array = 11, 22, 30, 33, 40, 44, 45
Term = 40
Step 1:
BEG = 1 and END = 7
MID = BEG+END/2 = 1+7/2 = 4
Array[MID] = Array[4] = 33
Step 2:
33 < 40
BEG = MID + 1 = 4+1 = 5
MID = BEG+END/2 = 5+7/2 = 6
Array[MID] =44
Step 3:
44 > 40
END = MID ¨C 1 = 6 ¨C 1 = 5
MID = BEG+END/2 = 5 + 5/2 = 5
Array[MID] = 40
ITEM IS FOUND AT POSITION NO. 5