0% found this document useful (0 votes)
2 views14 pages

3 Dsa Searching

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

3 Dsa Searching

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SEARCHING:

LINEAR SEARCH
BINARY SEARCH
SEARCHING
• Searching refers to the act of finding
whether a particular value is present in the
collection of values.
• Successful and unsuccessful search.
• Linear search
• Binary search
• Which method to use? (depends entirely
on how the values are organized in the
array)
Linear Search
• Also called sequential search.
• It works by comparing the value to be
searched with every element of the array
one by one in a sequence until a match is
found.
• Mostly used to search an unordered list of
elements
Linear Search
Linear Search: Time Complexity
• Best Case: When the value is equal to the
first element: O(1)
• Worst Case: When the value is equal to the
last element: O(n)
• Any ways to improve the performance of
linear search?
• Use sorted array
Binary Search
• Searching a particular name in the telephone
directory
• Searching a particular word in the dictionary
• Discard half of values in every iteration.
• Elements must be sorted before applying
binary search.
• int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
• Search val = 9
Binary Search
Complexity
• The complexity of the algorithm is
calculated depending on the number of
comparisons that are made.
• In the binary search, with each
comparison, the size of the segment
where search has to be made is reduced to
half.
• Total number of comparisons = O(log2n)
Difference between LS and BS
• In linear search , one comparison operation
reduces the size of the problem by one whereas
in binary search one comparison operation
reduces the size of the problem by half.
• Binary search requires the input data to
be sorted; linear search doesn't.
• Binary search requires an ordering
comparison (like >, <); linear search only
requires equality comparisons (=)
• Binary search has complexity O(log n); linear
search has complexity O(n)
Can Binary Search be applied on Unsorted
Array?
• NO, it is not possible to use or implement
Binary Search on unsorted arrays, because,
the repeated targeting of the mid element
of one half depends on the sorted order of
data structure.
• Another approach : Takes more time and
extra space. Hence not recommended.
If the linked list is sorted can
we apply binary search ?
• No, as it is meaningless because time
complexity will be O(n) due to fact that we
cannot do indexing in linked list
Search in an almost sorted array
• Given an array which is sorted, but after
sorting some elements are moved to
either of the adjacent positions, i.e., arr[i]
may be present at arr[i+1] or arr[i-1].
• Write an efficient function to search an
element in this array.
• {2, 3, 10, 4, 40}
Which is the best data structure for
faster searching of string?
• The best data structure for faster searching of
string is TRIE. (ReTRIEval of data)
• Tries are an extremely special and useful data-
structure that are based on the prefix of a
string.
• How to use binary search to find specific
words in a file.
• How to use the Binary search algorithm to
display duplicates of a sorted array, with
all their indexes.
• Suggest some linear Search optimization
techniques.
• Interpolation search : Improvement over
binary search, where the values in the
sorted array are uniformly distributed.

You might also like