SlideShare a Scribd company logo
1 of 8
Download to read offline
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

DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
Abdul Khan
 

Similar to Searching in Data Structure (20)

searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
my docoment
my docomentmy docoment
my docoment
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
SEARCHING
SEARCHINGSEARCHING
SEARCHING
 
21-algorithms (1).ppt
21-algorithms (1).ppt21-algorithms (1).ppt
21-algorithms (1).ppt
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
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
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 

Recently uploaded

electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
benjamincojr
 

Recently uploaded (20)

Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and ToolsMaximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
What is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, FunctionsWhat is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, Functions
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdf
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Station
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
 
History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & Modernization
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 

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