SlideShare a Scribd company logo
4
Most read
5
Most read
6
Most read
SearchSearch
Linear & Binary
09/30/15 © Reem Al-Attas 2
Linear Search
• Find 37?
≠ ≠ =
Return 2
09/30/15 © Reem Al-Attas 3
Binary Search
• Find 37?
1. Sort Array.
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
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
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
09/30/15 © Reem Al-Attas 7
Binary Search
09/30/15 © Reem Al-Attas 8
Binary Search
• If not found  stop when low > high.

More Related Content

PDF
Binary Search - Design & Analysis of Algorithms
PPTX
Sorting Algorithms
PPTX
Data Structures (CS8391)
PPT
PPT
Searching algorithms
PPT
Divide and Conquer
PDF
Sorting Algorithms
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Binary Search - Design & Analysis of Algorithms
Sorting Algorithms
Data Structures (CS8391)
Searching algorithms
Divide and Conquer
Sorting Algorithms
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue

What's hot (20)

PPT
Data Structures - Searching & sorting
PPTX
PPTX
Sorting types and Algorithms
PPT
Heap sort
PDF
Linear search algorithm
PPTX
Graph representation
PPTX
heap Sort Algorithm
PPT
Fundamentals of data structures
PPT
Merge sort
PPTX
Presentation on the topic selection sort
PPTX
Merge sort and quick sort
PPTX
Merging files (Data Structure)
PPT
finding Min and max element from given array using divide & conquer
PPT
Queue Data Structure
PPTX
My lectures circular queue
PPTX
Selection sorting
PDF
linear search and binary search
PPT
Binary Search
PPT
Algorithm: Quick-Sort
PPTX
Searching
Data Structures - Searching & sorting
Sorting types and Algorithms
Heap sort
Linear search algorithm
Graph representation
heap Sort Algorithm
Fundamentals of data structures
Merge sort
Presentation on the topic selection sort
Merge sort and quick sort
Merging files (Data Structure)
finding Min and max element from given array using divide & conquer
Queue Data Structure
My lectures circular queue
Selection sorting
linear search and binary search
Binary Search
Algorithm: Quick-Sort
Searching
Ad

More from Reem Alattas (20)

PDF
Rumble Lights Pitch Deck
PPTX
NASA Datanauts Water Cooler Chat: Autonomous Design of Modular Robots
PPTX
She looks just like me 2017
PPTX
Nasa Datanauts Water Cooler Chat: Robotics for Space Exploration
PPTX
Nasa Datanauts Water Cooler Chat: Evolutionary Robots for Space Exploration
PPTX
She Looks Just Like Me 2017
PPTX
Tran helmet pitch
PPTX
Evolutionary Algorithms
PPTX
Evolutionary Robotics
PDF
Create a Need
PPTX
Enhancing input on and above the interactive surface
PPTX
Skinput: Appropriating the Body as an Input Surface
PPT
XML - EXtensible Markup Language
PPT
Dynamic HTML Event Model
PPT
PHP Scripting
PPT
DHTML - Dynamic HTML
PPT
JavaScript Objects
PPT
JavaScript Arrays
PPT
JavaScript Functions
PPT
JavaScript Control Statements II
Rumble Lights Pitch Deck
NASA Datanauts Water Cooler Chat: Autonomous Design of Modular Robots
She looks just like me 2017
Nasa Datanauts Water Cooler Chat: Robotics for Space Exploration
Nasa Datanauts Water Cooler Chat: Evolutionary Robots for Space Exploration
She Looks Just Like Me 2017
Tran helmet pitch
Evolutionary Algorithms
Evolutionary Robotics
Create a Need
Enhancing input on and above the interactive surface
Skinput: Appropriating the Body as an Input Surface
XML - EXtensible Markup Language
Dynamic HTML Event Model
PHP Scripting
DHTML - Dynamic HTML
JavaScript Objects
JavaScript Arrays
JavaScript Functions
JavaScript Control Statements II
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Classroom Observation Tools for Teachers
PDF
Basic Mud Logging Guide for educational purpose
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Pharma ospi slides which help in ospi learning
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
01-Introduction-to-Information-Management.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Insiders guide to clinical Medicine.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
TR - Agricultural Crops Production NC III.pdf
Cell Types and Its function , kingdom of life
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Classroom Observation Tools for Teachers
Basic Mud Logging Guide for educational purpose
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
VCE English Exam - Section C Student Revision Booklet
Pharma ospi slides which help in ospi learning
2.FourierTransform-ShortQuestionswithAnswers.pdf
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Insiders guide to clinical Medicine.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPH.pptx obstetrics and gynecology in nursing
human mycosis Human fungal infections are called human mycosis..pptx
TR - Agricultural Crops Production NC III.pdf

Linear Search & Binary Search

  • 2. 09/30/15 © Reem Al-Attas 2 Linear Search • Find 37? ≠ ≠ = Return 2
  • 3. 09/30/15 © Reem Al-Attas 3 Binary Search • Find 37? 1. Sort Array.
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 09/30/15 © Reem Al-Attas 7 Binary Search
  • 8. 09/30/15 © Reem Al-Attas 8 Binary Search • If not found  stop when low > high.