Search Algo
Search Algo
SEARCHING TECHIQUES
Algorithms
• An algorithm is a well defined list of steps for solving a particular problem.
• The time and space are two major measures of the efficiency of an algorithm.
• The complexity of an algorithm is the function which gives the running time and /
or space in terms of input size.
Searching Algorithms
• Searching Algorithms are designed to check for an element or retrieve an element
from any data structure where it is stored.
• Based on the type of search operation, these algorithms are generally classified
into two categories:
• Linear Search
• Binary Search
Linear Search
• Linear search is a very basic and simple search algorithm.
• In Linear search, we search an element or value in a given array by traversing the
array from the starting, till the desired element or value is found or we can
establish that the element is not present.
• It compares the element to be searched with all the elements present in the array
and when the element is matched successfully, it returns the index of the element
in the array, else it return -1.
• Linear search is applied on unsorted or unordered lists, when there are fewer
elements in a list.
Linear Search - Example
• Search each record of the file, one at a time, until finding the given value.
Algorithm for Linear Search
• In Steps 1 and 2 of the algorithm, we initialize the
value of POS and I.
• In Step 3, a while loop is executed that would be
executed till I is less than N.
• In Step 4, a check is made to see if a match is
found between the current array element and VAL.
• If a match is found, then the position of the array
element is printed, else the value of I is
incremented to match the next element with VAL.
• If all the array elements have been compared with
VAL and no match is found, then it means that
VAL is not present in the array.
Program for Linear Search
Linear Search - Advantages
• When a key element matches the first element in the array, then linear search
algorithm is best case because executing time of linear search algorithm is O(n),
where n is the number of elements in an array.
• The list doesn’t have to sort. Contrary to a binary search, linear searching does not
demand a structured list.
• Not influenced by the order of insertions and deletions. Since the linear search
doesn’t call for the list to be sorted, added elements can be inserted and deleted.
Linear Search - Disadvantages
• The drawback of a linear search is the fact that it is time consuming if the size of
data is huge.
• Every time a vital element matches the last element from the array or an essential
element does not match any element Linear search algorithm is the worst case.
Binary Search
• Binary Search is used with sorted array or list. In binary search, we follow the
following steps:
1) We start by comparing the element to be searched with the element in the
middle of the list/array.
2) If we get a match, we return the index of the middle element and terminate
the process
3) If the element/number to be searched is greater than the middle element, we
pick the elements on the left/right side of the middle element, and move to
step 1.
4) If the element/number to be searched is lesser in value than the middle
number, then we pick the elements on the right/left side of the middle
element, and move to step 1.
Binary Search - Example
• Binary Search is useful when there are large number of elements in an array and
they are sorted.
Algorithm for Binary Search
• In Step 1, we initialize the value of variables, BEG,
END, and POS.
• In Step 2, a while loop is executed until BEG is less
than or equal to END.
• In Step 3, the value of MID is calculated.
• In Step 4, we check if the array value at MID is equal to
VAL. If a match is found, then the value of POS is
printed and the algorithm exits. If a match is not found,
and if the value of A[MID] is greater than VAL, the
value of END is modified, otherwise if A[MID] is
greater than VAL, then the value of BEG is altered.
• In Step 5, if the value of POS = –1, then VAL is not
present in the array and an appropriate message is
printed on the screen before the algorithm exits.
Program for Binary Search
Binary Search (Cont..)
Advantages Disadvantages
• It eliminates half of the list from • It employs recursive approach which
further searching by using the result of requires more stack space.
each comparison. • Programming binary search algorithm
• It indicates whether the element being is error prone and difficult.
searched is before or after the current • The interaction of binary search with
position in the list. memory hierarchy i.e. caching is poor.
• This information is used to narrow the
search.
• For large lists of data, it works
significantly better than linear search.
Difference between Linear & Binary Search
Linear Search Binary Search
• Linear Search necessitates the input • Binary Search necessitates the input
information to be not sorted information to be sorted.
• Linear Search needs equality • Binary Search necessitates an ordering
comparisons. contrast.
• Linear search has complexity C(n)= • Binary Search has complexity C(n)=
n/2. log 2 n.
• Linear Search needs sequential access. • Binary Search requires random access
to this information.
COMPLEXITY
TIME, SPACE MATRIX
Linear Search – Time & Space Matrix
• Time Complexity
o We need to go from the first element to the last so, in the worst case we have
to iterate through ‘n’ elements, n being the size of a given array.
• Space Complexity
o We just need to store three values: `lower_bound`, `upper_bound`, and
`middle_position`.
• It works similar to the way you sort playing cards in your hands
INSERTION SORT
• Insertion sorts works by taking
elements from the list one by one and Pseudo code
inserting them in their current position
into a new sorted list. INSERTION-SORT(A)
• Insertion sort consists of N - 1 passes, for i = 1 to n
where N is the number of elements to key ← A [i]
be sorted. j←i–1
• The ith pass of insertion sort will while j > = 0 and A[j] > key
insert the ith element A[i] into its A[j+1] ← A[j]
j←j–1
rightful place among A[1], A[2] to
End while
A[i - 1]. After doing this insertion the A[j+1] ← key
records occupying A[1]....A[i] are in End for
sorted order.
Working of insertion sort – Ascending order
• We start by considering the second element of the given array, i.e. element at index 1, as
the key.
• We compare the key element with the element(s) before it, i.e., element at index 0:
– If the key element i.e index 1 is less than the first element, we insert the key element
before the first element.(swap)
– If the key element is greater than the first element, then we insert it remains at its
position
– Then, we make the third element of the array as key and will compare it with elements
to it's left and insert it at the right position.
• And we go on repeating this, until the array is sorted
.
Example
Let us now understand working with the
following example:
Consider the following array: 25, 17, 31,
13, 2 where N=5