Euclid’s Algorithm
function gcd(a, b)
while b ≠ 0
t := b
b := a mod b
a := t
return a
Consecutive integer checking algorithm
1. Assign the value of min{m,n} to t
2. Divide m by t. If the remainder is 0, go
to S3;otherwise, go to S4
3. Divide n by t. If the remainder is 0,
return t and stop; otherwise, go to S4
4. Decrease t by 1 and go to S2
Middle-school procedure
1. Find the prime factorization of m
2. Find the prime factorization of n
3. Find all the common prime factors
4. Compute the product of all the
common prime factors and
return it as gcd(m,n)
Time complexity: O(sqrt(n))
Sieve of Eratosthenes [Prime]
Input: Integer n ≥ 2
for p ← 2 to n do A[p] ← p
for p ← 2 to root(n) do
o if A[p] !=0 //p hasn’t been
previously eliminated from the
list
o j ← p*p
o while j<=n do
A[j] ← 0 //mark element as
eliminated
o j ← j+ p
copy array of A to L of primes
String Matching -> Θ(mn)
String matching Text , Pattern
For i=0 to n-m
J=0
While j<m and P[j]=T[i+j]
J++
If j=m return i
Return -1
u
1 u l 1
i l
n2
n 1 i (n 1) (n 2) ..... 1
i 0
( n 1) n 1 2
n ( n 2 )
2
2
Int sqrt_floored(n)
// input n , number of digits in n
Digits=size of n
Xprevious=0 Xcurrent =
Digits*10^(Digits/2)
While (Xcurrent!=Xprevious)
Xprevious=Xcurrent
Xcurrent =0.5*(Xprevious+n/Xprevious)
Return Xcurrent
Tower of Hanoi [2M(n-1)+1]
Selection sort Θ(n^2), Stable
for i =0 to n –2 do
min =i
for j =i + 1 to n –1 do
if A[j] < A[min]
min =j
swap A[i] and A[min]
Space eff.= Θ(1) so Inplace
Binary search -> O(log n)
Input: sorted array a_i < … < a_j
and key x;
m =(i+j)/2;
while i < j and x != a_m do
if x < a_m then j =m-1
else i =m+1;
if x = a_m then output a_m;
Bubble Search -> Θ(n^2)
for i ← 0 to n-2 do
for j ← 0 to n-2-i do
if A[j]<A[j+1]
swap A[j] and A[j+1]
Graphs
• A graph G = <V, E> is defined by a pair of
two sets: a finite set V of items called
vertices and a set E of vertex pairs called
edges.
(digraphs).
an undirected graph with |V| vertices?
• A graph with every pair of its vertices
connected by an edge is called complete,
K|V|
• In Dense graph, the number of edges is
close to the maximal number of edges.
Exhaustive Search-> Θ((n-1)!)
Knapsack by Exhaustive Search Θ(2^n)
Assignment by Exhaustive SearchΘ(n!)
The Hungarian method runs in O(n^3)
Divide-and-Conquer Recurrence
T(n) = aT(n/b) + f (n) where f(n)
(nd), d 0
Master Theorem:
If a < bd, T(n) (nd)
If a = bd, T(n) (nd log n)
If a > bd, T(n) (nlog b a )
Convex hull brute force
for i = 1 to n do
convex[i] = false
end
for i = 1 to n1
do // consider the line Pi_Pj
for j = i+1 to n do
a = yj – yi // find the equation of the
b = xi – xj // line Pi_Pj
c = xiyj – yixj
ontheline = 0 // count of points for
three
oneside = 0 // groups relative to the
anotherside = 0 // line segment
Pi_Pj
for k = 1 to n do // check the
remaining
if k ≠ i and k ≠ j // n2
case axk + byk – c
0 : ontheline = ontheline + 1
+ : oneside = oneside + 1
– : anotherside = anotherside + 1
end //case
end //if
end //for k
If n – 2 – ontheline = oneside or
n – 2 – ontheline = anotherside
convex[i] = true
convex[j] = true
end
end //for j
end //for i
return convex
end ConvexHull
Graph Representation
Adjacency matrix
• n x n boolean matrix if |V| is n.
• The element on the ith row and jth
column is 1 if there’s an edge from ith
vertex to the jth vertex; otherwise 0.
• The adjacency matrix of an undirected
graph is symmetric.
Adjacency linked lists
• A collection of linked lists, one for each
vertex, that contain all the vertices
adjacent to the list’s vertex.
Tower of Hanoi
M(n) = 2M(n-1) + 1 or 2 n - 1
Fibonacci Sequence
*long Recursive_Fibonacci(int n){
if (n==0)
return 0;
else if(n==1)
return 1;
else { long sum=0; basic_operation++;
sum = Recursive_Fibonacci(n-1) +
Recursive_Fibonacci(n-2); return sum; }}
----------------------------*long Non_Recursive_Fibonacci(int n){
long term_2=0; long term_1=1;
if (n==0)
return term_2;
else if(n==1) return term_1;
else{ long sum=0;
for (int i=2;i<=n;i++){
basic_operation++;
sum=term_1+term_2;
term_2=term_1;
term_1=sum; }return sum;}}
Partitioning Algorithm
Time = Θ(r-l)
Best case running time: O(n log2n)
Worst case running time: O(n2)
Insertion Sort
for i = 2:n,
for (k = i; k > 1 and a[k] < a[k-1]; k--)
swap a[k,k-1]
end
Inplace,Stable,O(n2) comparisons &
swaps
Adaptive: O(n) time when nearly sorted
Polynomial Evaluation
p 0.0
for i n downto 0 do
power 1
for j 1 to i do //compute xi
power power x
p p + a[i] power
return p ---> Θ(n^2)
--------------------------p a[0]
power 1
for i 1 to n do
power power x
p p + a[i] power
return p ---> Θ(n)
Counting binary digits
Count=1;
While n>1 do
Count=count+1;
Return count;
Complexity=Log n
Closest-Pair ---> Θ(n^2)
dmin ∞
for i 1 to n-1 do
for j i+1 to n do
dsqrt ((xi-xj)^2+(yi-yj)^2)
if d<dmin
dmind;index1I;index2j;
return index1, index2
MergesortΘ -->
TimeΘ (p+q) = Θ(n) comp.
T(n) = 2T(n/2) + n-1 , If a = bd,
T(n) (nd log n) = (n log n)
© Copyright 2026 Paperzz