SlideShare a Scribd company logo
Analysis and Design of Algorithms
Searching Algorithms
Analysis and Design of Algorithms
Introduction
Linear Search
Binary Search
Jump Search
Interpolation Search
Analysis and Design of Algorithms
 Searching Algorithm is an algorithm made up of a series of
instructions that retrieves information stored within some data
structure, or calculated in the search space of a problem domain.
 There are many sorting algorithms, such as:
 Linear Search, Binary Search, Jump Search,
Interpolation Search, Exponential Search, Ternary
Search
Analysis and Design of Algorithms
Linear Search
Analysis and Design of Algorithms
 Linear Search is a method for finding a target value
within a list. It sequentially checks each element of
the list for the target value until a match is found or
until all the elements have been searched.
Analysis and Design of Algorithms
 Algorithm:
 Step1: Start from the leftmost element of array and one by one
compare x with each element of array.
 Step2: If x matches with an element, return the index.
 Step3: If x doesn’t match with any of elements, return -1.
Analysis and Design of Algorithms
 Assume the following Array:
 Search for 9
8 12 5 9 2
Analysis and Design of Algorithms
 Compare
 X=
8 12 5 9 2
9

i
Analysis and Design of Algorithms
 Compare
 X=
8 12 5 9 2
9

i
Analysis and Design of Algorithms
 Compare
 X=
8 12 5 9 2
9

i
Analysis and Design of Algorithms
 Compare
 X=
8 12 5 9 2
9

i
Analysis and Design of Algorithms
 Found at index = 3
8 12 5 9 2
Analysis and Design of Algorithms
 Python Code
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Time Complexity: O(n)
 Example of worst case: search for the last element
4 6 8 9 1
Analysis and Design of Algorithms
Binary Search
Analysis and Design of Algorithms
Binary Search is the most popular Search algorithm.
It is efficient and also one of the most commonly
used techniques that is used to solve problems.
Binary search use sorted array by repeatedly
dividing the search interval in half.
Analysis and Design of Algorithms
 Algorithm:
 Step1: Compare x with the middle element.
 Step2: If x matches with middle element, we return the mid index.
 Step3: Else If x is greater than the mid element, search on right half.
 Step4: Else If x is smaller than the mid element. search on left half.
Analysis and Design of Algorithms
 Assume the following Array:
 Search for 40
2 3 10 30 40 50 70
Analysis and Design of Algorithms
 Compare
 X =
2 3 10 30 40 50 70

L

R

mid
40
Analysis and Design of Algorithms
 Compare
 X =
2 3 10 30 40 50 70

L

R

mid
40
Analysis and Design of Algorithms
 Compare
 X =
2 3 10 30 40 50 70

L

R

mid
40
Analysis and Design of Algorithms
 x=40 , found at index = 4
2 3 10 30 40 50 70
Analysis and Design of Algorithms
 Iterative python implementation:
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Recursive Python implementation:
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Time Complexity: O(log2 n)
Analysis and Design of Algorithms
Jump Search
Analysis and Design of Algorithms
Jump Search is a searching algorithm for sorted
arrays. The basic idea is to check fewer elements
(than linear search) by jumping ahead by fixed steps
or skipping some elements in place of searching all
elements.
Analysis and Design of Algorithms
 Algorithm:
 Step1: Calculate Jump size
 Step2: Jump from index i to index i+jump
 Step3: If x = = arr[i+jump] return x
 Else jump back a step
 Step4: Perform linear search
Analysis and Design of Algorithms
 Assume the following sorted array:
 Search for 77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
Analysis and Design of Algorithms
 Calculate:
• Size of array n =16
• Jump size = sqrt(n)= 4
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
Analysis and Design of Algorithms
 Jump size = 4
 Search from index 0
 Compare index value with search number 0<77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
77
Analysis and Design of Algorithms
 Jump size = 4
 Jump from index 0 to index 3
 Compare index value with search number 2<77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
77
Analysis and Design of Algorithms
 Jump size = 4
 Jump from index 3 to index 6
 Compare index value with search number 8<77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
77
Analysis and Design of Algorithms
 Jump size = 4
 Jump from index 6 to index 9
 Compare index value with search number 34<77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
77
Analysis and Design of Algorithms
 Jump size = 4
 Jump from index 9 to index 12
 Compare index value with search number 89>77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
77
Analysis and Design of Algorithms
 jump back a step
 Perform linear search
 Compare found at index 11
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
77
Analysis and Design of Algorithms
 Time Complexity: O(sqrt(n))
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Python Code
Analysis and Design of Algorithms
Interpolation Search
Analysis and Design of Algorithms
The Interpolation Search is an improvement over Binary
Search for instances. On the other hand interpolation
search may go to different locations according the value of
key being searched.
Analysis and Design of Algorithms
 Algorithm:
 Step1: In a loop, calculate the value of “pos” using the position formula.
 Step2: If it is a match, return the index of the item, and exit.
 Step3: If the item is less than arr[pos], calculate the position of the left
sub-array. Otherwise calculate the same in the right sub-array.
 Step4: Repeat until a match is found or the sub-array reduces to zero.
Analysis and Design of Algorithms
// The idea of formula is to return higher value of pos
// when element to be searched is closer to arr[hi]. And
// smaller value when closer to arr[lo]
pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]
arr[] ==> Array where elements need to be searched
x ==> Element to be searched
lo ==> Starting index in arr[]
hi ==> Ending index in arr[]
Analysis and Design of Algorithms
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
Analysis and Design of Algorithms
 Assume the following sorted array:
 Search for x= 18
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
Analysis and Design of Algorithms
 Calculate pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]
 Lo=0, hi=14, arr[lo]=10, arr[hi]= 47
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
Analysis and Design of Algorithms
 Calculate pos = 3
 Compare with x=18
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
Analysis and Design of Algorithms
 Calculate pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]
 Lo=4, hi=14, arr[lo]=18, arr[hi]= 47
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
Analysis and Design of Algorithms
 Calculate pos = 4
 Compare with x=18 , found at index 4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
Analysis and Design of Algorithms
 Time Complexity: If elements are uniformly distributed, then O (log
log n)). In worst case it can take up to O(n).
Analysis and Design of Algorithms
 Python Code
Analysis and Design of Algorithms
 Python Code
Analysis and Design of Algorithms
facebook.com/mloey
mohamedloey@gmail.com
twitter.com/mloey
linkedin.com/in/mloey
mloey@fci.bu.edu.eg
mloey.github.io
Analysis and Design of Algorithms
www.YourCompany.com
© 2020 Companyname PowerPoint Business Theme. All Rights Reserved.
THANKS FOR
YOUR TIME

More Related Content

What's hot (20)

SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
Rajendra Dangwal
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
Nisha Soms
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
Daffodil International University
 
Big o notation
Big o notationBig o notation
Big o notation
hamza mushtaq
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
International Islamic University
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
Jeanie Arnoco
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
Ashutosh Satapathy
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
Muhammad Muzammal
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
Saurabh Kumar
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
Akshaya Arunan
 
Red black tree
Red black treeRed black tree
Red black tree
Rajendran
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
Syed Zaid Irshad
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
Tirthika Bandi
 

Similar to Algorithms Lecture 6: Searching Algorithms (20)

UNEC__1683196273.pptx
UNEC__1683196273.pptxUNEC__1683196273.pptx
UNEC__1683196273.pptx
huseynmusayev2
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
Misssaxena
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
ASMAALWADEE2
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
mbadhi barnabas
 
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
CHAP 3 ALGORITHM for infomatique ingenieure .pptCHAP 3 ALGORITHM for infomatique ingenieure .ppt
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
FarahHarrathi1
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
Archana Burujwale
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
Algorithms Intro Lecture
Algorithms Intro LectureAlgorithms Intro Lecture
Algorithms Intro Lecture
Ira D
 
21-algorithms (1).ppt
21-algorithms (1).ppt21-algorithms (1).ppt
21-algorithms (1).ppt
DaniloMislosAlbay
 
Lect-2.pptx
Lect-2.pptxLect-2.pptx
Lect-2.pptx
mrizwan38
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
Madurai Kamaraj University Madurai Tamil Nadu India
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
ashwinraiyani1
 
Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++
Johnny Jean Tigas
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptxSORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESPYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
vanithasivdc
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms master
Hossam Hassan
 
Introduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering studentsIntroduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering students
geraldjamesignacio2
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
Misssaxena
 
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
CHAP 3 ALGORITHM for infomatique ingenieure .pptCHAP 3 ALGORITHM for infomatique ingenieure .ppt
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
FarahHarrathi1
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
Algorithms Intro Lecture
Algorithms Intro LectureAlgorithms Intro Lecture
Algorithms Intro Lecture
Ira D
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
 
Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++
Johnny Jean Tigas
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptxSORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESPYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
vanithasivdc
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms master
Hossam Hassan
 
Introduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering studentsIntroduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering students
geraldjamesignacio2
 
Ad

More from Mohamed Loey (20)

Lecture 6: Deep Learning Applications
Lecture 6: Deep Learning ApplicationsLecture 6: Deep Learning Applications
Lecture 6: Deep Learning Applications
Mohamed Loey
 
Lecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network ModelsLecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network Models
Mohamed Loey
 
Lecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksLecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning Frameworks
Mohamed Loey
 
Lecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural NetworksLecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural Networks
Mohamed Loey
 
Lecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural NetworksLecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural Networks
Mohamed Loey
 
Lecture 2: Artificial Neural Network
Lecture 2: Artificial Neural NetworkLecture 2: Artificial Neural Network
Lecture 2: Artificial Neural Network
Mohamed Loey
 
Lecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer VisionLecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer Vision
Mohamed Loey
 
Design of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer DiseasesDesign of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer Diseases
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1
Mohamed Loey
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern Algorithms
Mohamed Loey
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
Mohamed Loey
 
Convolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep LearningConvolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep Learning
Mohamed Loey
 
Deep Learning - Overview of my work II
Deep Learning - Overview of my work IIDeep Learning - Overview of my work II
Deep Learning - Overview of my work II
Mohamed Loey
 
Computer Security Lecture 7: RSA
Computer Security Lecture 7: RSAComputer Security Lecture 7: RSA
Computer Security Lecture 7: RSA
Mohamed Loey
 
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption StandardComputer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Mohamed Loey
 
Computer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary MaterialComputer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary Material
Mohamed Loey
 
PMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration ManagementPMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration Management
Mohamed Loey
 
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption StandardComputer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Mohamed Loey
 
Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2
Mohamed Loey
 
Lecture 6: Deep Learning Applications
Lecture 6: Deep Learning ApplicationsLecture 6: Deep Learning Applications
Lecture 6: Deep Learning Applications
Mohamed Loey
 
Lecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network ModelsLecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network Models
Mohamed Loey
 
Lecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksLecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning Frameworks
Mohamed Loey
 
Lecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural NetworksLecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural Networks
Mohamed Loey
 
Lecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural NetworksLecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural Networks
Mohamed Loey
 
Lecture 2: Artificial Neural Network
Lecture 2: Artificial Neural NetworkLecture 2: Artificial Neural Network
Lecture 2: Artificial Neural Network
Mohamed Loey
 
Lecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer VisionLecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer Vision
Mohamed Loey
 
Design of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer DiseasesDesign of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer Diseases
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1
Mohamed Loey
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern Algorithms
Mohamed Loey
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
Mohamed Loey
 
Convolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep LearningConvolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep Learning
Mohamed Loey
 
Deep Learning - Overview of my work II
Deep Learning - Overview of my work IIDeep Learning - Overview of my work II
Deep Learning - Overview of my work II
Mohamed Loey
 
Computer Security Lecture 7: RSA
Computer Security Lecture 7: RSAComputer Security Lecture 7: RSA
Computer Security Lecture 7: RSA
Mohamed Loey
 
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption StandardComputer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Mohamed Loey
 
Computer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary MaterialComputer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary Material
Mohamed Loey
 
PMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration ManagementPMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration Management
Mohamed Loey
 
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption StandardComputer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Mohamed Loey
 
Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2
Mohamed Loey
 
Ad

Recently uploaded (20)

Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 

Algorithms Lecture 6: Searching Algorithms

  • 1. Analysis and Design of Algorithms Searching Algorithms
  • 2. Analysis and Design of Algorithms Introduction Linear Search Binary Search Jump Search Interpolation Search
  • 3. Analysis and Design of Algorithms  Searching Algorithm is an algorithm made up of a series of instructions that retrieves information stored within some data structure, or calculated in the search space of a problem domain.  There are many sorting algorithms, such as:  Linear Search, Binary Search, Jump Search, Interpolation Search, Exponential Search, Ternary Search
  • 4. Analysis and Design of Algorithms Linear Search
  • 5. Analysis and Design of Algorithms  Linear Search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.
  • 6. Analysis and Design of Algorithms  Algorithm:  Step1: Start from the leftmost element of array and one by one compare x with each element of array.  Step2: If x matches with an element, return the index.  Step3: If x doesn’t match with any of elements, return -1.
  • 7. Analysis and Design of Algorithms  Assume the following Array:  Search for 9 8 12 5 9 2
  • 8. Analysis and Design of Algorithms  Compare  X= 8 12 5 9 2 9  i
  • 9. Analysis and Design of Algorithms  Compare  X= 8 12 5 9 2 9  i
  • 10. Analysis and Design of Algorithms  Compare  X= 8 12 5 9 2 9  i
  • 11. Analysis and Design of Algorithms  Compare  X= 8 12 5 9 2 9  i
  • 12. Analysis and Design of Algorithms  Found at index = 3 8 12 5 9 2
  • 13. Analysis and Design of Algorithms  Python Code
  • 14. Analysis and Design of Algorithms
  • 15. Analysis and Design of Algorithms  Time Complexity: O(n)  Example of worst case: search for the last element 4 6 8 9 1
  • 16. Analysis and Design of Algorithms Binary Search
  • 17. Analysis and Design of Algorithms Binary Search is the most popular Search algorithm. It is efficient and also one of the most commonly used techniques that is used to solve problems. Binary search use sorted array by repeatedly dividing the search interval in half.
  • 18. Analysis and Design of Algorithms  Algorithm:  Step1: Compare x with the middle element.  Step2: If x matches with middle element, we return the mid index.  Step3: Else If x is greater than the mid element, search on right half.  Step4: Else If x is smaller than the mid element. search on left half.
  • 19. Analysis and Design of Algorithms  Assume the following Array:  Search for 40 2 3 10 30 40 50 70
  • 20. Analysis and Design of Algorithms  Compare  X = 2 3 10 30 40 50 70  L  R  mid 40
  • 21. Analysis and Design of Algorithms  Compare  X = 2 3 10 30 40 50 70  L  R  mid 40
  • 22. Analysis and Design of Algorithms  Compare  X = 2 3 10 30 40 50 70  L  R  mid 40
  • 23. Analysis and Design of Algorithms  x=40 , found at index = 4 2 3 10 30 40 50 70
  • 24. Analysis and Design of Algorithms  Iterative python implementation:
  • 25. Analysis and Design of Algorithms
  • 26. Analysis and Design of Algorithms  Recursive Python implementation:
  • 27. Analysis and Design of Algorithms
  • 28. Analysis and Design of Algorithms  Time Complexity: O(log2 n)
  • 29. Analysis and Design of Algorithms Jump Search
  • 30. Analysis and Design of Algorithms Jump Search is a searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some elements in place of searching all elements.
  • 31. Analysis and Design of Algorithms  Algorithm:  Step1: Calculate Jump size  Step2: Jump from index i to index i+jump  Step3: If x = = arr[i+jump] return x  Else jump back a step  Step4: Perform linear search
  • 32. Analysis and Design of Algorithms  Assume the following sorted array:  Search for 77 0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
  • 33. Analysis and Design of Algorithms  Calculate: • Size of array n =16 • Jump size = sqrt(n)= 4 0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
  • 34. Analysis and Design of Algorithms  Jump size = 4  Search from index 0  Compare index value with search number 0<77 0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110 77
  • 35. Analysis and Design of Algorithms  Jump size = 4  Jump from index 0 to index 3  Compare index value with search number 2<77 0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110 77
  • 36. Analysis and Design of Algorithms  Jump size = 4  Jump from index 3 to index 6  Compare index value with search number 8<77 0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110 77
  • 37. Analysis and Design of Algorithms  Jump size = 4  Jump from index 6 to index 9  Compare index value with search number 34<77 0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110 77
  • 38. Analysis and Design of Algorithms  Jump size = 4  Jump from index 9 to index 12  Compare index value with search number 89>77 0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110 77
  • 39. Analysis and Design of Algorithms  jump back a step  Perform linear search  Compare found at index 11 0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110 77
  • 40. Analysis and Design of Algorithms  Time Complexity: O(sqrt(n))
  • 41. Analysis and Design of Algorithms
  • 42. Analysis and Design of Algorithms  Python Code
  • 43. Analysis and Design of Algorithms Interpolation Search
  • 44. Analysis and Design of Algorithms The Interpolation Search is an improvement over Binary Search for instances. On the other hand interpolation search may go to different locations according the value of key being searched.
  • 45. Analysis and Design of Algorithms  Algorithm:  Step1: In a loop, calculate the value of “pos” using the position formula.  Step2: If it is a match, return the index of the item, and exit.  Step3: If the item is less than arr[pos], calculate the position of the left sub-array. Otherwise calculate the same in the right sub-array.  Step4: Repeat until a match is found or the sub-array reduces to zero.
  • 46. Analysis and Design of Algorithms // The idea of formula is to return higher value of pos // when element to be searched is closer to arr[hi]. And // smaller value when closer to arr[lo] pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ] arr[] ==> Array where elements need to be searched x ==> Element to be searched lo ==> Starting index in arr[] hi ==> Ending index in arr[]
  • 47. Analysis and Design of Algorithms 10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
  • 48. Analysis and Design of Algorithms  Assume the following sorted array:  Search for x= 18 10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
  • 49. Analysis and Design of Algorithms  Calculate pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]  Lo=0, hi=14, arr[lo]=10, arr[hi]= 47 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
  • 50. Analysis and Design of Algorithms  Calculate pos = 3  Compare with x=18 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
  • 51. Analysis and Design of Algorithms  Calculate pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]  Lo=4, hi=14, arr[lo]=18, arr[hi]= 47 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
  • 52. Analysis and Design of Algorithms  Calculate pos = 4  Compare with x=18 , found at index 4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
  • 53. Analysis and Design of Algorithms  Time Complexity: If elements are uniformly distributed, then O (log log n)). In worst case it can take up to O(n).
  • 54. Analysis and Design of Algorithms  Python Code
  • 55. Analysis and Design of Algorithms  Python Code
  • 56. Analysis and Design of Algorithms facebook.com/mloey [email protected] twitter.com/mloey linkedin.com/in/mloey [email protected] mloey.github.io
  • 57. Analysis and Design of Algorithms www.YourCompany.com © 2020 Companyname PowerPoint Business Theme. All Rights Reserved. THANKS FOR YOUR TIME