SlideShare a Scribd company logo
Data Structure and
Algorithms-Searching
Techniques
Prepared By,
S.Sajini
AP/CSE
DSA – Data Structure
and Algorithms
What is DS?
A data structure is
collection of data elements
that are arranged in one
name, and which defines a
particular way of storing
and organizing data in a
computer so that it can be
used efficiently.
Searching Algorithms
Linear Search
Data Search − Consider an inventory of 1 million(106) items of a
store. If the application is to search an item, it has to
search an item in 1 million(106) items every time slowing
down the search. As data grows, search will become slower.
This is why searching algorithms are important
Need for Searching Algorithms
1. Linear search or sequential search is a method for finding a
particular value in a list that checks each element in sequence
until the desired element is found or the list is exhausted.
2. The list need not be ordered.
3. It is very simple and works as follows: We keep on comparing
each element with the element to search until the desired
element is found or list ends.
Linear search
This is the array we are going to perform linear search
Let’s search for the number 3. We start at the beginning
and check the first element in the array.
Example
Is the first value 3? No, not it. Is it the next
element?
Is the second value 3?
Not there either. The next element?
Is the third value 3?
Not there either. Next?
Is the fourth value 3? Yes!
We found it!!! Now you understand the idea of
linear searching; we go through each element,
in order, until we find the correct value.
Linear search - Pseudocode:
# Input: Array D, integer key
# Output: first index of key in D, or -1 if not found
For i = 0 to last index of D:
if D[i] equals key:
return i
return -1
Time Complexity
In the linear search problem, the best case occurs when Target is
present at the first location. The number of operations in the
best case is constant (not dependent on n). So time complexity in
the best case would be O(1)
Best case Analysis
Time Complexity
For Linear Search, the worst case happens when the element to be
searched is not present in the array or it is present at the end of
the array.
In this scenario the search() compares it with all the elements of
array one by one. Therefore, the worst case time complexity of
linear search would be O(n).
Worst case Analysis
Time Complexity
The key is equally likely to be in any position in the array
If the key is in the first array position: 1 comparison
If the key is in the second array position: 2 comparisons ...
If the key is in the ith postion: i comparisons ...
So average all these possibilities:
(1+2+3+...+n)/n = [n(n+1)/2] /n = (n+1)/2 comparisons.
The average number of comparisons is (n+1)/2 = O(n).
Average case Analysis
Time Complexity
In average case analysis, we take all possible inputs and calculate
computing time for all of the inputs. Sum all the calculated values and
divide the sum by total number of inputs.
We must know distribution of cases. For the linear search problem, let us
assume that all cases are uniformly distributed (including the case of
target not being present in array).
Average case Analysis
Space Complexity
This type of search requires only a single unit of memory to store the
element being searched. This is not relevant to the size of the input
Array.
Hence, the Space Complexity of Linear Search is O(1).
Searching Algorithms
Binary Search
1. The binary search algorithm begins by comparing the target value to the
value of the middle element of the sorted array.
2. If the target value is equal to the middle element's value, then the
position is returned and the search is finished.
Binary search
3. If the target value is less than the middle element's value, then the
search continues on the lower half of the array; or if the target value is
greater than the middle element's value, then the search continues on the
upper half of the array.
4. This process continues, eliminating half of the elements, and comparing
the target value to the value of the middle element of the remaining
elements - until the target value is either or until the entire array has
been searched.
Binary search
This is the array we are going to perform binary search
Let’s search for the number 19.
Example
In the first step we start with the entire sequence and
consider its middle element 8
As the sequence is sorted and 8 is less than the key 19,
we conclude that 19 can occur only in the second half of
the sequence, not including the middle element 8.
We then repeat the same with the subsequence
consisting of the second half. Its middle element is
21, which is greater than the key 19.
We thus next consider the first half of this subsequence,
whose middle element is 11. In the last step the subsequence
consists of one element, and this element is both the middle
element and is equal to the key 19.
Thus we have discovered 19 in the sequence. Observe
that only the 4 elements (8, 21, 11 and 19) indexed by
the middle point index mid in the figure are compared
against the key 19.
If the same search is performed in linear search, it
will take 8 comparisons.
Example 2
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = (upperBound + lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midpoint
end while
end procedure
Time Complexity
n
n/2 n/2
n/4 n/4
n/8 n/8
Array
Sub -Array
.
.
.
Therefore the last sub array will be n/n or
n/2k
K = number of comparisons
made
n/n = n/2k
n/2k = 1
n = 2k
Taking log on both the sides
Log n = log 2k
Log n = k log 2
log n = k
Time Complexity is O(log n)
Space Complexity
This type of search requires only a single unit of memory to store the
element being searched. This is not relevant to the size of the input
Array.
Hence, the Space Complexity of Binary Search is O(1).
Dsa – data structure and algorithms   searching

More Related Content

PPTX
Rahat &amp; juhith
PDF
Searching and Sorting Techniques in Data Structure
PPTX
Searching & Sorting Algorithms
PPTX
Insertion Sort
PPT
Selection sort
PPTX
Sequential & binary, linear search
PPT
Selection sort
PPTX
Linear and Binary search
Rahat &amp; juhith
Searching and Sorting Techniques in Data Structure
Searching & Sorting Algorithms
Insertion Sort
Selection sort
Sequential & binary, linear search
Selection sort
Linear and Binary search

What's hot (20)

PPT
Data Structure and Algorithms Hashing
PPTX
Binary search
PPTX
Linear Search
PPTX
Binary search Algorithm
PPT
SEARCHING AND SORTING ALGORITHMS
PPTX
Different Sorting tecniques in Data Structure
PPTX
Insertion sort
PPTX
Binary search
PPT
Sorting Algorithms
PPTX
Searching
PPTX
PPTX
Searching and sorting
PDF
Binary Search - Design & Analysis of Algorithms
PDF
linear search and binary search
PPTX
PPTX
Data structures
PPTX
Presentation on the topic selection sort
PPTX
Hashing Technique In Data Structures
PPTX
Insertion sort
PPTX
Insertion sort algorithm power point presentation
Data Structure and Algorithms Hashing
Binary search
Linear Search
Binary search Algorithm
SEARCHING AND SORTING ALGORITHMS
Different Sorting tecniques in Data Structure
Insertion sort
Binary search
Sorting Algorithms
Searching
Searching and sorting
Binary Search - Design & Analysis of Algorithms
linear search and binary search
Data structures
Presentation on the topic selection sort
Hashing Technique In Data Structures
Insertion sort
Insertion sort algorithm power point presentation
Ad

Similar to Dsa – data structure and algorithms searching (20)

PDF
advanced searching and sorting.pdf
PPTX
SEARCHING
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
PPTX
linear search.pptxtttttttttttttttttttttt
PPTX
linear search.pptxtttttttttttttttttttttt
PPTX
linear search o power point presentation
DOCX
MODULE 5-Searching and-sorting
PPTX
Data Structures_ Sorting & Searching
PPTX
DS - Unit 2 FINAL (2).pptx
PPTX
Searching and Sorting Algorithms in Data Structures
PPT
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
PPTX
Lecture_Oct26.pptx
PPT
Searching Sorting
PPTX
Analysis of Algorithm - Binary Search.pptx
PPT
Binary Search
PPTX
Searching techniques in Data Structure And Algorithm
PPTX
Searching_Sorting.pptx
PPTX
Searching techniques
PPTX
Algorithm 8th lecture linear & binary search(2).pptx
advanced searching and sorting.pdf
SEARCHING
Chapter 3 - Data Structure and Algorithms.pptx
linear search.pptxtttttttttttttttttttttt
linear search.pptxtttttttttttttttttttttt
linear search o power point presentation
MODULE 5-Searching and-sorting
Data Structures_ Sorting & Searching
DS - Unit 2 FINAL (2).pptx
Searching and Sorting Algorithms in Data Structures
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
Lecture_Oct26.pptx
Searching Sorting
Analysis of Algorithm - Binary Search.pptx
Binary Search
Searching techniques in Data Structure And Algorithm
Searching_Sorting.pptx
Searching techniques
Algorithm 8th lecture linear & binary search(2).pptx
Ad

More from sajinis3 (8)

PPTX
Rice Theorem.pptx
PPTX
Examples of undecidable problems and problems.pptx
PPTX
Decidable problems.pptx
PPTX
Undecidability Basic definitions.pptx
PPTX
PPS-POINTERS.pptx
PPTX
Circular linked list
PPTX
Asymptotic notation
PPTX
Dsa – data structure and algorithms sorting
Rice Theorem.pptx
Examples of undecidable problems and problems.pptx
Decidable problems.pptx
Undecidability Basic definitions.pptx
PPS-POINTERS.pptx
Circular linked list
Asymptotic notation
Dsa – data structure and algorithms sorting

Recently uploaded (20)

PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PDF
composite construction of structures.pdf
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPTX
436813905-LNG-Process-Overview-Short.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
Geodesy 1.pptx...............................................
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
anatomy of limbus and anterior chamber .pptx
PDF
Queuing formulas to evaluate throughputs and servers
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
“Next-Gen AI: Trends Reshaping Our World”
PPTX
AgentX UiPath Community Webinar series - Delhi
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
composite construction of structures.pdf
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
436813905-LNG-Process-Overview-Short.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Geodesy 1.pptx...............................................
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
CH1 Production IntroductoryConcepts.pptx
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
anatomy of limbus and anterior chamber .pptx
Queuing formulas to evaluate throughputs and servers
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Lesson 3_Tessellation.pptx finite Mathematics
“Next-Gen AI: Trends Reshaping Our World”
AgentX UiPath Community Webinar series - Delhi
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation

Dsa – data structure and algorithms searching

  • 2. DSA – Data Structure and Algorithms What is DS? A data structure is collection of data elements that are arranged in one name, and which defines a particular way of storing and organizing data in a computer so that it can be used efficiently.
  • 4. Data Search − Consider an inventory of 1 million(106) items of a store. If the application is to search an item, it has to search an item in 1 million(106) items every time slowing down the search. As data grows, search will become slower. This is why searching algorithms are important Need for Searching Algorithms
  • 5. 1. Linear search or sequential search is a method for finding a particular value in a list that checks each element in sequence until the desired element is found or the list is exhausted. 2. The list need not be ordered. 3. It is very simple and works as follows: We keep on comparing each element with the element to search until the desired element is found or list ends. Linear search
  • 6. This is the array we are going to perform linear search Let’s search for the number 3. We start at the beginning and check the first element in the array. Example
  • 7. Is the first value 3? No, not it. Is it the next element? Is the second value 3? Not there either. The next element?
  • 8. Is the third value 3? Not there either. Next? Is the fourth value 3? Yes! We found it!!! Now you understand the idea of linear searching; we go through each element, in order, until we find the correct value.
  • 9. Linear search - Pseudocode: # Input: Array D, integer key # Output: first index of key in D, or -1 if not found For i = 0 to last index of D: if D[i] equals key: return i return -1
  • 10. Time Complexity In the linear search problem, the best case occurs when Target is present at the first location. The number of operations in the best case is constant (not dependent on n). So time complexity in the best case would be O(1) Best case Analysis
  • 11. Time Complexity For Linear Search, the worst case happens when the element to be searched is not present in the array or it is present at the end of the array. In this scenario the search() compares it with all the elements of array one by one. Therefore, the worst case time complexity of linear search would be O(n). Worst case Analysis
  • 12. Time Complexity The key is equally likely to be in any position in the array If the key is in the first array position: 1 comparison If the key is in the second array position: 2 comparisons ... If the key is in the ith postion: i comparisons ... So average all these possibilities: (1+2+3+...+n)/n = [n(n+1)/2] /n = (n+1)/2 comparisons. The average number of comparisons is (n+1)/2 = O(n). Average case Analysis
  • 13. Time Complexity In average case analysis, we take all possible inputs and calculate computing time for all of the inputs. Sum all the calculated values and divide the sum by total number of inputs. We must know distribution of cases. For the linear search problem, let us assume that all cases are uniformly distributed (including the case of target not being present in array). Average case Analysis
  • 14. Space Complexity This type of search requires only a single unit of memory to store the element being searched. This is not relevant to the size of the input Array. Hence, the Space Complexity of Linear Search is O(1).
  • 16. 1. The binary search algorithm begins by comparing the target value to the value of the middle element of the sorted array. 2. If the target value is equal to the middle element's value, then the position is returned and the search is finished. Binary search
  • 17. 3. If the target value is less than the middle element's value, then the search continues on the lower half of the array; or if the target value is greater than the middle element's value, then the search continues on the upper half of the array. 4. This process continues, eliminating half of the elements, and comparing the target value to the value of the middle element of the remaining elements - until the target value is either or until the entire array has been searched. Binary search
  • 18. This is the array we are going to perform binary search Let’s search for the number 19. Example In the first step we start with the entire sequence and consider its middle element 8
  • 19. As the sequence is sorted and 8 is less than the key 19, we conclude that 19 can occur only in the second half of the sequence, not including the middle element 8.
  • 20. We then repeat the same with the subsequence consisting of the second half. Its middle element is 21, which is greater than the key 19.
  • 21. We thus next consider the first half of this subsequence, whose middle element is 11. In the last step the subsequence consists of one element, and this element is both the middle element and is equal to the key 19.
  • 22. Thus we have discovered 19 in the sequence. Observe that only the 4 elements (8, 21, 11 and 19) indexed by the middle point index mid in the figure are compared against the key 19. If the same search is performed in linear search, it will take 8 comparisons.
  • 24. Procedure binary_search A ← sorted array n ← size of array x ← value to be searched Set lowerBound = 1 Set upperBound = n while x not found if upperBound < lowerBound EXIT: x does not exists. set midPoint = (upperBound + lowerBound ) / 2 if A[midPoint] < x set lowerBound = midPoint + 1 if A[midPoint] > x set upperBound = midPoint - 1 if A[midPoint] = x EXIT: x found at location midpoint end while end procedure
  • 25. Time Complexity n n/2 n/2 n/4 n/4 n/8 n/8 Array Sub -Array . . . Therefore the last sub array will be n/n or n/2k K = number of comparisons made
  • 26. n/n = n/2k n/2k = 1 n = 2k Taking log on both the sides Log n = log 2k Log n = k log 2 log n = k Time Complexity is O(log n)
  • 27. Space Complexity This type of search requires only a single unit of memory to store the element being searched. This is not relevant to the size of the input Array. Hence, the Space Complexity of Binary Search is O(1).