SlideShare a Scribd company logo
Searching
Objective : To find out the location of any given element in array. If element found then
successful, otherwise unsuccessful.
Two approaches –
1. Linear Search (Sequential Search): if array elements in random order
2. Binary Search: if array elements in sorted order
Ashish Gupta, Data Structures @ JUET, Guna
Linear Search
Algorithm: Let a is given array and no. of elements is n. Pos is representing the position of
desired element k in array a, if element found otherwise, set Pos to -1. Variable i is used as
simple variable in loop.
Ashish Gupta, Data Structures @ JUET, Guna
1. Initialize Pos = -1
2. For i = 0 to n-1
3. If a[i] == k
4. Set Pos = i+1
5. If Pos < 0
6. Print “Element is not found”
7. Else Print Pos
C1
C2
C3
C4
C5
C6
C7
Analysis of Linear Search
IDENTICAL ELEMENTS:
On successful search or unsuccessful search
Time Complexity = C1 + (n+1)*C2 + n * C3 + n* C4 + C5 + C6 +C7 = 3n + 5
= O(n)
DISTINCT ELEMENTS:
On successful search or unsuccessful search
Time Complexity = C1 + (n+1)*C2 + n * C3 + C4 + C5 + C6 +C7 = 2n + 6
= O(n)
That means in worst case, at most n number of comparisons are required to find
out the desired element location.
Ashish Gupta, Data Structures @ JUET, Guna
Binary Search
 Best searching approach
Constraint:
 Given array should be in sorted order
OR
 First sort the given array and then perform search, but then sorting time will be
added
Ashish Gupta, Data Structures @ JUET, Guna
1. Initialize low =0, high = n-1
2. While low <= high
3. mid = (low+high)/2
4. If a[mid] == Item
5. Set Pos = mid
6. Break and Jump to step 10
7. Else if Item < a[mid]
8. high = mid -1
9. Else low =mid +1
10. If Pos < 0
11. Print “Element is not found”
12. Else Print Pos
Binary Search (Iterative)
Algorithm: Let a is given array which is sorted in ascending order and no. of elements is n.
Pos is representing the position of desired element Item in array a, if element found
otherwise, set Pos to -1. Variable mid is used to keep the mid index of array.
Ashish Gupta, Data Structures @ JUET, Guna
Binary Search Illustration
mid = (low+high)/2
= (0 + 9) /2 = 4
item to be searched, Item = 94
If a[mid]<Item
low = mid + 1
Ashish Gupta, Data Structures @ JUET, Guna
7 8 15 27 31 57 68 94 103 104
low = 0 high = 9mid = 4
Binary Search Illustration
mid = (low+high)/2
= (5 + 9) /2 = 7
item to be searched, Item = 94
If a[mid]==Item
Pos = mid
Ashish Gupta, Data Structures @ JUET, Guna
7 8 15 27 31 57 68 94 103 104
low = 5 high = 9mid = 7
1. Initialize low =0, high = n-1
2. While low <= high
3. mid = (low+high)/2
4. If a[mid] == Item
5. Set Pos = mid
6. Break and Jump to step 10
7. Else if Item < a[mid]
8. high = mid -1
9. Else low =mid +1
10. If Pos < 0
11. Print “Element is not found”
12. Else Print Pos
Binary Search (Iterative)
Ashish Gupta, Data Structures @ JUET, Guna
C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
C11
C12
Analysis of Iterative Binary Search
DISTINCT & SORTED ELEMENTS:
On successful search or unsuccessful search
Time Complexity =
= C1 + ( log2n +1)*C2 + log2n * (C3 + C4 +C7 +C8 +C9) + C5 + C6 +C10 + C11 +C12
= 6 log2n + 7
= O (log2n)
That means in worst case, at most log2n number of comparisons are required to
find out the desired element location which is better than the linear search.
Ashish Gupta, Data Structures @ JUET, Guna
1. If low<=high
2. mid = (low+high)/2
3. If a[mid] == Item
4. Set Pos = mid
5. Return Pos
6. Else if Item < a[mid]
7. Return Bin_Search(a,Item,low,mid-1)
8. Else Return Bin_Search(a,Item,mid+1,high)
9. Return Pos
Binary Search (Recursive)
Algorithm Bin_Search(a, Item,low, high): Let a is given array which is sorted in ascending
order and no. of elements is n. Pos is representing the position of desired element Item in
array a, if element found otherwise, set Pos to -1. Variable mid is used to keep the mid index
of array. Here, initially low and high are assigned the lowest and highest index of array a.
Ashish Gupta, Data Structures @ JUET, Guna
Recurrence Relation
Algorithm
Iterative Recursive
T(n) = Total cost of steps T(n) = Recurrence relation
Order of Complexity
Ashish Gupta, Data Structures @ JUET, Guna
Recurrence Relation (Cont…)
Let T(n) is a recurrence relation. Then,
T – a recurrence function defining a function n – parameter to function T
Therefore,
T(n) – Sequence term generated by function T for parameter n
Examples :
1.
2.
3.
Ashish Gupta, Data Structures @ JUET, Guna
T(n) =
0 for n=0
T(n-1) + n n>0
T(n) =
1 for n=0
T(n/2) + 1 n>0
T(n) =
0 for n=0
1 n=1
2T(n-1) +1 n>1
Illustration 1: Factorial Recursive
Algorithm Fact_Rec(n): Let n is number for which factorial is to be calculated.
Ashish Gupta, Data Structures @ JUET, Guna
1. If n==1
2. Return 1
3. Return Fact_Rec(n-1) * n
C1
C2
C3
Therefore,
Now,
How to solve this recurrence relation ?
T(n) =
1 for n=1
T(n-1) + 1 n>1
Solving Recurrence Relation
Substitution method – try to find the pattern
Let’s take a instance –
n = 10 i.e.
T(10) = T(9) + 1
= T(8) + 1 + 1
= T(7) + 1 + 1 +1
………..
………..
= T(1) + 1 + 8
= 10
= n
Hence, Time complexity = O(n)
Ashish Gupta, Data Structures @ JUET, Guna
Illustration 2: Fibonacci Recursive
Algorithm Fibo_Rec(n): Let n is number means nth is to be calculated.
Ashish Gupta, Data Structures @ JUET, Guna
1. If n==1
2. Return 0
3. If n==2
4. Return 1
5. Return Fibo_Rec(n-1) + Fibo_Rec(n-2)
C1
C2
C3
Therefore,
T(n) =
0 for n=1
1 n=2
T(n-1) + T(n-2) n>2
Solving Recurrence Relation
Let’s take a instance –
n = 5 i.e.
T(5) = T(4) + T(3)
= T(3) + T(2) + T(2) + T(1)
= T(2) + T(1) + 1 + 1 + 0
= 1 + 0 + 1 +1
= 3
 It grows like exponential tree.
Time complexity = O(2n)
Ashish Gupta, Data Structures @ JUET, Guna
Analysis of Binary Search Recursive
Ashish Gupta, Data Structures @ JUET, Guna
Recurrence relation –
T(n) = T(n/2) + 1
= T(n/2*2) + 1 + 1
= T(n/2*2*2) + 3
= T(n/24) + 4
…………….
= T(n/2k) + k
Let n/2k = 1, n = 2k , log2n = k * log22
i.e. k = log2n
Now,
T(n) = T(1) + log2n = 1 + log2n
= O(log2n)
T(n) =
1 for n=1
T(|_n/2_|) + 1 n>1
Summary
 Linear search with illustration
 Analysis of linear search
 Binary search (iterative) and its analysis
 Binary search (recursive) and its analysis using recurrence relation
 Discussion on recurrence relation
Ashish Gupta, Data Structures @ JUET, Guna

More Related Content

What's hot

Group Theory
Group TheoryGroup Theory
Group Theory
Durgesh Chahar
 
Introduction to Set Theory
Introduction to Set TheoryIntroduction to Set Theory
Introduction to Set Theory
Usama ahmad
 
Relations and Functions 1
Relations and Functions 1Relations and Functions 1
Relations and Functions 1
Lakshmikanta Satapathy
 
Discrete Structure Mathematics lecture 1
Discrete Structure Mathematics lecture 1Discrete Structure Mathematics lecture 1
Discrete Structure Mathematics lecture 1
Amr Rashed
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
Malikireddy Bramhananda Reddy
 
Class XI CH 1 (sets)
Class XI CH 1 (sets)Class XI CH 1 (sets)
Class XI CH 1 (sets)
Pradeep Sharma
 
Discrete Mathematics and Its Applications 7th Edition Rose Solutions Manual
Discrete Mathematics and Its Applications 7th Edition Rose Solutions ManualDiscrete Mathematics and Its Applications 7th Edition Rose Solutions Manual
Discrete Mathematics and Its Applications 7th Edition Rose Solutions Manual
TallulahTallulah
 
Chapter2 functionsandgraphs-151003144959-lva1-app6891
Chapter2 functionsandgraphs-151003144959-lva1-app6891Chapter2 functionsandgraphs-151003144959-lva1-app6891
Chapter2 functionsandgraphs-151003144959-lva1-app6891
Cleophas Rwemera
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
TutorialsDuniya.com
 
Set theory and relation
Set theory and relationSet theory and relation
Set theory and relation
ankush_kumar
 
Class XI CH 2 (relations and functions)
Class XI CH 2 (relations and functions)Class XI CH 2 (relations and functions)
Class XI CH 2 (relations and functions)
Pradeep Sharma
 
Set theory
Set theorySet theory
Set theory
Prerak Trivedi
 
Characterizations of Prime and Minimal Prime Ideals of Hyperlattices
Characterizations of Prime and Minimal Prime Ideals of HyperlatticesCharacterizations of Prime and Minimal Prime Ideals of Hyperlattices
Characterizations of Prime and Minimal Prime Ideals of Hyperlattices
rahulmonikasharma
 
Set Theory 2
Set Theory 2Set Theory 2
Set data structure
Set data structure Set data structure
Set data structure
Tech_MX
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
Mohd Arif
 
Set theory
Set theorySet theory
Set theory
Gaditek
 
Ch1 sets and_logic(1)
Ch1 sets and_logic(1)Ch1 sets and_logic(1)
Ch1 sets and_logic(1)
Kwonpyo Ko
 
Heapsort
HeapsortHeapsort

What's hot (20)

Group Theory
Group TheoryGroup Theory
Group Theory
 
Introduction to Set Theory
Introduction to Set TheoryIntroduction to Set Theory
Introduction to Set Theory
 
Relations and Functions 1
Relations and Functions 1Relations and Functions 1
Relations and Functions 1
 
Discrete Structure Mathematics lecture 1
Discrete Structure Mathematics lecture 1Discrete Structure Mathematics lecture 1
Discrete Structure Mathematics lecture 1
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
 
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
 
Class XI CH 1 (sets)
Class XI CH 1 (sets)Class XI CH 1 (sets)
Class XI CH 1 (sets)
 
Discrete Mathematics and Its Applications 7th Edition Rose Solutions Manual
Discrete Mathematics and Its Applications 7th Edition Rose Solutions ManualDiscrete Mathematics and Its Applications 7th Edition Rose Solutions Manual
Discrete Mathematics and Its Applications 7th Edition Rose Solutions Manual
 
Chapter2 functionsandgraphs-151003144959-lva1-app6891
Chapter2 functionsandgraphs-151003144959-lva1-app6891Chapter2 functionsandgraphs-151003144959-lva1-app6891
Chapter2 functionsandgraphs-151003144959-lva1-app6891
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Set theory and relation
Set theory and relationSet theory and relation
Set theory and relation
 
Class XI CH 2 (relations and functions)
Class XI CH 2 (relations and functions)Class XI CH 2 (relations and functions)
Class XI CH 2 (relations and functions)
 
Set theory
Set theorySet theory
Set theory
 
Characterizations of Prime and Minimal Prime Ideals of Hyperlattices
Characterizations of Prime and Minimal Prime Ideals of HyperlatticesCharacterizations of Prime and Minimal Prime Ideals of Hyperlattices
Characterizations of Prime and Minimal Prime Ideals of Hyperlattices
 
Set Theory 2
Set Theory 2Set Theory 2
Set Theory 2
 
Set data structure
Set data structure Set data structure
Set data structure
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
Set theory
Set theorySet theory
Set theory
 
Ch1 sets and_logic(1)
Ch1 sets and_logic(1)Ch1 sets and_logic(1)
Ch1 sets and_logic(1)
 
Heapsort
HeapsortHeapsort
Heapsort
 

Viewers also liked

Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 
Perform brute force
Perform brute forcePerform brute force
Perform brute force
SHC
 
Brute force-attack presentation
Brute force-attack presentationBrute force-attack presentation
Brute force-attack presentation
Mahmoud Ibra
 
Password cracking and brute force
Password cracking and brute forcePassword cracking and brute force
Password cracking and brute force
vishalgohel12195
 
Google
GoogleGoogle
Google
vibhabehl
 
Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"
John Lynch
 
Chap04alg
Chap04algChap04alg
Chap04alg
Munkhchimeg
 
Algorithms - Aaron Bloomfield
Algorithms - Aaron BloomfieldAlgorithms - Aaron Bloomfield
Algorithms - Aaron Bloomfield
Reggie Niccolo Santos
 
Why Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & ScienceWhy Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & Science
John Lynch
 
sPen Data Management
sPen Data ManagementsPen Data Management
sPen Data Management
Uladzimir Slabin
 
simple-sorting algorithms
simple-sorting algorithmssimple-sorting algorithms
simple-sorting algorithms
Ravirajsinh Chauhan
 
The Chemical Revolution
The Chemical RevolutionThe Chemical Revolution
The Chemical Revolution
John Lynch
 
Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian Revolution
John Lynch
 
Chapter one
Chapter oneChapter one
Chapter one
mihiretu kassaye
 
How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...
American Astronautical Society
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
Introduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_bIntroduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_b
Shahi Raz Akhtar
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
pinakspatel
 
Introduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_aIntroduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_a
Shahi Raz Akhtar
 
CSS 3, Style and Beyond
CSS 3, Style and BeyondCSS 3, Style and Beyond
CSS 3, Style and Beyond
Samsung Open Source Group
 

Viewers also liked (20)

Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
 
Perform brute force
Perform brute forcePerform brute force
Perform brute force
 
Brute force-attack presentation
Brute force-attack presentationBrute force-attack presentation
Brute force-attack presentation
 
Password cracking and brute force
Password cracking and brute forcePassword cracking and brute force
Password cracking and brute force
 
Google
GoogleGoogle
Google
 
Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Algorithms - Aaron Bloomfield
Algorithms - Aaron BloomfieldAlgorithms - Aaron Bloomfield
Algorithms - Aaron Bloomfield
 
Why Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & ScienceWhy Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & Science
 
sPen Data Management
sPen Data ManagementsPen Data Management
sPen Data Management
 
simple-sorting algorithms
simple-sorting algorithmssimple-sorting algorithms
simple-sorting algorithms
 
The Chemical Revolution
The Chemical RevolutionThe Chemical Revolution
The Chemical Revolution
 
Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian Revolution
 
Chapter one
Chapter oneChapter one
Chapter one
 
How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Introduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_bIntroduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_b
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
Introduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_aIntroduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_a
 
CSS 3, Style and Beyond
CSS 3, Style and BeyondCSS 3, Style and Beyond
CSS 3, Style and Beyond
 

Similar to Searching algorithms

searching.pdf
searching.pdfsearching.pdf
searching.pdf
ISHAN194169
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
ashish gupta
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
SwatiHans10
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
Binary Search Algorithm.pptx
Binary Search Algorithm.pptxBinary Search Algorithm.pptx
Binary Search Algorithm.pptx
BhagyashreeMadan1
 
Searching
Searching Searching
1-D array
1-D array1-D array
Linear and Binary Search
Linear and Binary SearchLinear and Binary Search
Linear and Binary Search
WinNie Sjr
 
Code
CodeCode
Searching techniques
Searching techniquesSearching techniques
Searching techniques
ER Punit Jain
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
Durga Devi
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
RUHULAMINHAZARIKA
 
L10 sorting-searching
L10 sorting-searchingL10 sorting-searching
L10 sorting-searching
mondalakash2012
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
1D Array
1D Array1D Array
1D Array
Swarup Boro
 

Similar to Searching algorithms (20)

searching.pdf
searching.pdfsearching.pdf
searching.pdf
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Binary Search Algorithm.pptx
Binary Search Algorithm.pptxBinary Search Algorithm.pptx
Binary Search Algorithm.pptx
 
Searching
Searching Searching
Searching
 
1-D array
1-D array1-D array
1-D array
 
Linear and Binary Search
Linear and Binary SearchLinear and Binary Search
Linear and Binary Search
 
Code
CodeCode
Code
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
L10 sorting-searching
L10 sorting-searchingL10 sorting-searching
L10 sorting-searching
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
1D Array
1D Array1D Array
1D Array
 

Recently uploaded

MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 

Recently uploaded (20)

MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 

Searching algorithms

  • 1.
  • 2. Searching Objective : To find out the location of any given element in array. If element found then successful, otherwise unsuccessful. Two approaches – 1. Linear Search (Sequential Search): if array elements in random order 2. Binary Search: if array elements in sorted order Ashish Gupta, Data Structures @ JUET, Guna
  • 3. Linear Search Algorithm: Let a is given array and no. of elements is n. Pos is representing the position of desired element k in array a, if element found otherwise, set Pos to -1. Variable i is used as simple variable in loop. Ashish Gupta, Data Structures @ JUET, Guna 1. Initialize Pos = -1 2. For i = 0 to n-1 3. If a[i] == k 4. Set Pos = i+1 5. If Pos < 0 6. Print “Element is not found” 7. Else Print Pos C1 C2 C3 C4 C5 C6 C7
  • 4. Analysis of Linear Search IDENTICAL ELEMENTS: On successful search or unsuccessful search Time Complexity = C1 + (n+1)*C2 + n * C3 + n* C4 + C5 + C6 +C7 = 3n + 5 = O(n) DISTINCT ELEMENTS: On successful search or unsuccessful search Time Complexity = C1 + (n+1)*C2 + n * C3 + C4 + C5 + C6 +C7 = 2n + 6 = O(n) That means in worst case, at most n number of comparisons are required to find out the desired element location. Ashish Gupta, Data Structures @ JUET, Guna
  • 5. Binary Search  Best searching approach Constraint:  Given array should be in sorted order OR  First sort the given array and then perform search, but then sorting time will be added Ashish Gupta, Data Structures @ JUET, Guna
  • 6. 1. Initialize low =0, high = n-1 2. While low <= high 3. mid = (low+high)/2 4. If a[mid] == Item 5. Set Pos = mid 6. Break and Jump to step 10 7. Else if Item < a[mid] 8. high = mid -1 9. Else low =mid +1 10. If Pos < 0 11. Print “Element is not found” 12. Else Print Pos Binary Search (Iterative) Algorithm: Let a is given array which is sorted in ascending order and no. of elements is n. Pos is representing the position of desired element Item in array a, if element found otherwise, set Pos to -1. Variable mid is used to keep the mid index of array. Ashish Gupta, Data Structures @ JUET, Guna
  • 7. Binary Search Illustration mid = (low+high)/2 = (0 + 9) /2 = 4 item to be searched, Item = 94 If a[mid]<Item low = mid + 1 Ashish Gupta, Data Structures @ JUET, Guna 7 8 15 27 31 57 68 94 103 104 low = 0 high = 9mid = 4
  • 8. Binary Search Illustration mid = (low+high)/2 = (5 + 9) /2 = 7 item to be searched, Item = 94 If a[mid]==Item Pos = mid Ashish Gupta, Data Structures @ JUET, Guna 7 8 15 27 31 57 68 94 103 104 low = 5 high = 9mid = 7
  • 9. 1. Initialize low =0, high = n-1 2. While low <= high 3. mid = (low+high)/2 4. If a[mid] == Item 5. Set Pos = mid 6. Break and Jump to step 10 7. Else if Item < a[mid] 8. high = mid -1 9. Else low =mid +1 10. If Pos < 0 11. Print “Element is not found” 12. Else Print Pos Binary Search (Iterative) Ashish Gupta, Data Structures @ JUET, Guna C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12
  • 10. Analysis of Iterative Binary Search DISTINCT & SORTED ELEMENTS: On successful search or unsuccessful search Time Complexity = = C1 + ( log2n +1)*C2 + log2n * (C3 + C4 +C7 +C8 +C9) + C5 + C6 +C10 + C11 +C12 = 6 log2n + 7 = O (log2n) That means in worst case, at most log2n number of comparisons are required to find out the desired element location which is better than the linear search. Ashish Gupta, Data Structures @ JUET, Guna
  • 11. 1. If low<=high 2. mid = (low+high)/2 3. If a[mid] == Item 4. Set Pos = mid 5. Return Pos 6. Else if Item < a[mid] 7. Return Bin_Search(a,Item,low,mid-1) 8. Else Return Bin_Search(a,Item,mid+1,high) 9. Return Pos Binary Search (Recursive) Algorithm Bin_Search(a, Item,low, high): Let a is given array which is sorted in ascending order and no. of elements is n. Pos is representing the position of desired element Item in array a, if element found otherwise, set Pos to -1. Variable mid is used to keep the mid index of array. Here, initially low and high are assigned the lowest and highest index of array a. Ashish Gupta, Data Structures @ JUET, Guna
  • 12. Recurrence Relation Algorithm Iterative Recursive T(n) = Total cost of steps T(n) = Recurrence relation Order of Complexity Ashish Gupta, Data Structures @ JUET, Guna
  • 13. Recurrence Relation (Cont…) Let T(n) is a recurrence relation. Then, T – a recurrence function defining a function n – parameter to function T Therefore, T(n) – Sequence term generated by function T for parameter n Examples : 1. 2. 3. Ashish Gupta, Data Structures @ JUET, Guna T(n) = 0 for n=0 T(n-1) + n n>0 T(n) = 1 for n=0 T(n/2) + 1 n>0 T(n) = 0 for n=0 1 n=1 2T(n-1) +1 n>1
  • 14. Illustration 1: Factorial Recursive Algorithm Fact_Rec(n): Let n is number for which factorial is to be calculated. Ashish Gupta, Data Structures @ JUET, Guna 1. If n==1 2. Return 1 3. Return Fact_Rec(n-1) * n C1 C2 C3 Therefore, Now, How to solve this recurrence relation ? T(n) = 1 for n=1 T(n-1) + 1 n>1
  • 15. Solving Recurrence Relation Substitution method – try to find the pattern Let’s take a instance – n = 10 i.e. T(10) = T(9) + 1 = T(8) + 1 + 1 = T(7) + 1 + 1 +1 ……….. ……….. = T(1) + 1 + 8 = 10 = n Hence, Time complexity = O(n) Ashish Gupta, Data Structures @ JUET, Guna
  • 16. Illustration 2: Fibonacci Recursive Algorithm Fibo_Rec(n): Let n is number means nth is to be calculated. Ashish Gupta, Data Structures @ JUET, Guna 1. If n==1 2. Return 0 3. If n==2 4. Return 1 5. Return Fibo_Rec(n-1) + Fibo_Rec(n-2) C1 C2 C3 Therefore, T(n) = 0 for n=1 1 n=2 T(n-1) + T(n-2) n>2
  • 17. Solving Recurrence Relation Let’s take a instance – n = 5 i.e. T(5) = T(4) + T(3) = T(3) + T(2) + T(2) + T(1) = T(2) + T(1) + 1 + 1 + 0 = 1 + 0 + 1 +1 = 3  It grows like exponential tree. Time complexity = O(2n) Ashish Gupta, Data Structures @ JUET, Guna
  • 18. Analysis of Binary Search Recursive Ashish Gupta, Data Structures @ JUET, Guna Recurrence relation – T(n) = T(n/2) + 1 = T(n/2*2) + 1 + 1 = T(n/2*2*2) + 3 = T(n/24) + 4 ……………. = T(n/2k) + k Let n/2k = 1, n = 2k , log2n = k * log22 i.e. k = log2n Now, T(n) = T(1) + log2n = 1 + log2n = O(log2n) T(n) = 1 for n=1 T(|_n/2_|) + 1 n>1
  • 19. Summary  Linear search with illustration  Analysis of linear search  Binary search (iterative) and its analysis  Binary search (recursive) and its analysis using recurrence relation  Discussion on recurrence relation Ashish Gupta, Data Structures @ JUET, Guna