Algorithms
(Algorithm Code)
Pramod Ganapathi
Department of Computer Science
State University of New York at Stony Brook
January 29, 2025
Contents
What is an algorithm code?
Assignments
Static arrays
Dynamic arrays
Conditionals
Loops
Function invocations
Singly linked lists
Circularly singly linked lists
Doubly linked lists
Stacks
Queues
Deques
Balanced search trees
Hash sets
Hash maps
Priority queues
What is an algorithm code?
An algorithm code is also called a pseudocode
An algorithm code represents an algorithm in a structured,
modular, step-by-step format
An algorithm code is used to show the actual working logic of an
algorithm without including unnecessary keywords and
information contained in a program code that is not relevant to
the problem-solving logic
An algorithm code is programming-language neutral, i.e.,
programmers can easily understand an algorithm code
irrespective of their favorite programming language
Assignments
AlgorithmCode()
// Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
bdc+dee
a ← 1; a ← √
n+log3 k
+ ab mod c // semicolon-separated statements
currentpathcost ← 0 // variable names have all small letters
Static arrays
AlgorithmCode()
// Static array: Creation and assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create an array A[1 . . . n]
A[i] ← a // set ith value of array. time:Θ (1)
a ← A[i] // get ith value of array. time:Θ (1)
Create an array B[1 . . . n] ← [0 . . . 0] // B[i] = 0 for all i ∈ [1, n]
Create an array C[1 . . . n] ← [1 . . . n] // C[i] = i for all i ∈ [1, n]
Create an array D[1 . . . n] ← B[1 . . . n] // D[i] = B[i] for all i ∈ [1, n]
a ← Sum(A[1 . . . n]) + Max(A[1 . . . n]) + Min(A[1 . . . n])
Dynamic arrays
AlgorithmCode()
// Dynamic array: Creation and assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a dynamic array optimalsolutionslist ← [ ]
Create a dynamic array A ← optimalsolutionslist
[Link](a) // add element at last. time:Θ (1)∗
A[i] ← a // set ith value of array. time:Θ (1)
a ← A[i] // get ith value of array. time:Θ (1)
a ← [Link]() // return the first element. time:Θ (1)
a ← [Link]() // return the last element. time:Θ (1)
2-D matrices
AlgorithmCode()
// 2-D matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a 2-D matrix M [1 . . . m][1 . . . n]
M [i][1 . . . n] ← [1 . . . n] // set ith row. time:Θ (n)
M [1 . . . m][j] ← [1 . . . m] // set jth col. time:Θ (m)
M [i][j] ← a // set (i, j)th value of matrix. time:Θ (1)
a ← M [i][j] // get (i, j)th value of matrix. time:Θ (1)
Conditionals
AlgorithmCode()
// Conditional: If-else ladder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
if a > 0 then
b←5
else if a = 0 then
b←0
c←0
else
b ← −5
Conditionals
AlgorithmCode()
// Conditional: Compact if-else ladder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
if a > 0 then b ← 5
else if a = 0 then { b ← 0; c ← 0 }
else b ← −5
Conditionals
AlgorithmCode()
// Conditional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
a←(b=c)?d : e // d if condition true, e if condition false
Loops
AlgorithmCode()
// Loop: i takes values a, a + 1, a + 2, so on such that i ≤ n . . . . . . . . . . . . . . .
for i ← a to n do
print A[i]
// Loop: i takes values n, n − 1, n − 2, so on such that i ≥ a . . . . . . . . . . . . . .
for i ← n downto a do
print A[i]
Loops
AlgorithmCode()
// Loop: i takes values a, a + k, a + 2k, so on such that i ≤ n . . . . . . . . . . . . .
for i ← a to n increment k do
print A[i]
// Loop: i takes values n, n − k, n − 2k, so on such that i ≥ a . . . . . . . . . . . . .
for i ← n downto a decrement k do
print A[i]
Loops
AlgorithmCode()
// Loop: i takes values n, n − k, n − 2k, so on such that i ≥ a . . . . . . . . . . . . .
i←n
while i ≥ a do
i←i−k
Function invocations
AlgorithmCode()
// Function invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
output ← AnotherAlgorithm(a, A[1 . . . n])
return output
AnotherAlgorithm(a, A[1 . . . n])
return A[a]
Singly linked lists
AlgorithmCode()
// Singly linked list (SLL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a SinglyLinkedList L
a ← [Link]() // return the first element. time:Θ (1)
a ← [Link]() // return the last element. time:Θ (1)
[Link](a) // add element at first. time:Θ (1)
[Link](a) // add element at last. time:Θ (1)
a ← [Link]() // remove element at first. time:Θ (1)
a ← [Link]() // remove element at last. time:Θ (n)
Circularly singly linked lists
AlgorithmCode()
// Circularly singly linked list (CSLL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a CircularlySinglyLinkedList L
a ← [Link]() // return the first element. time:Θ (1)
a ← [Link]() // return the last element. time:Θ (1)
[Link](a) // add element at first. time:Θ (1)
[Link](a) // add element at last. time:Θ (1)
a ← [Link]() // remove element at first. time:Θ (1)
a ← [Link]() // remove element at last. time:Θ (n)
[Link]() // move first element to last. time:Θ (1)
Doubly linked lists
AlgorithmCode()
// Doubly linked list (DLL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a DoublyLinkedList L
a ← [Link]() // return the first element. time:Θ (1)
a ← [Link]() // return the last element. time:Θ (1)
[Link](a) // add element at first. time:Θ (1)
[Link](a) // add element at last. time:Θ (1)
a ← [Link]() // remove element at first. time:Θ (1)
a ← [Link]() // remove element at last. time:Θ (1)
Stacks
AlgorithmCode()
// Stack (implemented using dynamic array or SLL) . . . . . . . . . . . . . . . . . . . . . . . .
Create a stack S
[Link](a) // add element at top. time:Θ (1)
a ← [Link]() // remove element at top. time:Θ (1)
a ← [Link]() // return element at top. time:Θ (1)
Queues
AlgorithmCode()
// Queue (implemented using a circular dynamic array or SLL) . . . . . . . . . . . . .
Create a queue Q
[Link](a) // add element at last. time:Θ (1)
a ← [Link]() // remove element at first. time:Θ (1)
a ← [Link]() // return element at top. time:Θ (1)
Deques
AlgorithmCode()
// Double-ended queue (deque) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a deque D
[Link](a) // add element at first. time:Θ (1)
[Link](a) // add element at last. time:Θ (1)
a ← [Link]() // remove element at first. time:Θ (1)
a ← [Link]() // remove element at last. time:Θ (1)
Balanced search trees
AlgorithmCode()
// Balanced search tree with size n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a BalancedSearchTree T
[Link](a) // add element. time:O (log n)
[Link](a) // remove element. time:O (log n)
a ← [Link](a) // check if element exists. time:O (log n)
[Link]() // inorder traversal. time:Θ (n)
[Link]() // levelorder traversal. time:Θ (n)
Hash sets
AlgorithmCode()
// Hash set (assuming perfect hash function) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a hash set H to elements in unordered/unsorted fashion
[Link](a) // add element. time:O (1)∗
[Link](a) // remove element. time:O (1)∗
a ← [Link](a) // check if element exists. time:O (1)∗
Hash maps
AlgorithmCode()
// Hash map (assuming perfect hash function) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a hash map H to store unordered/unsorted (key, value) pairs
a ← [Link](k) // return the value for key k. time:O (1)∗
a ← H[k] // return the value for key k. time:O (1)∗
[Link](hk, vi) // add a pair. time:O (1)∗
H[k] ← v // add a pair. time:O (1)∗
hk, vi ← [Link](k) // return the pair with key k. time:O (1)∗
hk, vi ← [Link](k) // search for pair with key k. time:O (1)∗
Priority queues
AlgorithmCode()
// Minimum-heap with size n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a min-heap H to store (key, value) pairs
[Link](hk, vi) // add a pair. time:O (log n)
hk, vi ← [Link]() // return pair with min key. time:Θ (1)
hk, vi ← [Link]() // remove pair with min key. time:O (log n)