Given the following sorted array of integers 1 7 8 11 15 22
Given the following sorted array of integers: 1 7 8 11 15 22 38 What are the \"middle\" elements that the binary search algorithm would examine/compare to as it divides the array into sections when looking for the number 7? 1 7 8 11 15 22 38
Solution
When looking for 7 in binary search tree,
First divide the array into 2 parts by middle element
Middle element index = first + last / 2
Middle = 0+6 / 2 = 6 / 2 = 3
Now check the middle element, is it search element
arr[middle]= 11 which is not search element
Now change the last index to middle-1
Last = middle -1 = 3-1 = 2
Now find middle element index
Middle = 0+2/2 = 1
arr[middle] = 7 here we have find the search element.
