SlideShare a Scribd company logo
8
Most read
12
Most read
15
Most read
First Year BA (IT)
CT1120 Algorithms
Lecture 13
Dr. Zia Ush Shamszaman
z.shamszaman1@nuigalway.ie
1
School of Computer Science, College of Science and Engineering
17-01-2020
Information
•  Number of Lectures -12
–  When? Every Friday (12-1)
–  Where: AC201
17-01-2020 2
Overview
•  Flashback
•  Introduction
•  Linear Search
•  Binary Search
•  Feedback and Assessment
317-01-2020
Introduction
•  The definition of a search is the process of
looking for something.
•  In computer science
–  A search algorithm is an algorithm for finding
an item with specified properties among a
collection of items that coded into a computer
program.
17-01-2020 4
Linear Search (LS)
Linear Search involves checking all the
elements of the array (or any other
structure) one by one and in sequence
until the desired result is found.
17-01-2020 5
Graphical Illustration of LS
17-01-2020 6
Every item is checked but no match is found till the
end of the data collection
Graphical Illustration of LS
17-01-2020 7
Found a match at index 2
Linear Search Algorithm
•  Linear Search ( Array A, Value x)
•  Step 1: Set i to 1
•  Step 2: if i > n then go to step 7
•  Step 3: if A[i] = x then go to step 6
•  Step 4: Set i to i + 1
•  Step 5: Go to Step 2
•  Step 6: Print Element x Found at index i and go to step 8
•  Step 7: Print element not found
•  Step 8: Exit
17-01-2020 8
Pseudocode
•  procedure linear_search (list, value)
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure
17-01-2020 9
Adv. & Disadv. Of LS
•  Advantages
–  Easiest to understand and implement
–  No sorting required
–  Suitable for small list sizes
–  Works fine for small number of elements
•  Disadvantages
–  Time inefficient as compared to other algorithms
–  Not suitable for large-sized lists
–  Search time increases with number of elements
17-01-2020 10
Binary Search (BS)
•  Binary Search is a Divide and Conquer algorithm
•  Binary search algorithm finds the position of a target value
within a sorted array
•  A more efficient approach than Linear Search because
Binary Search basically reduces the search space to half at
each step
17-01-2020 11
Binary Search
•  The algorithm begins by comparing the target value to the
value of the middle element of the sorted array
•  If they are equal the middle position is returned and the
search is finished
•  If the target value is less than the middle element's value,
then the search continues on the lower half of the array;
•  If the target value is greater than the middle element’s
value, then the search continues on the upper half of the
array
•  This process continues, eliminating half of the elements until
the value is found
17-01-2020 12
Graphical Illustration of BS
17-01-2020 13
Graphical Illustration of BS
17-01-2020 14
Graphical Illustration of BS
17-01-2020 15
Graphical Illustration of BS
17-01-2020 16
Graphical Illustration of BS
17-01-2020 17
Graphical Illustration of BS
17-01-2020 18
Binary Search
•  With each test that fails to and a match, the search is
continued with one or other of the two sub-intervals,
each at most half the size
•  If the original number of items is N then after the
first iteration there will be at most N/2 items
remaining, then at most N/4 items, and so on
•  In the worst case, when the value is not in the list, the
algorithm must continue iterating until the list is empty
17-01-2020 19
Pseudocode
17-01-2020 20
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not
exists.
set midPoint =
lowerBound + ( upperBound -lowerBound )/2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
Binary Search Algorithm
•  Step 1 − Start searching data from middle of the list.
•  Step 2 − If it is a match, return the index of the item,
and exit.
•  Step 3 − If it is not a match, probe position.
•  Step 4 − Divide the list and find the new middle.
•  Step 5 − If data is greater than middle, search in
higher sub-list.
•  Step 6 − If data is smaller than middle, search in lower
sub-list.
•  Step 7 − Repeat until match.
17-01-2020 21
Next Lecture
•  Binary Search
•  Sorting
17-01-2020 22
References
•  https://www.tutorialspoint.com/
•  https://www.hackerearth.com/
•  www.khanacademy.org/computing/computer-science/algorithms
17-01-2020 23
Feedback & Assessment
17-01-2020 24

More Related Content

What's hot (20)

PPTX
Queue ppt
SouravKumar328
 
PPTX
Sorting Algorithms
Pranay Neema
 
PPT
Binary search tree(bst)
Hossain Md Shakhawat
 
PPT
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
PPTX
Searching
Ashim Lamichhane
 
PPT
Binary Search
kunj desai
 
PDF
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
PPTX
Linear and Binary search
Nisha Soms
 
PPTX
sorting and its types
SIVASHANKARIRAJAN
 
PPTX
single linked list
Sathasivam Rangasamy
 
PDF
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
PPTX
Sparse matrix and its representation data structure
Vardhil Patel
 
PPTX
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
DOC
Time and space complexity
Ankit Katiyar
 
PPTX
Hashing In Data Structure
Meghaj Mallick
 
PPTX
Circular queue
Lovely Professional University
 
PPTX
Linear Search Presentation
Markajul Hasnain Alif
 
PPTX
Array ppt
Kaushal Mehta
 
PDF
Array data structure
maamir farooq
 
PPTX
Quick Sort
Shweta Sahu
 
Queue ppt
SouravKumar328
 
Sorting Algorithms
Pranay Neema
 
Binary search tree(bst)
Hossain Md Shakhawat
 
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Searching
Ashim Lamichhane
 
Binary Search
kunj desai
 
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Linear and Binary search
Nisha Soms
 
sorting and its types
SIVASHANKARIRAJAN
 
single linked list
Sathasivam Rangasamy
 
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
Sparse matrix and its representation data structure
Vardhil Patel
 
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Time and space complexity
Ankit Katiyar
 
Hashing In Data Structure
Meghaj Mallick
 
Linear Search Presentation
Markajul Hasnain Alif
 
Array ppt
Kaushal Mehta
 
Array data structure
maamir farooq
 
Quick Sort
Shweta Sahu
 

Similar to linear search and binary search (20)

PPT
4- searching.ppt
zabihniazai1
 
PPTX
unit II_2_i.pptx
HODElex
 
PPTX
Searching techniques
Archana Burujwale
 
PPTX
Data structure and algorithms
technologygyan
 
PPTX
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
PPTX
Dsa – data structure and algorithms searching
sajinis3
 
PPTX
Presentation
zohaib arif
 
PPTX
A presentation on the comparison on complexity between
Jubayer Hasan
 
PDF
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PDF
advanced searching and sorting.pdf
haramaya university
 
PPTX
Rahat &amp; juhith
Rj Juhith
 
PPTX
Searching techniques in Data Structure And Algorithm
03446940736
 
PDF
Ocw chp6 2searchbinary
Prashant Rai
 
PDF
searching
A. S. M. Shafi
 
PPTX
searching techniques.pptx
Dr.Shweta
 
PPTX
Searching_Techniques Binary search Algorithm
Anil Yadav
 
PPT
Data Structures 8
Dr.Umadevi V
 
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
PPTX
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
 
PPTX
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
4- searching.ppt
zabihniazai1
 
unit II_2_i.pptx
HODElex
 
Searching techniques
Archana Burujwale
 
Data structure and algorithms
technologygyan
 
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
Dsa – data structure and algorithms searching
sajinis3
 
Presentation
zohaib arif
 
A presentation on the comparison on complexity between
Jubayer Hasan
 
advanced searching and sorting.pdf
haramaya university
 
Rahat &amp; juhith
Rj Juhith
 
Searching techniques in Data Structure And Algorithm
03446940736
 
Ocw chp6 2searchbinary
Prashant Rai
 
searching
A. S. M. Shafi
 
searching techniques.pptx
Dr.Shweta
 
Searching_Techniques Binary search Algorithm
Anil Yadav
 
Data Structures 8
Dr.Umadevi V
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
 
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
Ad

More from Zia Ush Shamszaman (12)

PDF
Pdfslide.net book of-abstracts-insight-student-conference-2015
Zia Ush Shamszaman
 
PDF
Hacking with Backtrack Lecture-3
Zia Ush Shamszaman
 
PDF
Hacking with Backtrack Lecture-2
Zia Ush Shamszaman
 
PDF
Hacking with Backtrack Lecture-1
Zia Ush Shamszaman
 
PDF
On the need for applications aware adaptive middleware in real-time RDF data ...
Zia Ush Shamszaman
 
PDF
On the need for applications aware adaptive middleware in real-time RDF data ...
Zia Ush Shamszaman
 
PDF
L 19 ct1120
Zia Ush Shamszaman
 
PDF
L 18 ct1120
Zia Ush Shamszaman
 
PDF
L 17 ct1120
Zia Ush Shamszaman
 
PDF
L 15 ct1120
Zia Ush Shamszaman
 
PDF
L 14-ct1120
Zia Ush Shamszaman
 
PDF
Bangladesh
Zia Ush Shamszaman
 
Pdfslide.net book of-abstracts-insight-student-conference-2015
Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-3
Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-2
Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-1
Zia Ush Shamszaman
 
On the need for applications aware adaptive middleware in real-time RDF data ...
Zia Ush Shamszaman
 
On the need for applications aware adaptive middleware in real-time RDF data ...
Zia Ush Shamszaman
 
L 19 ct1120
Zia Ush Shamszaman
 
L 18 ct1120
Zia Ush Shamszaman
 
L 17 ct1120
Zia Ush Shamszaman
 
L 15 ct1120
Zia Ush Shamszaman
 
L 14-ct1120
Zia Ush Shamszaman
 
Bangladesh
Zia Ush Shamszaman
 
Ad

Recently uploaded (20)

PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PPT
deep dive data management sharepoint apps.ppt
novaprofk
 
PPTX
GenAI-Introduction-to-Copilot-for-Bing-March-2025-FOR-HUB.pptx
cleydsonborges1
 
PPTX
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
PPTX
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PDF
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
PPTX
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
PPTX
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
PPTX
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
PDF
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
PPTX
Dr djdjjdsjsjsjsjsjsjjsjdjdjdjdjjd1.pptx
Nandy31
 
PPTX
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
PPTX
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
PPTX
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
PPTX
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
PDF
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
PPTX
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
PDF
Choosing the Right Database for Indexing.pdf
Tamanna
 
PDF
JavaScript - Good or Bad? Tips for Google Tag Manager
📊 Markus Baersch
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
deep dive data management sharepoint apps.ppt
novaprofk
 
GenAI-Introduction-to-Copilot-for-Bing-March-2025-FOR-HUB.pptx
cleydsonborges1
 
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
Dr djdjjdsjsjsjsjsjsjjsjdjdjdjdjjd1.pptx
Nandy31
 
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
Choosing the Right Database for Indexing.pdf
Tamanna
 
JavaScript - Good or Bad? Tips for Google Tag Manager
📊 Markus Baersch
 

linear search and binary search

  • 1. First Year BA (IT) CT1120 Algorithms Lecture 13 Dr. Zia Ush Shamszaman [email protected] 1 School of Computer Science, College of Science and Engineering 17-01-2020
  • 2. Information •  Number of Lectures -12 –  When? Every Friday (12-1) –  Where: AC201 17-01-2020 2
  • 3. Overview •  Flashback •  Introduction •  Linear Search •  Binary Search •  Feedback and Assessment 317-01-2020
  • 4. Introduction •  The definition of a search is the process of looking for something. •  In computer science –  A search algorithm is an algorithm for finding an item with specified properties among a collection of items that coded into a computer program. 17-01-2020 4
  • 5. Linear Search (LS) Linear Search involves checking all the elements of the array (or any other structure) one by one and in sequence until the desired result is found. 17-01-2020 5
  • 6. Graphical Illustration of LS 17-01-2020 6 Every item is checked but no match is found till the end of the data collection
  • 7. Graphical Illustration of LS 17-01-2020 7 Found a match at index 2
  • 8. Linear Search Algorithm •  Linear Search ( Array A, Value x) •  Step 1: Set i to 1 •  Step 2: if i > n then go to step 7 •  Step 3: if A[i] = x then go to step 6 •  Step 4: Set i to i + 1 •  Step 5: Go to Step 2 •  Step 6: Print Element x Found at index i and go to step 8 •  Step 7: Print element not found •  Step 8: Exit 17-01-2020 8
  • 9. Pseudocode •  procedure linear_search (list, value) for each item in the list if match item == value return the item's location end if end for end procedure 17-01-2020 9
  • 10. Adv. & Disadv. Of LS •  Advantages –  Easiest to understand and implement –  No sorting required –  Suitable for small list sizes –  Works fine for small number of elements •  Disadvantages –  Time inefficient as compared to other algorithms –  Not suitable for large-sized lists –  Search time increases with number of elements 17-01-2020 10
  • 11. Binary Search (BS) •  Binary Search is a Divide and Conquer algorithm •  Binary search algorithm finds the position of a target value within a sorted array •  A more efficient approach than Linear Search because Binary Search basically reduces the search space to half at each step 17-01-2020 11
  • 12. Binary Search •  The algorithm begins by comparing the target value to the value of the middle element of the sorted array •  If they are equal the middle position is returned and the search is finished •  If the target value is less than the middle element's value, then the search continues on the lower half of the array; •  If the target value is greater than the middle element’s value, then the search continues on the upper half of the array •  This process continues, eliminating half of the elements until the value is found 17-01-2020 12
  • 13. Graphical Illustration of BS 17-01-2020 13
  • 14. Graphical Illustration of BS 17-01-2020 14
  • 15. Graphical Illustration of BS 17-01-2020 15
  • 16. Graphical Illustration of BS 17-01-2020 16
  • 17. Graphical Illustration of BS 17-01-2020 17
  • 18. Graphical Illustration of BS 17-01-2020 18
  • 19. Binary Search •  With each test that fails to and a match, the search is continued with one or other of the two sub-intervals, each at most half the size •  If the original number of items is N then after the first iteration there will be at most N/2 items remaining, then at most N/4 items, and so on •  In the worst case, when the value is not in the list, the algorithm must continue iterating until the list is empty 17-01-2020 19
  • 20. Pseudocode 17-01-2020 20 Procedure binary_search A ← sorted array n ← size of array x ← value to be searched Set lowerBound = 1 Set upperBound = n while x not found if upperBound < lowerBound EXIT: x does not exists. set midPoint = lowerBound + ( upperBound -lowerBound )/2 if A[midPoint] < x set lowerBound = midPoint + 1 if A[midPoint] > x set upperBound = midPoint - 1 if A[midPoint] = x EXIT: x found at location midPoint end while end procedure
  • 21. Binary Search Algorithm •  Step 1 − Start searching data from middle of the list. •  Step 2 − If it is a match, return the index of the item, and exit. •  Step 3 − If it is not a match, probe position. •  Step 4 − Divide the list and find the new middle. •  Step 5 − If data is greater than middle, search in higher sub-list. •  Step 6 − If data is smaller than middle, search in lower sub-list. •  Step 7 − Repeat until match. 17-01-2020 21
  • 22. Next Lecture •  Binary Search •  Sorting 17-01-2020 22