CS3401-UNIT1
CS3401-UNIT1
UNIT 1
INTRODUCTION
Algorithm analysis: Time and space complexity - Asymptotic Notations and its
properties Best case, Worst case and average case analysis – Recurrence
relation: substitution method - Lower bounds – searching: linear search,
binary search and Interpolation Search, Pattern search: The naïve string-
matching algorithm - Rabin-Karp algorithm - Knuth-Morris-Pratt algorithm.
Sorting: Insertion sort – heap sort
Part-A
2. What is Pseudocode?
Time Complexity:
Time Complexity is defined as the number of times a particular
instruction set is executed rather than the total time taken. It is
PREPARED BY D.SUJATHA, HoD/AI&DS 2
SRRCET/CSE/II-YEAR/IV-SEM/CS3401-ALGORITHM
17. What are the best case, average case & worst case
complexity of linear search?
Best Case: In the best case, the key might be present at the
first index. So the best case complexity is O(1)
Worst Case: In the worst case, the key might be present at the
last index i.e., opposite to the end from which the search has
started in the list. So the worst-case complexity is O(N) where
N is the size of the list.
Average Case: O(N)
18. What are the best case, average case & worst case
complexity of binary search?
Best Case: O(1) This occurs when the middle element is the
target element.
Average Case: O(log n). This is the expected complexity when
the target element is not necessarily at the middle.
Worst Case: O(log n). This happens when the search has to go
all the way down the tree, but it's still logarithmic due to the
halving nature of binary search.
RABIN-KARP-MATCHER (T, P, d, q)
1. n ← length [T]
2. m ← length [P]
3. h ← dm-1 mod q
4. p← 0
5. t0 ← 0
6. for i ← 1 to m
9. for s ← 0 to n-m
10. do if p = ts
NAIVE-STRING-MATCHER (T, P)
1. n ← length [T]
2. m ← length [P]
3. for s ← 0 to n -m
4. do if P [1.....m] = T [s + 1....s + m]