UNIT 1 - CS3401-Algorithms
UNIT 1 - CS3401-Algorithms
Searching is a technique to find the location of a given The element to be searched is either at last position or
element (search key) in an array or list. not present. The Worst case time complexity of the linear
search algorithm is O(n), where n is the number of elements
1) Linear Search / Sequential Search being searched.
Linear Search is a searching technique in which every element Best Case Average Case Worst Case
is compared with key element Linearly (Sequentially). Elements O(1) O(n) O(n)
can be in any order. It is also called sequential search. Linear
search performs equality comparisons. Input data need not to Space Complexity
be in sorted order. The space complexity of the Linear Search Algorithm is
O(1).
Time Complexity
Time Complexity
Space Complexity
The space complexity of the interpolation Search
Algorithm is O(1).
Time Complexity
a[mid]=2
since a[mid]<4
search the right half
Time Complexity
a[mid]=4 Best Case Average Case Worst Case
since a[mid]=4 O(n) O(n) O(n*m)
Element found. Return index 2.
Comparisons:
((n-m+1)*m)
CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-I: 5
Example: The Naive String-Matching Algorithm Search
Suppose T = 1011101110
P = 111
Find all the Valid Shift.
Valid Shifts: s=2 and s=6. Thus, pattern matching can be obtained.
Show the comparisons the naive string matcher makes for the pattern
P = 0001 in the text T = 000010001010001.
Valid Shifts: s=1, s=5 and s=11. Thus, pattern matching can be obtained.
d=decimal value
q=prime number
m=window size
CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-I: 8
Example 2:
Text: a b b c a a c a
Pattern: a c a
h(p)=h(T6)
aca = aca
Spurious Hit
A spurious hit occurs when the hash value of the pattern
matches the hash value of the text, but the text differs from
the pattern.
Spurious hit increases the time complexity of the algorithm.
In order to minimize spurious hit, use modulus.
Time Complexity
Best Case Average Case Worst Case
O(n + m) O(n + m) O(n*m)
Comparisons:
Spurious Hit occurs with shift 3 (15), shift 4 (59) and shift 5 (92) ((n-m+1)*m)
Spurious Hit: Totally 3 shifts
Time Complexity
n ←length [T] ← 15
m ←length [P] ← 7
1) Insertion Sort
Insertion sort is a sorting algorithm that insert an unsorted element
in the correct position in each iteration.
Space Complexity
The space complexity of insertion sort is O(1). In
Insertion sort, an extra variable is required for swapping.
Heap Sort Algorithm First, we have to construct a heap from the given array and convert
a) Build a heap from the given input array. it into max heap.
b) Repeat the following steps until the heap contains only
one element:
Swap the root element of the heap (which is the
largest element) with the last element of the heap.
Remove the last element of the heap (which is now
in the correct position).
Heapify the remaining elements of the heap.
After converting the given heap into max heap, the array elements
are:
In the next step, we have to delete the root element (89) from the max
heap. To delete this node, we have to swap it with the last node (11).
After swapping the array element 89 with 11 and converting the heap
into max-heap, the elements of array are:
After swapping the array element 81 with 54 and converting the heap
into max-heap, the elements of array are: After swapping the array element 54 with 14 and converting the heap
into max-heap, the elements of array are:
In the next step, we have to delete the root element (76) from the max
heap. To delete this node, we have to swap it with the last node (9). In the next step, again we have to delete the root element (22) from
the max heap. To delete this node, we have to swap it with the last
node (11).
After swapping the array element 76 with 9 and converting the heap
into max-heap, the elements of array are: After swapping the array element 22 with 11 and converting the heap
into max-heap, the elements of array are:
2) Write about notion of algorithm 6) Define the asymptotic notation “Big oh” (O)
O(f(n)) represents an upper bound on the growth rate of a
function f(n). ( f(n) ≤ C * g(n) for all n, n≥K )
O notation represents the worst-case scenario for the running
time or space complexity of an algorithm.
Linear Search
Time Complexity
Best Case Average Case Worst Case
O(1) O(n) O(n)
Space Complexity: O(1)
Binary Search
Time Complexity
19) Difference between linear and binary search. Best Case Average Case Worst Case
Linear Search Binary Search
O(1) O(log n) O(log n)
Linear search performs Binary search performs
equality comparisons ordering comparisons Space Complexity: O(1)
It is also called sequential It is also called half-interval Interpolation Search
search. search. Time Complexity
Input data need not to be in Input data need to be in Best Case Average Case Worst Case
sorted order. sorted order. O(1) O(log (log n)) O(n)
Space Complexity: O(1)
Rabin-Karp Algorithm