Search: Linear & Binary

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

Search

Linear & Binary


Linear Search
• Find 37?

≠ ≠ =
Return 2

09/30/15 © Reem Al-Attas 2


Binary Search
• Find 37?
1. Sort Array.

09/30/15 © Reem Al-Attas 3


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
09/30/15 © Reem Al-Attas 4
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
09/30/15 © Reem Al-Attas 5
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
09/30/15 © Reem Al-Attas 6
Binary Search

09/30/15 © Reem Al-Attas 7


Binary Search
• If not found  stop when low > high.

09/30/15 © Reem Al-Attas 8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy