Sorting Algorithms Comparisons
Sorting Algorithms Comparisons
Notes:
• n = number of elements
• k = range of input values (for counting/bucket sort)
değil insertion pardon
• d = number of digits/characters (for radix sort)
• Bucket Sort stability: Depends on the sorting algorithm used within each bucket (biz counting kullanıyoz >>yes)
• Quick Sort space: O(log n) average case due to recursion stack, O(n) worst case
Selection Algorithms:
Algorithm Type Min Average Max Space Description In- Deterministic?
Runtime Runtime Runtime Complexity Place?
Random Divide and O(n) O(n) O(n²) O(log n) Finds kth smallest element using Yes No
Select Conquer randomized pivot selection and
partitioning
Random Helper O(n) O(n) O(n) O(1) Partitions array around randomly Yes No
Partition Function selected pivot element
Median of Divide and O(n) O(n) O(n) O(log n) Finds kth smallest element with Yes Yes
Medians Conquer guaranteed good pivot selection
(deterministic)
Notes:
Sorting Algorithms:
• Heapsort: All cases O(n log n) - performance doesn't depend on input order
• Selection Sort: All cases O(n²) - always scans entire unsorted portion regardless of input
• Insertion Sort:
o Best O(n): Array is already sorted (each element needs only 1 comparison)
o Worst O(n²): Array is reverse sorted (each element needs maximum shifts)
• Bubble Sort:
o Best O(n): Array is already sorted (optimized version with early termination)
o Worst O(n²): Array is reverse sorted (maximum swaps needed)
• Merge Sort: All cases O(n log n) - always divides and merges the same way
• Quick Sort:
o Best O(n log n): Pivot always divides array into equal halves
o Worst O(n²): Pivot is always smallest/largest element (already sorted/reverse sorted arrays with poor pivot
selection)
• Bucket Sort:
o Best O(n + k): Elements uniformly distributed across buckets
o Worst O(n²): All elements fall into same bucket, degenerates to sorting algorithm used within bucket
• Counting/Radix Sort: Performance independent of input order
• Topological Sort:
o All cases O(V + E): Performance depends on graph structure, not vertex ordering
o Only works on DAGs: Directed Acyclic Graphs - if cycle exists, topological sort is impossible
o Two main approaches: DFS-based (using finishing times) or BFS-based (Kahn's algorithm using in-degrees)
Selection Algorithms:
• Random Select:
o Best/Average O(n): Good pivot selection reduces problem size effectively
o Worst O(n²): Consistently poor pivot selection (always picks min/max element)
• Median of Medians:
o All cases O(n): Guarantees good pivot selection by finding median of medians as pivot
o Algorithm: Divides array into groups of 5, finds median of each group, recursively finds median of those median
Algorithm Summary Tables:
Rod Cutting Problem
Algorithm Approach Logic Formula/Recurrence Time Space Variables Properties Key Points
1. Naive Recursive Try every r(n) = max(p[i] + O(2^ O(n) n=rod length, p[i]=price ✓ Optimal substructure, ✓ - Very slow (exponential), -
Recursive possible cut r(n-i)) for i=1 to n n) stack for length i, r(n)=max Recomputes same
Overlapping subproblems,
position and space revenue for length n ✗ Greedy-choice property subproblems, - Base case: r(0)
take maximum =0
2. Top- Dynamic Same logic but r(n) = max(p[i] + O(n²) O(n) Same as above + memo ✓ Optimal substructure, ✓ - Good approach, - Saves
Down with Programmin cache results r(n-i)) with memo table for caching subproblem results, - Avoids
Overlapping subproblems,
Memoizati g to avoid table redundant work
✗ Greedy-choice property
on (Recursive) recomputation
3. Bottom- Dynamic Build solution r[j] = max(p[i] + O(n²) O(n) j=current length being ✓ Optimal substructure, ✓ - Most efficient approach, - No
Up Programmin from smallest r[j-i]) for i=1 to j computed, i=cut position, recursion overhead, - Computes
Overlapping subproblems,
g (Iterative) subproblems r[j]=max revenue for ✗ Greedy-choice property each subproblem once
up length j
Problem Definition
Variable Definitions
Notes
Algorithm Approach Logic Formula/Recurrence Time Space Variables Properties Key Points
MCM(p,i,j) =
1. Naive Recursive Try all
min(MCM(p,i,k) +
O(2^n) O(n) p[]=dimension array, ✓ Optimal substructure, ✓ - Exponential time, - Base
Recursive possible MCM(p,k+1,j) + p[i- stack i,j=matrix range, k=split Overlapping subproblems, case: i=j return 0, - Explores
split points 1]×p[k]×p[j])for k=i to j-1 space point, MCM(p,i,j)=min ✗ Greedy-choice property all possible splits
k, take cost
minimum
cost
2. Top- Dynamic Same logic Same recurrence with O(n³) O(n²) Same as above + ✓ Optimal substructure, ✓ - Reduces exponential to
Down with Programmin but cache memo table m[i,j] m[i,j]=memo table stores Overlapping subproblems, polynomial, - Avoids
Memoizati g results in min costs ✗ Greedy-choice property recomputation, - Check
on (Recursive) m[i,j] table memo before computing
3. Bottom- Dynamic Fill table m[i,j] = min(m[i,k] +
m[k+1,j] + p[i-
O(n³) O(n²) l=chain length, i,j=matrix ✓ Optimal substructure, ✓ - Most common approach, -
Up Programmin from smaller 1]×p[k]×p[j]) indices, k=split point, Overlapping subproblems, No recursion overhead, - Fill
g (Iterative) lengths to m[i,j]=min cost table ✗ Greedy-choice property by increasing chain length
larger
Problem Definition
• Input: Chain of matrices A₁×A₂×...×Aₙ with dimensions p₀×p₁, p₁×p₂, ..., pₙ₋₁×pₙ
• Goal: Find optimal parenthesization to minimize scalar multiplications
• Key Insight: Different parenthesizations have vastly different costs
• Brute Force Complexity: Catalan number C(n-1) = (2n-2)!/(n-1)!n! ≈ 4^n ways to parenthesize (exponential
without DP)
Algorithm Approach Logic Formula/Recurrence Time Space Variables Properties Key Points
1. Brute Generate all Generate all 2^m Generate all possible O(n × 2^m) O(1) X,Y=input ✓ Optimal substructure, - Exponential and very inefficient,
Force subsequences subsequences of subsequences and sequences, ✓ Overlapping - Generate 2^m subsequences for
X, check if each compare m,n=lengths X, - Check each in Y takes O(n)
subproblems, ✗
appears in Y time
Greedy-choice property
2. Naive Recursive Compare LCS(i,j) = 0, if O(2^(m+n)) O(m+n) i,j=current ✓ Optimal substructure, - Exponential time, - Recomputes
Recursive characters: if i=0 or j=0; stack positions, same subproblems, - Base case:
✓ Overlapping
match, add 1 + X[i],Y[j]=ch empty sequence gives LCS=0
LCS(i-1,j-1)+1, subproblems, ✗
LCS of if X[i]=Y[j]; aracters,
Greedy-choice property
remaining, else LCS(i,j)=len
take max of two max(LCS(i-1,j), gth of LCS
options LCS(i,j-1)) else
3. Top-Down Dynamic Same recursive Same recurrence with O(m×n) O(m×n) Same as ✓ Optimal substructure, - Reduces exponential to
with Programming logic but cache memo table c[i,j] above + ✓ Overlapping polynomial, - Check memo before
Memoization (Recursive) results in c[i,j] c[i,j]=memo computing, - Store computed
subproblems, ✗
table table storing results
Greedy-choice property
LCS lengths
4. Bottom-Up Dynamic Fill table from c[i,j] = 0, if O(m×n) O(m×n) i,j=table ✓ Optimal substructure, - Most efficient approach, - Fill
Programming smaller i=0 or j=0; basic, indices, table row by row, - Can optimize
✓ Overlapping
(Iterative) subproblems to O(min(m, c[i,j]=LCS space to O(min(m,n))
c[i-1,j-1]+1, if subproblems, ✗
larger X[i]=Y[j]; n)) length table,
Greedy-choice property
optimized X,Y=sequen
max(c[i- ces
1,j],c[i,j-1])
else
Problem Definition
Important Notes
Algorithm Approach Logic Formula/Recurrence Time Space Variables Properties Key Points
1. Naive Recursive For each activity, c[i,j] = 0 if Sᵢⱼ = ∅; O(n × O(n) i,j=time indices, ✓ Optimal substructure, - Very slow (exponential), -
Recursive try including it or max{c[i,k] + 1 + c[k,j]} 2^n) stack k=activity index, ✓ Overlapping Explores all possible
not, take if Sᵢⱼ ≠ ∅ space c[i,j]=max activities in combinations, - Recomputes
subproblems, ✓
maximum time interval, subproblems
Greedy-choice property
Sᵢⱼ=compatible activities
2. Dynamic Dynamic Build table c[i,j] c[i,j] = max{c[i,k] + 1 O(n³) O(n²) Same as above + table ✓ Optimal substructure, - Polynomial time, - No recursion
Programmi Programmin for all intervals, + c[k,j]} for all k where c[i,j] for memoization ✓ Overlapping overhead, - Fill table
ng g (Iterative) fill from smaller aₖ ∈ Sᵢⱼ subproblems, ✓
systematically
to larger intervals
Greedy-choice property
3. Greedy Greedy Always select AS(s,f,n): Sort by O(n O(1) s[i]=start times, ✓ Optimal substructure, - FASTEST approach, - Sort
Algorithm activity that finish time, select log n) f[i]=finish times, ✗ Overlapping once by finish time, - Always
finishes earliest earliest finishing, n=number of activities optimal for this problem
subproblems, ✓
among remaining remove overlapping
Greedy-choice property
compatible
activities
Problem Definition
• Input: n activities with start times s[i] and finish times f[i]
• Goal: Select maximum number of non-overlapping activities
• Key Insight: Activities are compatible if they don't overlap in time
• Compatible Activities: Two activities don't overlap if fᵢ ≤ sⱼ or fⱼ ≤ sᵢ
Variable Definitions
Algorithm Approa Logic Formula/Recurrenc Time Space Variables Properties Key Points
ch e
1. Brute Exhaust Try all possible full ABC = Σ(frequency O(4^n) O(n) n=alphabet size, ✓ Optimal substructure, ✗ - Exponential (Catalan numbers), -
Force ive binary trees with n × code_length) for ABC=average bits Overlapping subproblems, Tests all possible tree structures, -
Search leaves, calculate all characters per character ✓ Greedy-choice property Impractical for large alphabets
ABC for each
2. Shannon- Divide- Split alphabet into Split S into S₁, S₂ O(n² log O(n log S₁, S₂=split ✓ Optimal substructure, ✗ - Not always optimal, - Top-down
Fano (1948) and- two equal-frequency where Σf_l in S₁ ≈ n) n) subsets, recursive Overlapping subproblems, approach, - Attempts balanced splits
Conque parts, recursively Σf_l in S₂ = 0.5 depth=O(logn) ✗ Greedy-choice property
r build subtrees
3. Huffman Greedy Always merge two f_w = f_y + f_z O(n log n) O(n) Q=priority queue, ✓ Optimal substructure, ✗ - OPTIMAL for prefix codes, -
Algorithm (Botto lowest-frequency where y,z are min y,z=min freq Overlapping subproblems, Uses min-heap/priority queue, -
m-up) nodes, build tree frequencies chars, w=new ✓ Greedy-choice property Bottom-up construction
from leaves to root internal node
Problem Definition
Variable Definitions
Algorithm Approach Logic Formula/Recurrence Time Space Variables Properties Key Points
1. Brute Exhaustive Try all possible Check all n^(n-2) O(n^n) O(n) G=(V,E), ✗ Not optimal for large - NOT OPTIMAL -
Force Search spanning trees, spanning trees w=edge weights, graphs ✗ Exponential time Exponential complexity
calculate weight of (Cayley's formula) T=spanning tree, Theoretical approach only
complexity ✓ Guarantees
each, return minimum n= Guarantees global minimum
absolute minimum ✗ Infeasible for n>10-15
Impractical for real vertices
applications
2. Prim's Greedy Grow tree from single At each step: add O(m log O(n) Q=priority queue, ✓ Optimal substructure, ✗ - OPTIMAL, - Vertex-
Algorithm (Vertex- vertex, always add min{w(u,v) : u∈T, n) with key[v]=min edge Overlapping subproblems, ✓ centric approach, - Good for
based) minimum weight v∉T} heap, weight to v, Greedy-choice property dense graphs, - Uses priority
edge connecting tree O(n²) parent[v]=predec queue
to new vertex with essor
array
3. Greedy Sort all edges by Sort edges: O(m log O(m log O(n) Sorted edge list ✓ Optimal substructure, ✗ - OPTIMAL, - Edge-centric
Kruskal's (Edge- weight, add lightest m), For each edge: if m) E, Union-Find Overlapping subproblems, ✓ approach, - Good for sparse
Algorithm based) edge that doesn't FindSet(u) ≠ structure, Greedy-choice property graphs, - Uses Union-Find
create cycle using FindSet(v) then A=MST edge set for cycle detection
Union-Find Union(u,v)
Problem Definition
Variable Definitions
Implementation Approach Logic Operations Time per Operation Space Variables Properties Key Points
1. Naive Linked Linked List Each set is linked MakeSet(x), MakeSet: O(1), O(n) Each element ✗ Expensive - Union updates all pointers, - Total:
List list, maintain FindSet(x), FindSet: O(1), Union: points to Union O(m×n) for m operations
pointer to Union(x,y) O(n) representative
representative
2. Weighted Linked List Always append Same operations, MakeSet: O(1), O(n) Size of each ✓ Weighted - Much faster Union, - Each element
Union + Size smaller list to Union chooses FindSet: O(1), Union: set, weighted Union moved ≤ log n times, - Total: O(m + n
Optimization larger list smaller list O(log n) amortized union Heuristic log n)
heuristic
Problem Definition
Algorithm Approach Logic Formula/Recurrence Time Space Variables Properties Key Points
BFS Graph Explore Visit all nodes at O(V O(V) V=vertices, E=edges, ✗ Optimal substructure, - Uses queue for FIFO order, - Finds
Traversal graph level- distance k before + E) Q=queue, colors ✗ Overlapping shortest path in unweighted graphs, -
by-level visiting nodes at (White/Gray/Black), Colors: White=undiscovered,
subproblems, ✗
using queue distance k+1 d[u]=distance, Gray=discovered but not fully explored,
Greedy-choice property
(FIFO) π[u]=parent Black=fully explored
Problem Definition
Algorithm Approach Logic Formula/Recurrence Time Space Variables Properties Key Points
DFS Graph Go as deep as Recursively visit O(V + O(V) V=vertices, E=edges, ✗ Optimal - Explores structure deeply, - Detects
Traversal possible down neighbors, backtrack E) colors substructure, ✗ cycles, - Used for topological sorting,
one path, then when no unvisited (White/Gray/Black), Overlapping finding strongly connected components, -
backtrack neighbors remain d[u]=discovery time, Time increases every time you discover or
subproblems, ✗
f[u]=finish time, finish a vertex
Greedy-choice
π[u]=parent
property
Problem Definition
Algorithm Approach Logic Formula/Recurrence Time Space Variables Properties Key Points
Kosaraju's Graph 1) Run DFS on G, record Each DFS tree in step O(V + O(V) G=original graph, ✗ Optimal - 3-step process: DFS → Transpose
SCC Algorithm finish times 2) Transpose G 3 is one SCC E) G^T=transpose substructure, ✗ → DFS on transpose, - Transposing
3) Run DFS on G^T in graph, f[u]=finish Overlapping doesn't change SCC structure, - Each
decreasing order of finish time, SCC subproblems, ✗ DFS tree in final step = one SCC, -
times components Works because of mathematical
Greedy-choice
properties of SCCs
property
Problem Definition
Algorithm Steps:
Variable Definitions:
Algorithm Approach Logic Formula/Recurrence Time Space Variables Properties Key Points
1. Dynamic Relax all edges RELAX(u,v,w): if O(|V| × |E|) O(|V|) G=(V,E), ✓ Optimal - Handles negative weights, -
Bellman- Programming |V|-1 times, then d[u] + w(u,v) < d[v] w=weight substructure, ✓ Detects negative cycles, - |V|-1
Ford check for then d[v] = d[u] + function, Overlapping iterations find all shortest paths, -
negative cycles w(u,v), π[v] = u d[v]=shortest 3rd loop checks for negative cycles
subproblems, ✗
distance,
Greedy-choice
π[v]=predecessor,
property
s=source
2. DAG- Topological Process vertices Same RELAX O(|V| + |E|) O(|V|) Same as above + ✓ Optimal - Only works on DAGs (no cycles),
SSSP Sort + in topological operation but on topological substructure, ✓ - Linear time solution, - Most
Relaxation order, relax topologically sorted ordering Overlapping optimal O(|V| + |E|), - Handles
outgoing edges vertices negative weights
subproblems, ✗
Greedy-choice
property
3. Greedy Always pick Same RELAX + O(|E| + O(|V|) Same as above + ✗ Optimal - Only non-negative weights, -
Dijkstra closest unvisited priority queue for |V|log|V|) with priority queue Q, substructure, ✗ Greedy approach (always pick
vertex, relax its minimum distance Fibonacci heap, S=finalized Overlapping closest), - Most common for
neighbors O(|E|log|V|) vertices practical use, - Fast performance
subproblems, ✓
with binary
Greedy-choice
heap
property
Algorithm Time Complexity Space Negative Weights Negative Cycles Best Use Case
Bellman-Ford O(|V| × |E|) O(|V|) ✓ Detects Graph has negative edge weights
DAG-SSSP O(|V| + |E|) O(|V|) ✓ N/A (no cycles) Directed Acyclic Graph
Dijkstra O(|E|log|V|) O(|V|) ✗ N/A All edge weights non-negative
Problem Definition
Algorithm Details:
Bellman-Ford:
• Why |V|-1 iterations: Any shortest path has at most |V|-1 edges (simple path)
• After k iterations: Find shortest paths using at most k edges
• Third loop purpose: If any edge can still be relaxed after |V|-1 iterations, negative cycle exists
• Negative cycle detection: What makes Bellman-Ford special
DAG-SSSP:
Dijkstra:
Key Insights:
1. Greedy vs. Dynamic Programming: Dijkstra uses greedy (works due to non-negative weights), Bellman-Ford
uses dynamic programming approach
2. Why Dijkstra fails with negative weights: Greedy choice might not be optimal, once a vertex is in S, its distance
is considered final
3. Topological ordering power: DAG-SSSP achieves linear time by processing vertices in right order
Problem Definition:
1. Greedy Choice Property: Locally optimal choice leads to globally optimal solution
2. Optimal Substructure: Must also have optimal substructure
PageRank Algorithm
Algorithm Approach Logic Formula/Recurrence Time Space Variables Properties Key Points
1. Basic Iterative Each page PR(A) = (1-d)/N + d × O(k×n²) O(n²) PR(A)=PageRank ✗ Optimal - Models web as directed graph, -
PageRank (Power shares its Σ(PR(T)/C(T)) for all T where of page A, substructure, ✗ Iterative until convergence, - Handles
Method) importance linking to A k=iterations d=damping factor Overlapping "random surfer" behavior
equally (0.85), N=total
subproblems, ✗
among pages pages,
Greedy-choice
it links to C(T)=outgoing
property
links from T
2. Google Linear Solve Ax = x M = (1-d)/N × J + d × O(n³) direct O(n²) M=Google matrix, ✗ Optimal - Eigenvector with eigenvalue 1, -
Matrix Algebra where A is A, where PageRank solution, A=transition substructure, ✗ Perron-Frobenius theorem applies, -
Form (Eigenvalue) Google vector v* is dominant O(k×n²) matrix, J=matrix Overlapping Power iteration converges to dominant
Matrix eigenvector iterative of all 1s, eigenvector
subproblems, ✗
v*=PageRank
Greedy-choice
vector
property
Problem Definition:
Concept Approac Logic Formula/Rec Time Space Variables Properties Key Points
h urrence
1. Eulerian Graph Check if every Check degree O(V + E) O(V) G=(V,E), ✓ Polynomial verifiable, - Visit every EDGE exactly once, -
Cycle Traversal vertex has even of each vertex: degree(v)=number ✓ Polynomial solvable All vertices must have even degree,
Detection degree and graph is degree(v) % 2 of edges incident to - Graph must be connected
connected == 0 for all v v
2. Backtrack Try to visit every No simple O(n!) brute O(n²) for G=(V,E), path ✓ Polynomial verifiable, - Visit every NODE exactly once, -
Hamiltonia ing/DP vertex exactly once formula - NP- force, DP visiting each vertex ✗ No known polynomial NP-complete problem, - No
n Cycle and return to start complete O(n²2ⁿ) DP once solution efficient algorithm known
Detection problem
Eulerian Cycle:
• Definition: Path that visits every edge exactly once and returns to starting vertex
• Condition: All vertices must have even degree AND graph must be connected
• Certificate: Sequence of edges using each edge once
• Verifier: Check edges used exactly once and path is valid
• Examples: Seven Bridges of Königsberg (no solution - odd degrees)
Hamiltonian Cycle:
• Definition: Path that visits every vertex exactly once and returns to starting vertex
• Condition: No simple necessary/sufficient condition known
• Certificate: Sequence of vertices visiting each vertex once
• Verifier: Check all vertices visited exactly once and edges exist
• Examples: Traveling Salesman Problem (find shortest Hamiltonian cycle)
Complexity Classes
• SAT (Boolean Satisfiability): Given boolean formula, does satisfying assignment exist?
• 3-SAT: SAT with clauses of exactly 3 literals
• Clique: Does graph contain complete subgraph of size k?
• Hamiltonian Cycle: Does graph have cycle visiting each vertex once?
• Traveling Salesman: Find shortest tour visiting all cities once