Loading...
Loading...
An algorithm is a finite, well-defined sequence of steps to solve a problem.
Properties of Good Algorithm:
Asymptotic Notations:
| Notation | Meaning | Use | |---|---|---| | O (Big O) | Upper bound (worst case) | Most commonly used | | Ω (Omega) | Lower bound (best case) | Best case analysis | | Θ (Theta) | Tight bound (average case) | When best=worst |
Master Theorem for T(n) = aT(n/b) + f(n):
Compare adjacent elements and swap if out of order. Repeat n-1 passes.
def bubble_sort(arr):
n = len(arr)
for i in range(n-1):
for j in range(n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
Split array in half, sort each half, merge sorted halves.
def merge_sort(arr):
if len(arr) <= 1: return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
Pick pivot, partition array, recursively sort partitions.
Build max-heap, extract max repeatedly.
| Algorithm | Best | Average | Worst | Space | Stable | |---|---|---|---|---|---| | Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | | Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No | | Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | | Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | | Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No | | Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | | Counting Sort | O(n+k) | O(n+k) | O(n+k) | O(k) | Yes |
Check each element one by one.
On sorted array: compare middle, eliminate half.
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target: return mid
elif arr[mid] < target: low = mid + 1
else: high = mid - 1
return -1
Strategy: Divide problem → Conquer subproblems → Combine results.
Examples:
DP solves problems with overlapping subproblems and optimal substructure by storing solutions.
Two Approaches:
# Naive recursion: O(2ⁿ) — exponential!
# DP with memoization: O(n)
def fib(n, memo={}):
if n in memo: return memo[n]
if n <= 1: return n
memo[n] = fib(n-1, memo) + fib(n-2, memo)
return memo[n]
Given weights and values of n items, maximize value in knapsack of capacity W.
dp[i][w] = max(dp[i-1][w], value[i] + dp[i-1][w-weight[i]])
if X[i] == Y[j]: dp[i][j] = dp[i-1][j-1] + 1
else: dp[i][j] = max(dp[i-1][j], dp[i][j-1])
Make locally optimal choice at each step, hoping for global optimum.
Greedy works for: Activity selection, Huffman coding, Kruskal/Prim MST, Dijkstra.
Select maximum non-overlapping activities. Strategy: Always select activity with earliest finish time.
Variable-length encoding — frequent characters get shorter codes. Build tree: repeatedly merge two nodes with lowest frequency.
Single source shortest path for non-negative weights.
Handles negative weights. Detects negative cycles.
All-pairs shortest path.
Kruskal's Algorithm:
Prim's Algorithm:
Q1 (2023): Apply merge sort on [38, 27, 43, 3, 9, 82, 10]
Split: [38,27,43] [3,9,82,10]
Split: [38][27,43] [3,9][82,10]
Split: [38][27][43] [3][9][82][10]
Merge: [27,38,43] [3,9,10,82]
Merge: [3,9,10,27,38,43,82] ✓
Q2 (2022): Find shortest path from A to all vertices using Dijkstra. Use priority queue, process minimum distance vertex first, relax all edges.
Q3 (2024): Solve 0/1 knapsack: items=(2,6),(2,10),(3,12), capacity=5
dp[3][5]:
Item 1 (w=2,v=6): row 1: [0,0,6,6,6,6]
Item 2 (w=2,v=10): row 2: [0,0,10,10,16,16]
Item 3 (w=3,v=12): row 3: [0,0,10,12,16,22]
Maximum value = 22
Complete Algorithms notes for B.Tech CS Semester 3 — sorting, searching, divide and conquer, dynamic programming, greedy algorithms, graph algorithms, and complexity analysis.
54 pages · 2.7 MB · Updated 2026-03-11
Divide & Conquer splits problem and solves subproblems independently (merge sort). Dynamic Programming solves overlapping subproblems and stores results (memoization) to avoid recomputation.
Average case: pivot divides array roughly in half each time. Worst case: pivot is always smallest or largest element (already sorted array with first element as pivot), creating unbalanced partitions.
Object Oriented Programming (OOP) — Complete Notes with Java & C++
Object Oriented Programming
Data Structures Complete Notes — B.Tech CS Sem 3
Data Structures
DBMS Complete Notes — B.Tech CS Sem 4
Database Management Systems
Compiler Design — Complete Notes CS Sem 6
Compiler Design
Machine Learning Complete Notes — B.Tech CS Sem 6
Machine Learning
Your feedback helps us improve notes and tutorials.