Data Structures and Algorithm - Week 9 - Search Algorithms

Ferdin Joe John Joseph PhD
Ferdin Joe John Joseph PhDAssistant Professor, Faculty of Information Technology en Thai-Nichi Institute of Technology
Data Structures and
Algorithms
Week 9: Search Algorithms
Ferdin Joe John Joseph, PhD
Faculty of Information Technology
Thai-Nichi Institute of Technology, Bangkok
Week 9
• Linear Search
• Binary Search
• Implementation in Java
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
2
Linear Search
• Searching is the process of determining whether or not a
given value exists in a data structure or a storage media.
• We discuss two searching methods on one-dimensional
arrays: linear search and binary search.
• The linear (or sequential) search algorithm on an array is:
• Sequentially scan the array, comparing each array item with the searched value.
• If a match is found; return the index of the matched element; otherwise return –1.
• Note: linear search can be applied to both sorted and unsorted
arrays.
Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-
3
Linear Search
• The algorithm translates to the following Java method:
public static int linearSearch(Object[] array,
Object key)
{
for(int k = 0; k < array.length; k++)
if(array[k].equals(key))
return k;
return -1;
}
Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-
4
Binary Search
• Binary search uses a recursive method to search an
array to find a specified value
• The array must be a sorted array:
a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex]
• If the value is found, its index is returned
• If the value is not found, -1 is returned
• Note: Each execution of the recursive method
reduces the search space by about a half
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
5
Binary Search
• An algorithm to solve this task looks at the middle
of the array or array segment first
• If the value looked for is smaller than the value in
the middle of the array
• Then the second half of the array or array segment can
be ignored
• This strategy is then applied to the first half of the array
or array segment
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
6
Binary Search
• If the value looked for is larger than the value in the middle
of the array or array segment
• Then the first half of the array or array segment can be ignored
• This strategy is then applied to the second half of the array or array
segment
• If the value looked for is at the middle of the array or array
segment, then it has been found
• If the entire array (or array segment) has been searched in
this way without finding the value, then it is not in the array
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
7
Pseudocode for Binary Search
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
8
Recursive Method for Binary Search
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
9
Execution of the Method search
(Part 1 of 2)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
10
Execution of the Method search
(Part 1 of 2)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
11
Checking the search Method
1. There is no infinite recursion
• On each recursive call, the value of first is
increased, or the value of last is decreased
• If the chain of recursive calls does not end in some
other way, then eventually the method will be called
with first larger than last
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
12
Checking the search Method
2. Each stopping case performs the correct action
for that case
• If first > last, there are no array elements
between a[first] and a[last], so key is not in
this segment of the array, and result is correctly
set to -1
• If key == a[mid], result is correctly set to mid
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
13
Checking the search Method
3. For each of the cases that involve recursion, if all
recursive calls perform their actions correctly,
then the entire case performs correctly
• If key < a[mid], then key must be one of the
elements a[first] through a[mid-1], or it is not
in the array
• The method should then search only those elements,
which it does
• The recursive call is correct, therefore the entire
action is correct
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
14
Checking the search Method
• If key > a[mid], then key must be one of the
elements a[mid+1] through a[last], or it is not
in the array
• The method should then search only those elements,
which it does
• The recursive call is correct, therefore the entire
action is correct
The method search passes all three tests:
Therefore, it is a good recursive method definition
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
15
Efficiency of Binary Search
• The binary search algorithm is extremely fast
compared to an algorithm that tries all array
elements in order
• About half the array is eliminated from consideration
right at the start
• Then a quarter of the array, then an eighth of the array,
and so forth
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
16
Efficiency of Binary Search
• Given an array with 1,000 elements, the binary search will
only need to compare about 10 array elements to the key
value, as compared to an average of 500 for a serial search
algorithm
• The binary search algorithm has a worst-case running time
that is logarithmic: O(log n)
• A serial search algorithm is linear: O(n)
• If desired, the recursive version of the method search can
be converted to an iterative version that will run more
efficiently
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
17
Iterative Version of Binary Search
(Part 1 of 2)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
18
Iterative Version of Binary Search
(Part 2 of 2)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
19
Next Week
Sorting Algorithms
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
20
1 de 20

Recomendados

Data Structures and Algorithm - Week 11 - Algorithm Analysis por
Data Structures and Algorithm - Week 11 - Algorithm AnalysisData Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm AnalysisFerdin Joe John Joseph PhD
493 vistas39 diapositivas
Data Structures and Algorithm - Week 4 - Trees, Binary Trees por
Data Structures and Algorithm - Week 4 - Trees, Binary TreesData Structures and Algorithm - Week 4 - Trees, Binary Trees
Data Structures and Algorithm - Week 4 - Trees, Binary TreesFerdin Joe John Joseph PhD
553 vistas35 diapositivas
Data Structures and Algorithm - Week 3 - Stacks and Queues por
Data Structures and Algorithm - Week 3 - Stacks and QueuesData Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesFerdin Joe John Joseph PhD
559 vistas59 diapositivas
Week 1 - Data Structures and Algorithms por
Week 1 - Data Structures and AlgorithmsWeek 1 - Data Structures and Algorithms
Week 1 - Data Structures and AlgorithmsFerdin Joe John Joseph PhD
378 vistas36 diapositivas
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees por
Data Structures and Algorithm - Week 8 - Minimum Spanning TreesData Structures and Algorithm - Week 8 - Minimum Spanning Trees
Data Structures and Algorithm - Week 8 - Minimum Spanning TreesFerdin Joe John Joseph PhD
243 vistas40 diapositivas
Week 2 - Data Structures and Algorithms por
Week 2 - Data Structures and AlgorithmsWeek 2 - Data Structures and Algorithms
Week 2 - Data Structures and AlgorithmsFerdin Joe John Joseph PhD
700 vistas43 diapositivas

Más contenido relacionado

La actualidad más candente

geekgap.io webinar #1 por
geekgap.io webinar #1geekgap.io webinar #1
geekgap.io webinar #1junior Teudjio
32 vistas38 diapositivas
Data Structure por
Data StructureData Structure
Data Structuresheraz1
1.1K vistas40 diapositivas
Stacks in algorithems & data structure por
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structurefaran nawaz
1.7K vistas17 diapositivas
Introduction to data structure and algorithms por
Introduction to data structure and algorithmsIntroduction to data structure and algorithms
Introduction to data structure and algorithmsResearch Scholar in Manonmaniam Sundaranar University
507 vistas13 diapositivas
Recommendation algorithm using reinforcement learning por
Recommendation algorithm using reinforcement learningRecommendation algorithm using reinforcement learning
Recommendation algorithm using reinforcement learningArithmer Inc.
1.2K vistas20 diapositivas
Data structures and algorithm analysis in java por
Data structures and algorithm analysis in javaData structures and algorithm analysis in java
Data structures and algorithm analysis in javaMuhammad Aleem Siddiqui
1.3K vistas601 diapositivas

La actualidad más candente(20)

Data Structure por sheraz1
Data StructureData Structure
Data Structure
sheraz11.1K vistas
Stacks in algorithems & data structure por faran nawaz
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
faran nawaz1.7K vistas
Recommendation algorithm using reinforcement learning por Arithmer Inc.
Recommendation algorithm using reinforcement learningRecommendation algorithm using reinforcement learning
Recommendation algorithm using reinforcement learning
Arithmer Inc.1.2K vistas
4. Recursion - Data Structures using C++ by Varsha Patil por widespreadpromotion
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion3.3K vistas
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil por widespreadpromotion
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
widespreadpromotion1.1K vistas
9. Searching & Sorting - Data Structures using C++ by Varsha Patil por widespreadpromotion
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil por widespreadpromotion
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion4.3K vistas
13. Indexing MTrees - Data Structures using C++ by Varsha Patil por widespreadpromotion
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion1.3K vistas
3. Stack - Data Structures using C++ by Varsha Patil por widespreadpromotion
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
widespreadpromotion2.9K vistas
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G... por Johann Petrak
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
Johann Petrak91 vistas
5. Queue - Data Structures using C++ by Varsha Patil por widespreadpromotion
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
widespreadpromotion1.7K vistas
Data structures algorithms_tutorial por HarikaReddy115
Data structures algorithms_tutorialData structures algorithms_tutorial
Data structures algorithms_tutorial
HarikaReddy11577 vistas
1. Fundamental Concept - Data Structures using C++ by Varsha Patil por widespreadpromotion
1. Fundamental Concept - Data Structures using C++ by Varsha Patil1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
widespreadpromotion5.2K vistas
8. Graph - Data Structures using C++ by Varsha Patil por widespreadpromotion
8. Graph - Data Structures using C++ by Varsha Patil8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil
widespreadpromotion3.2K vistas

Similar a Data Structures and Algorithm - Week 9 - Search Algorithms

Algorithm analysis (All in one) por
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
3.7K vistas148 diapositivas
Rahat &amp; juhith por
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhithRj Juhith
560 vistas31 diapositivas
Linear search-and-binary-search por
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-searchInternational Islamic University
14.8K vistas19 diapositivas
IRJET- A Survey on Different Searching Algorithms por
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET Journal
22 vistas5 diapositivas
Data Structures 8 por
Data Structures 8Data Structures 8
Data Structures 8Dr.Umadevi V
147 vistas36 diapositivas
Binary search algorithm.pptx por
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptxbhuvansaachi18
3 vistas12 diapositivas

Similar a Data Structures and Algorithm - Week 9 - Search Algorithms(20)

Algorithm analysis (All in one) por jehan1987
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
jehan19873.7K vistas
Rahat &amp; juhith por Rj Juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
Rj Juhith560 vistas
IRJET- A Survey on Different Searching Algorithms por IRJET Journal
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal22 vistas
ADSA orientation.pptx por Kiran Babar
ADSA orientation.pptxADSA orientation.pptx
ADSA orientation.pptx
Kiran Babar17 vistas
04-Data-Analysis-Overview.pptx por Shree Shree
04-Data-Analysis-Overview.pptx04-Data-Analysis-Overview.pptx
04-Data-Analysis-Overview.pptx
Shree Shree16 vistas
Algorithm 8th lecture linear & binary search(2).pptx por Aftabali702240
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali70224018 vistas
Unit II_Searching and Sorting Algorithms.ppt por HODElex
Unit II_Searching and Sorting Algorithms.pptUnit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.ppt
HODElex3 vistas
A REVIEW ON COMPARISION OF BINARY SEARCH AND LINEAR SEARCH por Daniel Wachtel
A REVIEW ON COMPARISION OF BINARY SEARCH AND LINEAR SEARCHA REVIEW ON COMPARISION OF BINARY SEARCH AND LINEAR SEARCH
A REVIEW ON COMPARISION OF BINARY SEARCH AND LINEAR SEARCH
Daniel Wachtel2 vistas
Searching and Sorting Techniques in Data Structure por Balwant Gorad
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad13.3K vistas
04 Classification in Data Mining por Valerii Klymchuk
04 Classification in Data Mining04 Classification in Data Mining
04 Classification in Data Mining
Valerii Klymchuk11.6K vistas
4.1 sequentioal search por Krish_ver2
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
Krish_ver2518 vistas
Searching techniques in Data Structure And Algorithm por 03446940736
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
0344694073610K vistas

Más de Ferdin Joe John Joseph PhD

Invited Talk DGTiCon 2022 por
Invited Talk DGTiCon 2022Invited Talk DGTiCon 2022
Invited Talk DGTiCon 2022Ferdin Joe John Joseph PhD
160 vistas27 diapositivas
Week 12: Cloud AI- DSA 441 Cloud Computing por
Week 12: Cloud AI- DSA 441 Cloud ComputingWeek 12: Cloud AI- DSA 441 Cloud Computing
Week 12: Cloud AI- DSA 441 Cloud ComputingFerdin Joe John Joseph PhD
199 vistas23 diapositivas
Week 11: Cloud Native- DSA 441 Cloud Computing por
Week 11: Cloud Native- DSA 441 Cloud ComputingWeek 11: Cloud Native- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud ComputingFerdin Joe John Joseph PhD
124 vistas42 diapositivas
Week 10: Cloud Security- DSA 441 Cloud Computing por
Week 10: Cloud Security- DSA 441 Cloud ComputingWeek 10: Cloud Security- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud ComputingFerdin Joe John Joseph PhD
115 vistas43 diapositivas
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing por
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud ComputingWeek 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud ComputingFerdin Joe John Joseph PhD
168 vistas30 diapositivas
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing por
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud ComputingWeek 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud ComputingFerdin Joe John Joseph PhD
187 vistas30 diapositivas

Más de Ferdin Joe John Joseph PhD(20)

Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co... por Ferdin Joe John Joseph PhD
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu... por Ferdin Joe John Joseph PhD
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C... por Ferdin Joe John Joseph PhD
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...

Último

[DSC Europe 23][AI:CSI] Dragan Pleskonjic - AI Impact on Cybersecurity and P... por
[DSC Europe 23][AI:CSI]  Dragan Pleskonjic - AI Impact on Cybersecurity and P...[DSC Europe 23][AI:CSI]  Dragan Pleskonjic - AI Impact on Cybersecurity and P...
[DSC Europe 23][AI:CSI] Dragan Pleskonjic - AI Impact on Cybersecurity and P...DataScienceConferenc1
8 vistas36 diapositivas
[DSC Europe 23] Luca Morena - From Psychohistory to Curious Machines por
[DSC Europe 23] Luca Morena - From Psychohistory to Curious Machines[DSC Europe 23] Luca Morena - From Psychohistory to Curious Machines
[DSC Europe 23] Luca Morena - From Psychohistory to Curious MachinesDataScienceConferenc1
5 vistas20 diapositivas
SUPER STORE SQL PROJECT.pptx por
SUPER STORE SQL PROJECT.pptxSUPER STORE SQL PROJECT.pptx
SUPER STORE SQL PROJECT.pptxkhan888620
13 vistas16 diapositivas
Dr. Ousmane Badiane-2023 ReSAKSS Conference por
Dr. Ousmane Badiane-2023 ReSAKSS ConferenceDr. Ousmane Badiane-2023 ReSAKSS Conference
Dr. Ousmane Badiane-2023 ReSAKSS ConferenceAKADEMIYA2063
5 vistas34 diapositivas
[DSC Europe 23] Stefan Mrsic_Goran Savic - Evolving Technology Excellence.pptx por
[DSC Europe 23] Stefan Mrsic_Goran Savic - Evolving Technology Excellence.pptx[DSC Europe 23] Stefan Mrsic_Goran Savic - Evolving Technology Excellence.pptx
[DSC Europe 23] Stefan Mrsic_Goran Savic - Evolving Technology Excellence.pptxDataScienceConferenc1
9 vistas16 diapositivas
[DSC Europe 23] Rania Wazir - Opening up the box: the complexity of human int... por
[DSC Europe 23] Rania Wazir - Opening up the box: the complexity of human int...[DSC Europe 23] Rania Wazir - Opening up the box: the complexity of human int...
[DSC Europe 23] Rania Wazir - Opening up the box: the complexity of human int...DataScienceConferenc1
5 vistas17 diapositivas

Último(20)

[DSC Europe 23][AI:CSI] Dragan Pleskonjic - AI Impact on Cybersecurity and P... por DataScienceConferenc1
[DSC Europe 23][AI:CSI]  Dragan Pleskonjic - AI Impact on Cybersecurity and P...[DSC Europe 23][AI:CSI]  Dragan Pleskonjic - AI Impact on Cybersecurity and P...
[DSC Europe 23][AI:CSI] Dragan Pleskonjic - AI Impact on Cybersecurity and P...
[DSC Europe 23] Luca Morena - From Psychohistory to Curious Machines por DataScienceConferenc1
[DSC Europe 23] Luca Morena - From Psychohistory to Curious Machines[DSC Europe 23] Luca Morena - From Psychohistory to Curious Machines
[DSC Europe 23] Luca Morena - From Psychohistory to Curious Machines
SUPER STORE SQL PROJECT.pptx por khan888620
SUPER STORE SQL PROJECT.pptxSUPER STORE SQL PROJECT.pptx
SUPER STORE SQL PROJECT.pptx
khan88862013 vistas
Dr. Ousmane Badiane-2023 ReSAKSS Conference por AKADEMIYA2063
Dr. Ousmane Badiane-2023 ReSAKSS ConferenceDr. Ousmane Badiane-2023 ReSAKSS Conference
Dr. Ousmane Badiane-2023 ReSAKSS Conference
AKADEMIYA20635 vistas
[DSC Europe 23] Stefan Mrsic_Goran Savic - Evolving Technology Excellence.pptx por DataScienceConferenc1
[DSC Europe 23] Stefan Mrsic_Goran Savic - Evolving Technology Excellence.pptx[DSC Europe 23] Stefan Mrsic_Goran Savic - Evolving Technology Excellence.pptx
[DSC Europe 23] Stefan Mrsic_Goran Savic - Evolving Technology Excellence.pptx
[DSC Europe 23] Rania Wazir - Opening up the box: the complexity of human int... por DataScienceConferenc1
[DSC Europe 23] Rania Wazir - Opening up the box: the complexity of human int...[DSC Europe 23] Rania Wazir - Opening up the box: the complexity of human int...
[DSC Europe 23] Rania Wazir - Opening up the box: the complexity of human int...
CRIJ4385_Death Penalty_F23.pptx por yvettemm100
CRIJ4385_Death Penalty_F23.pptxCRIJ4385_Death Penalty_F23.pptx
CRIJ4385_Death Penalty_F23.pptx
yvettemm1007 vistas
OECD-Persol Holdings Workshop on Advancing Employee Well-being in Business an... por StatsCommunications
OECD-Persol Holdings Workshop on Advancing Employee Well-being in Business an...OECD-Persol Holdings Workshop on Advancing Employee Well-being in Business an...
OECD-Persol Holdings Workshop on Advancing Employee Well-being in Business an...
6498-Butun_Beyinli_Cocuq-Daniel_J.Siegel-Tina_Payne_Bryson-2011-259s.pdf por 10urkyr34
6498-Butun_Beyinli_Cocuq-Daniel_J.Siegel-Tina_Payne_Bryson-2011-259s.pdf6498-Butun_Beyinli_Cocuq-Daniel_J.Siegel-Tina_Payne_Bryson-2011-259s.pdf
6498-Butun_Beyinli_Cocuq-Daniel_J.Siegel-Tina_Payne_Bryson-2011-259s.pdf
10urkyr346 vistas
[DSC Europe 23] Spela Poklukar & Tea Brasanac - Retrieval Augmented Generation por DataScienceConferenc1
[DSC Europe 23] Spela Poklukar & Tea Brasanac - Retrieval Augmented Generation[DSC Europe 23] Spela Poklukar & Tea Brasanac - Retrieval Augmented Generation
[DSC Europe 23] Spela Poklukar & Tea Brasanac - Retrieval Augmented Generation
[DSC Europe 23][DigiHealth] Muthu Ramachandran AI and Blockchain Framework fo... por DataScienceConferenc1
[DSC Europe 23][DigiHealth] Muthu Ramachandran AI and Blockchain Framework fo...[DSC Europe 23][DigiHealth] Muthu Ramachandran AI and Blockchain Framework fo...
[DSC Europe 23][DigiHealth] Muthu Ramachandran AI and Blockchain Framework fo...
[DSC Europe 23][AI:CSI] Aleksa Stojanovic - Applying AI for Threat Detection ... por DataScienceConferenc1
[DSC Europe 23][AI:CSI] Aleksa Stojanovic - Applying AI for Threat Detection ...[DSC Europe 23][AI:CSI] Aleksa Stojanovic - Applying AI for Threat Detection ...
[DSC Europe 23][AI:CSI] Aleksa Stojanovic - Applying AI for Threat Detection ...
Short Story Assignment by Kelly Nguyen por kellynguyen01
Short Story Assignment by Kelly NguyenShort Story Assignment by Kelly Nguyen
Short Story Assignment by Kelly Nguyen
kellynguyen0119 vistas
OPPOTUS - Malaysians on Malaysia 3Q2023.pdf por Oppotus
OPPOTUS - Malaysians on Malaysia 3Q2023.pdfOPPOTUS - Malaysians on Malaysia 3Q2023.pdf
OPPOTUS - Malaysians on Malaysia 3Q2023.pdf
Oppotus23 vistas
4_4_WP_4_06_ND_Model.pptx por d6fmc6kwd4
4_4_WP_4_06_ND_Model.pptx4_4_WP_4_06_ND_Model.pptx
4_4_WP_4_06_ND_Model.pptx
d6fmc6kwd47 vistas
DGST Methodology Presentation.pdf por maddierlegum
DGST Methodology Presentation.pdfDGST Methodology Presentation.pdf
DGST Methodology Presentation.pdf
maddierlegum5 vistas
CRM stick or twist workshop por info828217
CRM stick or twist workshopCRM stick or twist workshop
CRM stick or twist workshop
info82821712 vistas
[DSC Europe 23] Ales Gros - Quantum and Today s security with Quantum.pdf por DataScienceConferenc1
[DSC Europe 23] Ales Gros - Quantum and Today s security with Quantum.pdf[DSC Europe 23] Ales Gros - Quantum and Today s security with Quantum.pdf
[DSC Europe 23] Ales Gros - Quantum and Today s security with Quantum.pdf

Data Structures and Algorithm - Week 9 - Search Algorithms

  • 1. Data Structures and Algorithms Week 9: Search Algorithms Ferdin Joe John Joseph, PhD Faculty of Information Technology Thai-Nichi Institute of Technology, Bangkok
  • 2. Week 9 • Linear Search • Binary Search • Implementation in Java Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 2
  • 3. Linear Search • Searching is the process of determining whether or not a given value exists in a data structure or a storage media. • We discuss two searching methods on one-dimensional arrays: linear search and binary search. • The linear (or sequential) search algorithm on an array is: • Sequentially scan the array, comparing each array item with the searched value. • If a match is found; return the index of the matched element; otherwise return –1. • Note: linear search can be applied to both sorted and unsorted arrays. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai- 3
  • 4. Linear Search • The algorithm translates to the following Java method: public static int linearSearch(Object[] array, Object key) { for(int k = 0; k < array.length; k++) if(array[k].equals(key)) return k; return -1; } Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai- 4
  • 5. Binary Search • Binary search uses a recursive method to search an array to find a specified value • The array must be a sorted array: a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex] • If the value is found, its index is returned • If the value is not found, -1 is returned • Note: Each execution of the recursive method reduces the search space by about a half Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 5
  • 6. Binary Search • An algorithm to solve this task looks at the middle of the array or array segment first • If the value looked for is smaller than the value in the middle of the array • Then the second half of the array or array segment can be ignored • This strategy is then applied to the first half of the array or array segment Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 6
  • 7. Binary Search • If the value looked for is larger than the value in the middle of the array or array segment • Then the first half of the array or array segment can be ignored • This strategy is then applied to the second half of the array or array segment • If the value looked for is at the middle of the array or array segment, then it has been found • If the entire array (or array segment) has been searched in this way without finding the value, then it is not in the array Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 7
  • 8. Pseudocode for Binary Search Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 8
  • 9. Recursive Method for Binary Search Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 9
  • 10. Execution of the Method search (Part 1 of 2) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 10
  • 11. Execution of the Method search (Part 1 of 2) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 11
  • 12. Checking the search Method 1. There is no infinite recursion • On each recursive call, the value of first is increased, or the value of last is decreased • If the chain of recursive calls does not end in some other way, then eventually the method will be called with first larger than last Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 12
  • 13. Checking the search Method 2. Each stopping case performs the correct action for that case • If first > last, there are no array elements between a[first] and a[last], so key is not in this segment of the array, and result is correctly set to -1 • If key == a[mid], result is correctly set to mid Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 13
  • 14. Checking the search Method 3. For each of the cases that involve recursion, if all recursive calls perform their actions correctly, then the entire case performs correctly • If key < a[mid], then key must be one of the elements a[first] through a[mid-1], or it is not in the array • The method should then search only those elements, which it does • The recursive call is correct, therefore the entire action is correct Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 14
  • 15. Checking the search Method • If key > a[mid], then key must be one of the elements a[mid+1] through a[last], or it is not in the array • The method should then search only those elements, which it does • The recursive call is correct, therefore the entire action is correct The method search passes all three tests: Therefore, it is a good recursive method definition Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 15
  • 16. Efficiency of Binary Search • The binary search algorithm is extremely fast compared to an algorithm that tries all array elements in order • About half the array is eliminated from consideration right at the start • Then a quarter of the array, then an eighth of the array, and so forth Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 16
  • 17. Efficiency of Binary Search • Given an array with 1,000 elements, the binary search will only need to compare about 10 array elements to the key value, as compared to an average of 500 for a serial search algorithm • The binary search algorithm has a worst-case running time that is logarithmic: O(log n) • A serial search algorithm is linear: O(n) • If desired, the recursive version of the method search can be converted to an iterative version that will run more efficiently Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 17
  • 18. Iterative Version of Binary Search (Part 1 of 2) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 18
  • 19. Iterative Version of Binary Search (Part 2 of 2) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 19
  • 20. Next Week Sorting Algorithms Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 20