Design & Analysis of Algorithms
| # | Algorithm | Paradigm | Best | Average | Worst |
|---|---|---|---|---|---|
| 01 | Linear search | Brute force | O(1) | O(n) | O(n) |
| 02 | Binary search | Divide & conquer | O(1) | O(log n) | O(log n) |
| 03 | Bubble sort | Brute force | O(n²) | O(n²) | O(n²) |
| 04 | Selection sort | Brute force | O(n²) | O(n²) | O(n²) |
| 05 | Insertion sort | Incremental | O(n) | O(n²) | O(n²) |
| 06 | Radix sort | Non-comparison | O(d·n) | O(d·n) | O(d·n) |
| 07 | Bucket sort | Distribution | O(n+k) | O(n+k) | O(n²) |
| 08 | Quick sort | Divide & conquer | O(n log n) | O(n log n) | O(n²) |
| 09 | Merge sort | Divide & conquer | O(n log n) | O(n log n) | O(n log n) |
| 10 | Fibonacci (recursive) | Recursion | O(2ⁿ) | O(2ⁿ) | O(2ⁿ) |
| 11 | Fibonacci (DP) | Dynamic programming | O(n) | O(n) | O(n) |
| 12 | Matrix chain multiplication | Dynamic programming | O(n³) | O(n³) | O(n³) |
| 13 | 0/1 knapsack | Dynamic programming | O(nW) | O(nW) | O(nW) |
| 14 | Longest common subsequence | Dynamic programming | O(mn) | O(mn) | O(mn) |
| 15 | Fractional knapsack | Greedy | O(n log n) | O(n log n) | O(n log n) |
| 16 | Job sequencing with deadlines | Greedy | O(n log n) | O(n²) | O(n²) |
| 17 | Huffman coding | Greedy | O(n log n) | O(n log n) | O(n log n) |
| 18 | N-Queen | Backtracking | O(n!) | O(n!) | O(n!) |
| 19 | Travelling salesman (DP) | Dynamic programming | O(n²·2ⁿ) | O(n²·2ⁿ) | O(n²·2ⁿ) |
| 20 | Breadth-first search | Graph traversal | O(V+E) | O(V+E) | O(V+E) |
| 21 | Depth-first search | Graph traversal | O(V+E) | O(V+E) | O(V+E) |
| 22 | Job assignment (branch & bound) | Branch & bound | O(n²) | O(n!) | O(n!) |
| 23 | Dijkstra | Greedy | O(V²) | O(V²) | O(V²) |
| 24 | Floyd–Warshall | Dynamic programming | O(V³) | O(V³) | O(V³) |
| 25 | Bellman–Ford | Dynamic programming | O(VE) | O(VE) | O(VE) |
| 26 | Prim's MST | Greedy | O(V²) | O(V²) | O(V²) |
| 27 | Kruskal's MST | Greedy | O(E²) | O(E²) | O(E²) |
Divide & conquer
Split into independent subproblems, solve each, combine
Binary search, merge sort, quick sort
Dynamic programming
Overlapping subproblems + optimal substructure, so store and reuse the answers
Fibonacci DP, MCM, 0/1 knapsack, LCS, Floyd–Warshall, Bellman–Ford, TSP
Greedy
Take the locally best choice; optimal only with the greedy-choice property
Fractional knapsack, job sequencing, Huffman, Prim, Kruskal, Dijkstra
Backtracking
Build a solution step by step and undo when a constraint fails
N-Queen, subset sum, graph colouring
Branch & bound
Explore the state space, prune with lower or upper bounds
Job assignment, TSP, 0/1 knapsack
#include <stdio.h>
#include <time.h>
clock_t start = clock();
/* ... algorithm ... */
clock_t end = clock();
double t = (double)(end - start) / CLOCKS_PER_SEC;
printf("\nTime taken:%f seconds", t);int n, i;
printf("Enter the number of elements:");
scanf("%d", &n);
int a[n];
printf("Enter the elements:");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);int w[50][50];
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &w[i][j]);int temp = a[i];
a[i] = a[j];
a[j] = temp;int count = 0;
for (...) {
count++; // one comparison
if (a[j] > a[j + 1]) ...
}
printf("\nNumber of comparisons:%d", count);clock() is coarse: on six numbers it reports almost 0 s. The comparison count is the honest measure.
gcc -o 05.out 05_insertion.c
./05.out
# all at once
for f in *.c; do gcc -o "${f%.c}.out" "$f"; doneType the inputs on one line separated by spaces, e.g. 42 8 27 14 35 19.