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

Dsa – data structure and algorithms searching

  • 1.
  • 2.
    DSA – DataStructure 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.
  • 3.
  • 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 searchor 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 thearray 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 firstvalue 3? No, not it. Is it the next element? Is the second value 3? Not there either. The next element?
  • 8.
    Is the thirdvalue 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 thelinear 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 LinearSearch, 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 keyis 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 averagecase 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 typeof 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).
  • 15.
  • 16.
    1. The binarysearch 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 thetarget 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 thearray 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 sequenceis 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 repeatthe same with the subsequence consisting of the second half. Its middle element is 21, which is greater than the key 19.
  • 21.
    We thus nextconsider 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 havediscovered 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.
  • 23.
  • 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/4n/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 typeof 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).