SlideShare a Scribd company logo
2
Most read
Data Structures
Raghav Birla
1
Searching Methods
Two popular methods for searching
Linear search
Binary search
Linear search
In linear search, we compare every elements of the array one by one in a sequential
manner until a match is found
Linear search algorithm
Linear_search(A[], n, e)
Step 1) Set i = 0 and loc = 0.
Step 2) repeat steps 3, 4 while(loc=0 && i<n)
Step 3) if A[i] = e, then set loc = i;
Step 4) Set i = i+1
Step 5) If loc = 0 , then, element ‘e’ is not in array
Step 6) else return i (location of e).
Here, A= name of array
n= size of array A
i= used for index of array
e= element to be searched
loc= this will store the location of searched element
Complexity = O(n)
Advantages & Disadvantages
 Advantages:
 We can apply for both sorted and unsorted data.
 Best case running time complexity is O(1)
Disadvantages:
Worst case Running Time complexity is O(n)
5
Binary search
 Here, we compare search elements ‘e’ with the middle element of array A,
that is A[mid], where mid = n/2. and n = size of array.
 In binary search array elements must be sorted (either in ascending or
descending order)
 If e = A[mid]  we have found element
 If e < A[mid]  restrict search to A[0] to A[m-1]
 If e > A[mid]  restrict search to A[m+1] to A[n-1]
Algorithm of binary search
Binary_search(A[], n, e)
Step 1) set low = 0, high = n-1
Step 2) repeat steps 3 and 4 while(low<=high)
Step 3) mid = (low + high)/2
Step 4) if (e == A[mid])
Return mid
Else if (e< A[mid])
High = mid – 1
Else low = mid + 1
Step 5) return -1
Here, low = lower bound of array A of size n
high = upper bound of array A
e= element to be searched
::Complexity = O(log n)
Advantages & Disadvantages
 Advantages
 Worst case running time complexity is O(logn).
 Perform better than linear search
 Disadvatages
 We can use only for the sorted data.
8

More Related Content

Similar to Searching in Data Structure (20)

PPT
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
PPTX
Dsa – data structure and algorithms sorting
sajinis3
 
PPTX
Linear and Binary search .pptx
p83629918
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
PDF
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPTX
my docoment
NeeshanYonzan
 
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
PDF
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PPT
9 Arrays
Praveen M Jigajinni
 
PPTX
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
PPTX
Data Structures_ Sorting & Searching
ThenmozhiK5
 
PPTX
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
PPTX
Searching techniques with progrms
Misssaxena
 
PDF
advanced searching and sorting.pdf
haramaya university
 
PPTX
Searching_Sorting.pptx
21BD1A058RSahithi
 
PDF
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
PPT
arrays1.ppt python programme arrays insertion
bushraashraf639
 
PPTX
Lecture 4_Linear & Binary search from data structure and algorithm
ArifatunNesa
 
PPT
search_sort.ppt
SwatiHans10
 
PPTX
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
Dsa – data structure and algorithms sorting
sajinis3
 
Linear and Binary search .pptx
p83629918
 
searching in data structure.pptx
chouguleamruta24
 
my docoment
NeeshanYonzan
 
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Searching techniques with progrms
Misssaxena
 
advanced searching and sorting.pdf
haramaya university
 
Searching_Sorting.pptx
21BD1A058RSahithi
 
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
arrays1.ppt python programme arrays insertion
bushraashraf639
 
Lecture 4_Linear & Binary search from data structure and algorithm
ArifatunNesa
 
search_sort.ppt
SwatiHans10
 
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 

Recently uploaded (20)

PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Design Thinking basics for Engineers.pdf
CMR University
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Thermal runway and thermal stability.pptx
godow93766
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Ad

Searching in Data Structure

  • 2. Searching Methods Two popular methods for searching Linear search Binary search
  • 3. Linear search In linear search, we compare every elements of the array one by one in a sequential manner until a match is found
  • 4. Linear search algorithm Linear_search(A[], n, e) Step 1) Set i = 0 and loc = 0. Step 2) repeat steps 3, 4 while(loc=0 && i<n) Step 3) if A[i] = e, then set loc = i; Step 4) Set i = i+1 Step 5) If loc = 0 , then, element ‘e’ is not in array Step 6) else return i (location of e). Here, A= name of array n= size of array A i= used for index of array e= element to be searched loc= this will store the location of searched element Complexity = O(n)
  • 5. Advantages & Disadvantages  Advantages:  We can apply for both sorted and unsorted data.  Best case running time complexity is O(1) Disadvantages: Worst case Running Time complexity is O(n) 5
  • 6. Binary search  Here, we compare search elements ‘e’ with the middle element of array A, that is A[mid], where mid = n/2. and n = size of array.  In binary search array elements must be sorted (either in ascending or descending order)  If e = A[mid]  we have found element  If e < A[mid]  restrict search to A[0] to A[m-1]  If e > A[mid]  restrict search to A[m+1] to A[n-1]
  • 7. Algorithm of binary search Binary_search(A[], n, e) Step 1) set low = 0, high = n-1 Step 2) repeat steps 3 and 4 while(low<=high) Step 3) mid = (low + high)/2 Step 4) if (e == A[mid]) Return mid Else if (e< A[mid]) High = mid – 1 Else low = mid + 1 Step 5) return -1 Here, low = lower bound of array A of size n high = upper bound of array A e= element to be searched ::Complexity = O(log n)
  • 8. Advantages & Disadvantages  Advantages  Worst case running time complexity is O(logn).  Perform better than linear search  Disadvatages  We can use only for the sorted data. 8