SlideShare a Scribd company logo
SEARCHING
Vocabulary
• List
• A collection of heterogeneous data (values can be different types)
• Dynamic in size
• Array
• A collection of homogenous data (values are all the same type)
• Static in size
• Element
• A value in a list or array
• Index
• A location in the list or array
• Key
• The search parameter (the element you are looking for)
Linear Search Algorithm
1. Start from the beginning of the list
2. Check if current element matches key
1. If there is a match, then do whatever you need to do
1. Example: return the current index
2. Example: print a message saying that the key was found
3. Move to next element
4. Repeat steps 2-3 until key is found or end of list is
reached
1. If the end of the list is reach and the key is not found, then do
whatever you need to do
1. Example: return -1
2. Example: print a message saying that the key was not found
Linear Search Questions
• Does the array need to be sorted for linear search?
• Assuming N elements in an array, on average, where will
a key be found?
• In a best case scenario (key is found immediately), how
many comparisons or checks will be done in linear
search?
• In a worst case scenario (key is not found at all), how
many comparisons or checks will be done in linear
search?
Binary Search
• Also known as a “dictionary search”
• The list/array must be sorted (like a dictionary)
• Super fast at finding things
Binary Search Algorithm
1. Go to the middle element (at the middle index)
2. If the middle element matches the key, then the search
is over
3. If the key is less than middle element, go to the left
(down), else go to the right (up)
4. Repeat steps 2-4 until key is found or when the left and
right bounds pass each other
Detailed Binary Search Algorithm
1. The left bound variable is initialized to 0
2. The right bound variable is initialized to the last index
3. The middle index is the halfway point between left and right
4. Go to the middle element (at the middle index)
5. If the middle element matches the key, then the search is
over
6. If the key is less than middle element, go to the left
1. Set the right bound variable to the middle index – 1
7. If the key is greater than middle element, go to the right
1. Set the left bound variable to the middle index + 1
8. Repeat steps 3-7 while key is not found and while the left
bound is less than or equal to the right bound
Binary Search Questions
• Does the array need to be sorted for binary search?
• In a best case scenario (key is found immediately), how
many comparisons or checks will be done in binary
search?
• In a worst case scenario (key is not found at all), how
many comparisons or checks will be done in binary
search?
• How much faster is binary search than linear search?

More Related Content

PPTX
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
 
PPTX
unit II_2_i.pptx
HODElex
 
PPTX
Linear Search for design and analysis of algorithm
JavedKhan524377
 
PPT
Searching Sorting
guest2cb109
 
PPTX
Searching techniques in Data Structure And Algorithm
03446940736
 
PDF
Ocw chp6 2searchbinary
Prashant Rai
 
PPTX
Searching techniques
Archana Burujwale
 
PDF
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
 
unit II_2_i.pptx
HODElex
 
Linear Search for design and analysis of algorithm
JavedKhan524377
 
Searching Sorting
guest2cb109
 
Searching techniques in Data Structure And Algorithm
03446940736
 
Ocw chp6 2searchbinary
Prashant Rai
 
Searching techniques
Archana Burujwale
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 

Similar to Introduction to Searching Algorithm for beginners (20)

PPT
4- searching.ppt
zabihniazai1
 
DOCX
MODULE 5-Searching and-sorting
nikshaikh786
 
PPTX
Chapter 2. data structure and algorithm
SolomonEndalu
 
PPTX
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
PPTX
Searching techniques
ER Punit Jain
 
PDF
programming in C
ADITHYAM19
 
PPTX
Data Structures_ Sorting & Searching
ThenmozhiK5
 
PPTX
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
PPT
Search techniques and Hashing
Thirunavukarasu Mani
 
PDF
Unit-9 Searching .pdf
Yatru Harsha Hiski
 
PPTX
Dsa – data structure and algorithms searching
sajinis3
 
PPTX
seaching internal 2 ppt
SubhrasisBiswal1
 
PPTX
Searching_Sorting.pptx
21BD1A058RSahithi
 
PDF
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPTX
searching techniques.pptx
Dr.Shweta
 
PPTX
Searching Algorithms - Foundations of Algorithms
ssusere05275
 
PDF
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf
mebite666
 
PPTX
Searching & Sorting Algorithms
Rahul Jamwal
 
PPTX
1.4 Sorting.pptx
Sujan527908
 
PDF
Searching Algorithms for students of CS and IT using C++
shahidameer8
 
4- searching.ppt
zabihniazai1
 
MODULE 5-Searching and-sorting
nikshaikh786
 
Chapter 2. data structure and algorithm
SolomonEndalu
 
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
Searching techniques
ER Punit Jain
 
programming in C
ADITHYAM19
 
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Search techniques and Hashing
Thirunavukarasu Mani
 
Unit-9 Searching .pdf
Yatru Harsha Hiski
 
Dsa – data structure and algorithms searching
sajinis3
 
seaching internal 2 ppt
SubhrasisBiswal1
 
Searching_Sorting.pptx
21BD1A058RSahithi
 
searching techniques.pptx
Dr.Shweta
 
Searching Algorithms - Foundations of Algorithms
ssusere05275
 
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf
mebite666
 
Searching & Sorting Algorithms
Rahul Jamwal
 
1.4 Sorting.pptx
Sujan527908
 
Searching Algorithms for students of CS and IT using C++
shahidameer8
 
Ad

Recently uploaded (20)

PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PDF
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pdf
Certivo Inc
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pdf
Certivo Inc
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Ad

Introduction to Searching Algorithm for beginners

  • 2. Vocabulary • List • A collection of heterogeneous data (values can be different types) • Dynamic in size • Array • A collection of homogenous data (values are all the same type) • Static in size • Element • A value in a list or array • Index • A location in the list or array • Key • The search parameter (the element you are looking for)
  • 3. Linear Search Algorithm 1. Start from the beginning of the list 2. Check if current element matches key 1. If there is a match, then do whatever you need to do 1. Example: return the current index 2. Example: print a message saying that the key was found 3. Move to next element 4. Repeat steps 2-3 until key is found or end of list is reached 1. If the end of the list is reach and the key is not found, then do whatever you need to do 1. Example: return -1 2. Example: print a message saying that the key was not found
  • 4. Linear Search Questions • Does the array need to be sorted for linear search? • Assuming N elements in an array, on average, where will a key be found? • In a best case scenario (key is found immediately), how many comparisons or checks will be done in linear search? • In a worst case scenario (key is not found at all), how many comparisons or checks will be done in linear search?
  • 5. Binary Search • Also known as a “dictionary search” • The list/array must be sorted (like a dictionary) • Super fast at finding things
  • 6. Binary Search Algorithm 1. Go to the middle element (at the middle index) 2. If the middle element matches the key, then the search is over 3. If the key is less than middle element, go to the left (down), else go to the right (up) 4. Repeat steps 2-4 until key is found or when the left and right bounds pass each other
  • 7. Detailed Binary Search Algorithm 1. The left bound variable is initialized to 0 2. The right bound variable is initialized to the last index 3. The middle index is the halfway point between left and right 4. Go to the middle element (at the middle index) 5. If the middle element matches the key, then the search is over 6. If the key is less than middle element, go to the left 1. Set the right bound variable to the middle index – 1 7. If the key is greater than middle element, go to the right 1. Set the left bound variable to the middle index + 1 8. Repeat steps 3-7 while key is not found and while the left bound is less than or equal to the right bound
  • 8. Binary Search Questions • Does the array need to be sorted for binary search? • In a best case scenario (key is found immediately), how many comparisons or checks will be done in binary search? • In a worst case scenario (key is not found at all), how many comparisons or checks will be done in binary search? • How much faster is binary search than linear search?