SlideShare a Scribd company logo
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

Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary search
Arjunsinh Jadeja
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
Talha Shaikh
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
stack presentation
stack presentationstack presentation
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Searching
SearchingSearching
Searching
Ashim Lamichhane
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
Markajul Hasnain Alif
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Sorting
SortingSorting

What's hot (20)

Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary search
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Quick sort
Quick sortQuick sort
Quick sort
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
stack presentation
stack presentationstack presentation
stack presentation
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Searching
SearchingSearching
Searching
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
single linked list
single linked listsingle linked list
single linked list
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Heaps
HeapsHeaps
Heaps
 
Sorting
SortingSorting
Sorting
 

Similar to linear search and binary search

4- searching.ppt
4- searching.ppt4- searching.ppt
4- searching.ppt
zabihniazai1
 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptx
HODElex
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
Archana Burujwale
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
technologygyan
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
sajinis3
 
Presentation
PresentationPresentation
Presentation
zohaib arif
 
A presentation on the comparison on complexity between
A presentation on the comparison on complexity betweenA presentation on the comparison on complexity between
A presentation on the comparison on complexity between
Jubayer Hasan
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
Rj Juhith
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
Prashant Rai
 
searching
searchingsearching
searching
A. S. M. Shafi
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
Dr.Shweta
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
Dr.Umadevi V
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
programming in C
programming in Cprogramming in C
programming in C
ADITHYAM19
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
21BD1A058RSahithi
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
Mohammed472103
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
ParagAhir1
 

Similar to linear search and binary search (20)

4- searching.ppt
4- searching.ppt4- searching.ppt
4- searching.ppt
 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptx
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Presentation
PresentationPresentation
Presentation
 
A presentation on the comparison on complexity between
A presentation on the comparison on complexity betweenA presentation on the comparison on complexity between
A presentation on the comparison on complexity between
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
 
searching
searchingsearching
searching
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
programming in C
programming in Cprogramming in C
programming in C
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 

More from Zia Ush Shamszaman

Pdfslide.net book of-abstracts-insight-student-conference-2015
Pdfslide.net book of-abstracts-insight-student-conference-2015Pdfslide.net book of-abstracts-insight-student-conference-2015
Pdfslide.net book of-abstracts-insight-student-conference-2015
Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-3
Hacking with Backtrack Lecture-3Hacking with Backtrack Lecture-3
Hacking with Backtrack Lecture-3
Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-2
Hacking with Backtrack Lecture-2Hacking with Backtrack Lecture-2
Hacking with Backtrack Lecture-2
Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-1
Hacking with Backtrack Lecture-1Hacking with Backtrack Lecture-1
Hacking with Backtrack Lecture-1
Zia Ush Shamszaman
 
On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...On the need for applications aware adaptive middleware in real-time RDF data ...
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 ...
On the need for applications aware adaptive middleware in real-time RDF data ...On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...
Zia Ush Shamszaman
 
L 19 ct1120
L 19 ct1120L 19 ct1120
L 19 ct1120
Zia Ush Shamszaman
 
L 18 ct1120
L 18 ct1120L 18 ct1120
L 18 ct1120
Zia Ush Shamszaman
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
Zia Ush Shamszaman
 
L 15 ct1120
L 15 ct1120L 15 ct1120
L 15 ct1120
Zia Ush Shamszaman
 
L 14-ct1120
L 14-ct1120L 14-ct1120
L 14-ct1120
Zia Ush Shamszaman
 
Bangladesh
BangladeshBangladesh
Bangladesh
Zia Ush Shamszaman
 

More from Zia Ush Shamszaman (12)

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

Recently uploaded

一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
NABLAS株式会社
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
correoyaya
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
ewymefz
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
ewymefz
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 

Recently uploaded (20)

一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 

linear search and binary search

  • 1. 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
  • 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
  • 23. References •  https://www.tutorialspoint.com/ •  https://www.hackerearth.com/ •  www.khanacademy.org/computing/computer-science/algorithms 17-01-2020 23