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

PDF
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
PDF
Algorithms Lecture 5: Sorting Algorithms II
Mohamed Loey
 
PDF
Algorithms Lecture 3: Analysis of Algorithms II
Mohamed Loey
 
PDF
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
PDF
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
 
PDF
Algorithms Lecture 7: Graph Algorithms
Mohamed Loey
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PDF
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Algorithms Lecture 5: Sorting Algorithms II
Mohamed Loey
 
Algorithms Lecture 3: Analysis of Algorithms II
Mohamed Loey
 
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
 
Algorithms Lecture 7: Graph Algorithms
Mohamed Loey
 
Sorting Algorithms
Mohammed Hussein
 
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 

What's hot (20)

PDF
linear search and binary search
Zia Ush Shamszaman
 
PPTX
Graph in data structure
Abrish06
 
PPTX
Sorting Algorithms
Pranay Neema
 
PPT
Time complexity
Katang Isip
 
PPTX
Quick sort-Data Structure
Jeanie Arnoco
 
PPTX
Data structures and algorithms
Julie Iskander
 
PPT
Dinive conquer algorithm
Mohd Arif
 
PPTX
Linear search-and-binary-search
International Islamic University
 
PPTX
Selection sorting
Himanshu Kesharwani
 
PPTX
Breadth First Search & Depth First Search
Kevin Jadiya
 
PPT
Searching algorithms
Trupti Agrawal
 
PPTX
Graph traversals in Data Structures
Anandhasilambarasan D
 
PPTX
heap Sort Algorithm
Lemia Algmri
 
PPTX
Linear Search Presentation
Markajul Hasnain Alif
 
PPTX
Asymptotic notations
Nikhil Sharma
 
PPTX
Introduction to pandas
Piyush rai
 
PPTX
Insertion sort
MYER301
 
PPTX
Queue in Data Structure
Janki Shah
 
PPTX
Lexical Analysis - Compiler Design
Akhil Kaushik
 
PPTX
Priority Queue in Data Structure
Meghaj Mallick
 
linear search and binary search
Zia Ush Shamszaman
 
Graph in data structure
Abrish06
 
Sorting Algorithms
Pranay Neema
 
Time complexity
Katang Isip
 
Quick sort-Data Structure
Jeanie Arnoco
 
Data structures and algorithms
Julie Iskander
 
Dinive conquer algorithm
Mohd Arif
 
Linear search-and-binary-search
International Islamic University
 
Selection sorting
Himanshu Kesharwani
 
Breadth First Search & Depth First Search
Kevin Jadiya
 
Searching algorithms
Trupti Agrawal
 
Graph traversals in Data Structures
Anandhasilambarasan D
 
heap Sort Algorithm
Lemia Algmri
 
Linear Search Presentation
Markajul Hasnain Alif
 
Asymptotic notations
Nikhil Sharma
 
Introduction to pandas
Piyush rai
 
Insertion sort
MYER301
 
Queue in Data Structure
Janki Shah
 
Lexical Analysis - Compiler Design
Akhil Kaushik
 
Priority Queue in Data Structure
Meghaj Mallick
 
Ad

Similar to Algorithms Lecture 6: Searching Algorithms (20)

PPTX
UNEC__1683196273.pptx
huseynmusayev2
 
PPTX
AoA Lec Design of algorithm spresentation
HamzaSadaat
 
PPT
chapter 1
yatheesha
 
PPTX
Algorithms : Introduction and Analysis
Dhrumil Patel
 
PPT
Searching.ppt
SpammemeLmao
 
PPTX
Algorithm & data structures lec4&5
Abdul Khan
 
PPT
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
FarahHarrathi1
 
PPT
Lec1.ppt
ssuser8bddb2
 
PPT
ds 2-Arrays and its types and operations
kavita20193
 
PPTX
Module 2 Design Analysis and Algorithms
Cool Guy
 
PPT
Data Structure and Algorithms Arrays
ManishPrajapati78
 
PDF
Design and Analysis Algorithms.pdf
HarshNagda5
 
PPT
ds 2Arrays.ppt
AlliVinay1
 
PPT
Lecture 01-2.ppt
RaoHamza24
 
PDF
complexity analysis.pdf
pasinduneshan
 
PPT
467719275-Lecture-01 designe and alg.ppt
satyawrat1
 
PPT
Data structure and problem solving ch03.ppt
Ping261512
 
PPT
UnitI (1).ppt
DSirisha2
 
PPT
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
DaveCalapis3
 
PPTX
ADSA orientation.pptx
Kiran Babar
 
UNEC__1683196273.pptx
huseynmusayev2
 
AoA Lec Design of algorithm spresentation
HamzaSadaat
 
chapter 1
yatheesha
 
Algorithms : Introduction and Analysis
Dhrumil Patel
 
Searching.ppt
SpammemeLmao
 
Algorithm & data structures lec4&5
Abdul Khan
 
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
FarahHarrathi1
 
Lec1.ppt
ssuser8bddb2
 
ds 2-Arrays and its types and operations
kavita20193
 
Module 2 Design Analysis and Algorithms
Cool Guy
 
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Design and Analysis Algorithms.pdf
HarshNagda5
 
ds 2Arrays.ppt
AlliVinay1
 
Lecture 01-2.ppt
RaoHamza24
 
complexity analysis.pdf
pasinduneshan
 
467719275-Lecture-01 designe and alg.ppt
satyawrat1
 
Data structure and problem solving ch03.ppt
Ping261512
 
UnitI (1).ppt
DSirisha2
 
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
DaveCalapis3
 
ADSA orientation.pptx
Kiran Babar
 
Ad

More from Mohamed Loey (20)

PDF
Lecture 6: Deep Learning Applications
Mohamed Loey
 
PDF
Lecture 5: Convolutional Neural Network Models
Mohamed Loey
 
PDF
Lecture 4: Deep Learning Frameworks
Mohamed Loey
 
PDF
Lecture 4: How it Works: Convolutional Neural Networks
Mohamed Loey
 
PPTX
Lecture 3: Convolutional Neural Networks
Mohamed Loey
 
PDF
Lecture 2: Artificial Neural Network
Mohamed Loey
 
PDF
Lecture 1: Deep Learning for Computer Vision
Mohamed Loey
 
PDF
Design of an Intelligent System for Improving Classification of Cancer Diseases
Mohamed Loey
 
PDF
Computer Security - CCNA Security - Lecture 2
Mohamed Loey
 
PDF
Computer Security - CCNA Security - Lecture 1
Mohamed Loey
 
PDF
Algorithms Lecture 8: Pattern Algorithms
Mohamed Loey
 
PDF
Convolutional Neural Network Models - Deep Learning
Mohamed Loey
 
PDF
Deep Learning - Overview of my work II
Mohamed Loey
 
PDF
Computer Security Lecture 7: RSA
Mohamed Loey
 
PDF
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Mohamed Loey
 
PDF
Computer Security Lecture 4.1: DES Supplementary Material
Mohamed Loey
 
PDF
PMP Lecture 4: Project Integration Management
Mohamed Loey
 
PDF
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Mohamed Loey
 
PDF
Computer Security Lecture 3: Classical Encryption Techniques 2
Mohamed Loey
 
PDF
Computer Security Lecture 2: Classical Encryption Techniques 1
Mohamed Loey
 
Lecture 6: Deep Learning Applications
Mohamed Loey
 
Lecture 5: Convolutional Neural Network Models
Mohamed Loey
 
Lecture 4: Deep Learning Frameworks
Mohamed Loey
 
Lecture 4: How it Works: Convolutional Neural Networks
Mohamed Loey
 
Lecture 3: Convolutional Neural Networks
Mohamed Loey
 
Lecture 2: Artificial Neural Network
Mohamed Loey
 
Lecture 1: Deep Learning for Computer Vision
Mohamed Loey
 
Design of an Intelligent System for Improving Classification of Cancer Diseases
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 2
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 1
Mohamed Loey
 
Algorithms Lecture 8: Pattern Algorithms
Mohamed Loey
 
Convolutional Neural Network Models - Deep Learning
Mohamed Loey
 
Deep Learning - Overview of my work II
Mohamed Loey
 
Computer Security Lecture 7: RSA
Mohamed Loey
 
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Mohamed Loey
 
Computer Security Lecture 4.1: DES Supplementary Material
Mohamed Loey
 
PMP Lecture 4: Project Integration Management
Mohamed Loey
 
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Mohamed Loey
 
Computer Security Lecture 3: Classical Encryption Techniques 2
Mohamed Loey
 
Computer Security Lecture 2: Classical Encryption Techniques 1
Mohamed Loey
 

Recently uploaded (20)

PPTX
CDH. pptx
AneetaSharma15
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
CDH. pptx
AneetaSharma15
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 

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