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

More Related Content

PDF
Data Structures and Algorithm - Week 11 - Algorithm Analysis
PDF
Data Structures and Algorithm - Week 4 - Trees, Binary Trees
PDF
Data Structures and Algorithm - Week 3 - Stacks and Queues
PDF
Week 1 - Data Structures and Algorithms
PDF
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees
PDF
Week 2 - Data Structures and Algorithms
PDF
Data Structures and Algorithm - Week 5 - AVL Trees
PDF
Data Structures and Algorithm - Week 6 - Red Black Trees
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 4 - Trees, Binary Trees
Data Structures and Algorithm - Week 3 - Stacks and Queues
Week 1 - Data Structures and Algorithms
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees
Week 2 - Data Structures and Algorithms
Data Structures and Algorithm - Week 5 - AVL Trees
Data Structures and Algorithm - Week 6 - Red Black Trees

What's hot (20)

PDF
geekgap.io webinar #1
PPS
Data Structure
PPT
Stacks in algorithems & data structure
PPTX
Introduction to data structure and algorithms
PDF
Recommendation algorithm using reinforcement learning
PDF
Data structures and algorithm analysis in java
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
PPTX
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
PDF
Test for AI model
PPTX
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
PPTX
7. Tree - Data Structures using C++ by Varsha Patil
PDF
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
PPTX
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
PPTX
3. Stack - Data Structures using C++ by Varsha Patil
PDF
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
PDF
Exposé Ontology
PPTX
5. Queue - Data Structures using C++ by Varsha Patil
PDF
Data structures algorithms_tutorial
PPTX
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
PPTX
8. Graph - Data Structures using C++ by Varsha Patil
geekgap.io webinar #1
Data Structure
Stacks in algorithems & data structure
Introduction to data structure and algorithms
Recommendation algorithm using reinforcement learning
Data structures and algorithm analysis in java
4. Recursion - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
Test for AI model
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
Exposé Ontology
5. Queue - Data Structures using C++ by Varsha Patil
Data structures algorithms_tutorial
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil
Ad

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

PPTX
seaching internal 2 ppt
PPTX
Data Structures Unit 2 FINAL presentation.pptx
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PDF
UNIT IV -Data Structures.pdf
PPTX
Data structure Unit - II Searching and Sorting.pptx
PPTX
seaching internal 2 ppt.pptx
PDF
Searching
PPTX
Searching in Data Structure
DOCX
MODULE 5-Searching and-sorting
PDF
SearchingSearchingSearchingSearchingvSearchingSearchingSearchingSearchingSear...
PPTX
Searching searching in in arrays arrays.pptx
PPTX
Searching & Algorithms IN DATA STRUCTURES
PPTX
Rahat &amp; juhith
PPTX
Searching and Sorting Algorithms in Data Structures
PPTX
data_structure_Chapter two_computer.pptx
PDF
advanced searching and sorting.pdf
PPTX
Searching and Sorting Unit II Part I.pptx
PPTX
Binary search
PPTX
Searching linear &amp; binary search
PPTX
Government Polytechnic Lucknow.pptx
seaching internal 2 ppt
Data Structures Unit 2 FINAL presentation.pptx
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
UNIT IV -Data Structures.pdf
Data structure Unit - II Searching and Sorting.pptx
seaching internal 2 ppt.pptx
Searching
Searching in Data Structure
MODULE 5-Searching and-sorting
SearchingSearchingSearchingSearchingvSearchingSearchingSearchingSearchingSear...
Searching searching in in arrays arrays.pptx
Searching & Algorithms IN DATA STRUCTURES
Rahat &amp; juhith
Searching and Sorting Algorithms in Data Structures
data_structure_Chapter two_computer.pptx
advanced searching and sorting.pdf
Searching and Sorting Unit II Part I.pptx
Binary search
Searching linear &amp; binary search
Government Polytechnic Lucknow.pptx
Ad

More from Ferdin Joe John Joseph PhD (20)

PDF
Invited Talk DGTiCon 2022
PDF
Week 12: Cloud AI- DSA 441 Cloud Computing
PDF
Week 11: Cloud Native- DSA 441 Cloud Computing
PDF
Week 10: Cloud Security- DSA 441 Cloud Computing
PDF
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
PDF
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
PDF
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
PDF
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
PDF
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
PDF
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
PDF
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
PDF
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
PDF
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
PDF
Hadoop in Alibaba Cloud
PDF
Cloud Computing Essentials in Alibaba Cloud
PDF
Transforming deep into transformers – a computer vision approach
PDF
Week 11: Programming for Data Analysis
PDF
Week 10: Programming for Data Analysis
PDF
Week 9: Programming for Data Analysis
PDF
Week 8: Programming for Data Analysis
Invited Talk DGTiCon 2022
Week 12: Cloud AI- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
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...
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Hadoop in Alibaba Cloud
Cloud Computing Essentials in Alibaba Cloud
Transforming deep into transformers – a computer vision approach
Week 11: Programming for Data Analysis
Week 10: Programming for Data Analysis
Week 9: Programming for Data Analysis
Week 8: Programming for Data Analysis

Recently uploaded (20)

PDF
Fluorescence-microscope_Botany_detailed content
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PDF
Foundation of Data Science unit number two notes
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PDF
Mega Projects Data Mega Projects Data
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPT
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
Moving the Public Sector (Government) to a Digital Adoption
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
1_Introduction to advance data techniques.pptx
PDF
Launch Your Data Science Career in Kochi – 2025
PPTX
Computer network topology notes for revision
PPTX
Database Infoormation System (DBIS).pptx
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Fluorescence-microscope_Botany_detailed content
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Galatica Smart Energy Infrastructure Startup Pitch Deck
Foundation of Data Science unit number two notes
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
Mega Projects Data Mega Projects Data
Introduction-to-Cloud-ComputingFinal.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Miokarditis (Inflamasi pada Otot Jantung)
Moving the Public Sector (Government) to a Digital Adoption
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
1_Introduction to advance data techniques.pptx
Launch Your Data Science Career in Kochi – 2025
Computer network topology notes for revision
Database Infoormation System (DBIS).pptx
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.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