Examples
Code Examples
Real algorithms written in Pseudo - from sorting to graph traversal.
Bubble Sort
bubble_sort.pseudo
func bubbleSort(arr)
for i from 0 to len(arr)
for j from 0 to len(arr) - i - 1
if arr[j] > arr[j+1]
swap arr[j] and arr[j+1]
return arr
bubbleSort([5, 2, 9, 1, 7, 3])Output
[1, 2, 3, 5, 7, 9]
Binary Search
binary_search.pseudo
func binarySearch(arr, target)
left = 0
right = len(arr) - 1
while left <= right
mid = (left + right) // 2
if arr[mid] == target
return mid
else if arr[mid] < target
left = mid + 1
else
right = mid - 1
return -1
arr = [1, 3, 5, 7, 9, 11, 13]
binarySearch(arr, 7)Output
3
Result 3 is the index of 7 in the array.
Fibonacci with Memoization
fib_memo.pseudo
map = HashMap()
recursive fib(n)
if n <= 1: return n
if map.has(n): return map.get(n)
result = fib(n-1) + fib(n-2)
map.put(n, result)
return result
fib(30)Output
832040
Balanced Brackets (Stack)
balanced.pseudo
func isBalanced(s)
stack = Stack()
for each ch in s
if ch == "(" or ch == "[" or ch == "{"
push(stack, ch)
else if ch == ")" or ch == "]" or ch == "}"
if isEmpty(stack): return false
top = pop(stack)
if ch == ")" and top != "(": return false
if ch == "]" and top != "[": return false
if ch == "}" and top != "{": return false
return isEmpty(stack)
isBalanced("({[]})")Output
true
BFS - Breadth-First Search
bfs.pseudo
func bfs(graph, start)
visited = Set()
q = Queue()
enqueue(q, start)
visited.add(start)
order = []
while not isEmpty(q)
node = dequeue(q)
append(order, node)
for each neighbor in graph.neighbors(node)
if not visited.has(neighbor)
visited.add(neighbor)
enqueue(q, neighbor)
return order
g = Graph()
g.addEdge(1, 2)
g.addEdge(1, 3)
g.addEdge(2, 4)
g.addEdge(3, 4)
bfs(g, 1)Output
[1, 2, 3, 4]
Two Sum (HashMap)
two_sum.pseudo
func twoSum(nums, target)
seen = HashMap()
for i from 0 to len(nums)
complement = target - nums[i]
if seen.has(complement)
return [seen.get(complement), i]
seen.put(nums[i], i)
return []
twoSum([2, 7, 11, 15], 9)Output
[0, 1]
Indices 0 and 1 (2 + 7 = 9) - O(n) solution using a HashMap.
Binary Tree - In-order Traversal
inorder.pseudo
func inorder(node)
if node == null: return
inorder(node.left)
print node.val
inorder(node.right)
root = TreeNode(4)
root.left = TreeNode(2)
root.right = TreeNode(6)
root.left.left = TreeNode(1)
root.left.right = TreeNode(3)
root.right.left = TreeNode(5)
root.right.right = TreeNode(7)
inorder(root)Output
1
2
3
4
5
6
7
Merge Sort
merge_sort.pseudo
func merge(left, right)
result = []
i = 0
j = 0
while i < len(left) and j < len(right)
if left[i] <= right[j]
append(result, left[i])
i = i + 1
else
append(result, right[j])
j = j + 1
while i < len(left)
append(result, left[i])
i = i + 1
while j < len(right)
append(result, right[j])
j = j + 1
return result
recursive mergeSort(arr)
if len(arr) <= 1: return arr
mid = len(arr) // 2
left = mergeSort(arr[0:mid])
right = mergeSort(arr[mid:len(arr)])
return merge(left, right)
mergeSort([8, 3, 1, 5, 2, 9, 4, 7])Output
[1, 2, 3, 4, 5, 7, 8, 9]
Run any example with --stepTry
pseudo run bubble_sort.pseudo --step to watch every swap happen interactively.Analyze complexityAdd
--analyze to see the Big-O analysis: pseudo run merge_sort.pseudo --analyze