Embed presentation
Downloaded 821 times



![09/30/15 © Reem Al-Attas 4
Binary Search
2. Calculate middle = (low + high) / 2.
= (0 + 8) / 2 = 4.
If 37 == array[middle] return middle
Else if 37 < array[middle] high = middle -1
Else if 37 > array[middle] low = middle +1](https://image.slidesharecdn.com/ch12searchlinearbinary-150930234148-lva1-app6891/75/Linear-Search-Binary-Search-4-2048.jpg)
![09/30/15 © Reem Al-Attas 5
Binary Search
Repeat 2. Calculate middle = (low + high) / 2.
= (0 + 3) / 2 = 1.
If 37 == array[middle] return middle
Else if 37 < array[middle] high = middle -1
Else if 37 > array[middle] low = middle +1](https://image.slidesharecdn.com/ch12searchlinearbinary-150930234148-lva1-app6891/75/Linear-Search-Binary-Search-5-2048.jpg)
![09/30/15 © Reem Al-Attas 6
Binary Search
Repeat 2. Calculate middle = (low + high) / 2.
= (2 + 3) / 2 = 2.
If 37 == array[middle] return middle
Else if 37 < array[middle] high = middle -1
Else if 37 > array[middle] low = middle +1](https://image.slidesharecdn.com/ch12searchlinearbinary-150930234148-lva1-app6891/75/Linear-Search-Binary-Search-6-2048.jpg)



The document discusses and compares linear and binary search algorithms. Linear search sequentially checks each element of an unsorted array to find a target value, while binary search works on a sorted array by repeatedly calculating the midpoint and comparing the target to the value there to narrow the search range. It provides steps for performing a binary search, including sorting the array, calculating the midpoint, and updating the search range based on whether the target is less than, greater than, or equal to the midpoint value.



![09/30/15 © Reem Al-Attas 4
Binary Search
2. Calculate middle = (low + high) / 2.
= (0 + 8) / 2 = 4.
If 37 == array[middle] return middle
Else if 37 < array[middle] high = middle -1
Else if 37 > array[middle] low = middle +1](https://image.slidesharecdn.com/ch12searchlinearbinary-150930234148-lva1-app6891/75/Linear-Search-Binary-Search-4-2048.jpg)
![09/30/15 © Reem Al-Attas 5
Binary Search
Repeat 2. Calculate middle = (low + high) / 2.
= (0 + 3) / 2 = 1.
If 37 == array[middle] return middle
Else if 37 < array[middle] high = middle -1
Else if 37 > array[middle] low = middle +1](https://image.slidesharecdn.com/ch12searchlinearbinary-150930234148-lva1-app6891/75/Linear-Search-Binary-Search-5-2048.jpg)
![09/30/15 © Reem Al-Attas 6
Binary Search
Repeat 2. Calculate middle = (low + high) / 2.
= (2 + 3) / 2 = 2.
If 37 == array[middle] return middle
Else if 37 < array[middle] high = middle -1
Else if 37 > array[middle] low = middle +1](https://image.slidesharecdn.com/ch12searchlinearbinary-150930234148-lva1-app6891/75/Linear-Search-Binary-Search-6-2048.jpg)

