Brute Force O(n) Convex Hull O(𝒏𝟐 ) Lexicographic Order // Dictionary
Searching an unsorted collection. ConvexHull (P) Input: 𝑎! 𝑎! 𝑎! … 𝑎!
Loop till index if found. N <- P.length, K <- 0, H <- (2*N)
// lower hull Generate next lexicographic perm
BruteForceSearch (set A, item a) for i:=0 to n do Find largest 𝑎! such that 𝑎! < 𝑎! + 1
for i:=0 to n–1 do while (K>=2 && cross(H(K-2), Find 𝑎! such that 𝑎! > 𝑎! (largest j)
if(A[i]==a) H[K-1],P[i] <= 0)) Swap 𝑎! with 𝑎! and reverse the order
return i K--
𝑎! + 1 … 𝑎!
H[K++] = P[i]
Insertion Sort O(𝒏𝟐 ) // upper hull !!! EXAMPLE HERE !!!
for i:=n-2 to 0 and T = K+1 do //-i
Each loop, we find next element’s pos while (K>=T && cross(H(K-2),
Grey Code O(𝟐𝒏 )
in the left sorted array and insert H[K-1],P[i] <= 0))
it. K--
1 bit 2 bit 3 bit 3 bit Binary
H[K++] = P[i]
0 00 000 000 000
InsertionSort (A[0…n-1]) H.resize(K)
1 01 001 001 001
for i:=1 to n–1 do return H
11 011 011 010
v <- A[i], j <- i-1
10 010 010 011
while(j>=0 and A[j]>V) Subset Sum // Backtracking
110 110 100
A[j+1] <- A[j]
111 111 101
J <- j-1 Find a subset of A which sub is equal
101 101 110
A[j+1] <- V to a positive integer d.
100 100 111
Selection Sort O(𝒏𝟐 ) *note: Generic algo
*flip least significant 1 from Binary
Select smallest from each loop and Backtrack (A[1…i])
Recursive version is more exp in
place it at i. if A[1…i] is solution then
terms of space.
write A[1…i]
SelectionSort (A[0…n-1]) else
IterativeGC ()
for i:=0 to n–2 do for each element x ∈ 𝑆!!!
for i:=0 to 2 ! // set size
min <- i consistent with A[1…i] and the
for j:=i+1 to n-2 do constrain grey = 𝑖 ! (I >> 1)
if(A[j] < A[min]) A[i+1] <- x
min = j Backtrack(A[1…i+1]) OR
Swap(A[i], A[min])
Branch and Bound count <- 0
Initial Seq = 000
Bubble Sort O(𝒏𝟐 )
Intuition: No need to search deeper do
into the tree if we know that node in count++
For each loop, bubble the largest
subtree != soln Find pos of least sig 1 in count
element to its largest pos. (n-2-i)
Flip tt pos in seq
Job assignment (worker/job) problem: While (seq != 000)
BubbleSort (A[0…n-1])
for i:=0 to n–2 do Find unique assignment with minimum
cost. RecursiveGC(N)
for j:=0 to n-2–i do
if(N==1)
if A[j]>A[j+1] then
1. Find lower return {0, 1}
Swap A[j] with A[j+1] 1 2 3 4
bound for each L1 <- RecursiveGC(N-1)
A 3 8 5 4 L2 <- Reverse(L1)
Snail Sort O(n!) node, ignoring
B 8 7 5 3 Insert 0 infront of all elements L1
constrains
C 1 9 3 8 2. Only expand a Insert 1 infront of all elements L2
Randomly permutes all numbers till
D 2 8 7 6 node when “worth return L1 + L2
array is sorted.
expanding”
3. Stop when there is a soln better Integer Partitioning
SnailSort (set A)
done <- 0 than all other solns
Finding all ways to represent an
n <- length(A)
{}:9 // 3 + 3 + 1 + 2 integer as a sum of positive
while done==0 do
integers.
p <- ranPerm(n)
A = A(p) {a:1}:15 {a:2}:14 {a:3}:11 {a:4}:12 Eg. 3 = 2 + 1 = 1 + 1 + 1
if(IsSortet(A))
return A
IntPartitioning ()
done = 1 {b:1}:27 {b:2}:15 {b:4}:11 // terminate if not found
Find smallest part K > 1.
Convex Hull O(𝒏𝟐 ) // Brute Force Let K’ = K-1. Rewrite 1K as K’ + 1
{b:1}:21 {b:2}:14 {b:3}:12
Collect all 1s into parts !> K’
Compare all combination of points and
check the distance. Return smallest. {c:1}:17 {c:2}:19 {c:1}:18 {c:2}:20 Eg.
ClosestPair (P) Permutations and Combinations Eqn K K’ Eqn K K’
d <- ∞ 6 6 5 3+1+1+1 3 2
for i:=1 to n-1 do Johnson Trotter’s Algo 5+1 5 4 2+2+2 2 1
for j:=i+1 to n do 4+2 2 1 2+2+1+1 2 1
d <- min(d, dist(P[i], P[j])) Want: Given n items, generate all 3+3 3 2 2+1+1+1+1 2 1
return d possible permutations of n items. 3+2+1 2 1 1+1+1+1+1+1
Approach: Number items from 1 to n,
Convex Hull O(𝒏𝟑 ) // Brute Force Dumb deal with items via numbers.
Property: Generate perm s.t.
For each lowest loop, find if consecutive perms only differ by 1
combination of point form outer edge swap (minimal swap). 123 -> 312
of convex hull.
A number is mobile if its arrow is
ConvexHull (P, N) pointing towards a smaller number.
s <- 0
for i:=0 to n-2 do JohnsonTrotter ()
for j:=i+1 to n-1 do while ∃ mobile element
sameSide <- true find largest mobile element K
for k:=0 to n-1 do swap(K, element its pointing to)
sameSide <- sameSide && reverse all arrows larger than K
Test[P(k)] on the
same side as
other points 1 2 3 → 1 3 2 → 3 1 2 →
if(sameSide) 3 1 2 → 2 3 1 → 2 1 3
add P(i), P(j) to S
return S
Divide and Conquer Merge Sort O(nlog ! n)
Inversion Counting MergeSort(A[0…n-1])
if(n>1)
To find how close preferences are. ! !
copy A[0… -1] to B[0… -1]
! !
! !
X: BCDAE, Y: DBCEA copy B[ …n-1] to C[0… -1]
! !
Let X be “sorted”. !
BCDAE : 12345 12345 mergesort(B[0… -1])
!
DBCEA : 31254 31254 3 inversions. !
mergesort(C[0… -1])
!
merge(B,C,A)
Def: inversion is when a seq is
0,1,2,n. yet there exist i < j, but Merge(B[0…p-1], C[0…q-1], A[0…p+q-1])
𝑎! > 𝑎! . i <- 0, j <- 0, k <- 0
while i < p and j < q
SortNCount (L) if B[i] <= C[j]
if |L| == 1 A[k] <- B[i]
return count = 0 and L i <- i + 1
Split L into L1, L2 else
Do SortNCount(L2) n obtain SL1 and A[k] <- C[j]
count 1 j <- j + 1
Do SortNCount(L1) n obtain SL2 and k <- k + 1
count 2 if (i==p)
Call MergeNCount(L1, L2) n obtain copy C[j…q-1] to A[k…p+q-1]
SL3, count3 else
Return sorted list SL and (count1 + copy B[i…p-1] to A[k…p+q-1]
count2 + count3)
!
T(n) – 2( ) + T(n) for n > i, C(1)=0
MergeNCount(A,B) !
Let A = 𝑎! , 𝑎! , 𝑎! … 𝑎! Tworst = nlogn – n + 1 = O(nlog ! n)
Let B = 𝑏! , 𝑏! , 𝑏! … 𝑏!
minSize = min(n,m) Quick Sort O(log n)
maintain output list 0 n current
index j = 0 QuickSort(A[l…r])
pt Ap = 0 for A, Bp = 0 for B if(l>r)
while (Ap!=M || Ap!=N) S <- Partition(A[l…r])
if Ap!=N && Bp!=M QuickSort(A[l…S-1])
if Aap < Abp then QuickSort(A[S+1…r])
Oj = Aap
Ap++ Partition(A[l…r])
Else P <- A[l], i <- l, j <- r + 1
Oj = Bbp repeat
++Bp repeat
++count i <- i + 1
j++ until A[i] >= P
while (Ap!=N) repeat
j <- j – 1
Oj = Aap
until A[j] <= P
j++, Ap++
while (Bp!=M) until i >= j
Oj = Bbp // undo last swap when i >= j
j++, Bp++ Swap(A[i], A[j])
return 0 as output Swap(A[i], A[j])
return j
Closest Pair
Tbest(n) = nlog ! n
ClosestPair(P,Q) Tworst(n) = 𝜃(𝑛! )
if(|P| <= 3) Tavg(n) = 2nlnn ≈ 1.39 nlog ! n
return brute force minimal dist
else Bubble Sort O(𝒏𝟐 )
!
copy first of P to Pl
! For each loop, bubble the largest
!
copy same of Q to Ql element to its largest pos. (n-2-i)
!
!
copy remaining from P to Pr
! BubbleSort (A[0…n-1])
!
copy same from Q to Qr for i:=0 to n–2 do
!
d1 <- ClosestPair(Pl,Ql) for j:=0 to n-2–i do
d2 <- ClosestPair(Pr,Qr) if A[j]>A[j+1] then
! Swap A[j] with A[j+1]
d <- min(d1, d2), m <= P[ − 1].x
!
copy all pts of Q for which
|x-m|<d into arr S[0…num-1]
dminsq <= 𝑑 !
for i:=0 to num-2 fo
k <- i + j
while K<=num-1 and
(S[K]. y – S[i]. y)! < dminsq
dminsq <- min(dminsq,
dist(S[K], S[i]))
K <- K + 1
Return sqrt(dminsq)
!
T(n) = 2T( )+ f(n)
!
f(n) ∈ 𝜃(𝑛)
T(n) ∈ 𝜃 (𝑛𝑙𝑜𝑔𝑛)