A Field Guide for Young Explorers

The Algorithm
Field Guide.

Nine of the most common algorithms - now in your hands. Run each one yourself: press play, watch it think, and master every step one decision at a time. What you see in action, you never forget - start exploring now!

fig. 0 - a specimen sorts itself, on a loop
Scroll

Specimens in this guide

09 CATALOGUED Cheat sheet โ†’
Specimen 01 ยท O(nยฒ) sorting

Bubble SortOrdinatio Vicina - "ordering by neighbors"

Walk down the row. Compare two neighbors. If the left one is bigger, swap them. By the end of one pass, the largest value has "bubbled" to the far end. Repeat until nothing needs swapping.

Learn more โ†’
Press Play or Step to begin the observation.
bubble_sort.py
1def bubble_sort(arr):
2 n = len(arr)
3 for i in range(n - 1):
4 for j in range(n - 1 - i):
5 if arr[j] > arr[j+1]:
6 arr[j], arr[j+1] = \
arr[j+1], arr[j]
7 return arr
Comparisons
0
Swaps
0
Pass
0
Status
idle
Practice on LeetCode
Specimen 02 ยท O(log n) searching

Binary SearchQuaerens Dimidium - "seeking by halves"

On a row that's already sorted, look at the middle value. Too small? The answer must be to the right - discard the left half. Too big? Discard the right half. Repeat: the search space halves every step.

Learn more โ†’
Choose a target value below, then press Play or Step.
binary_search.py
1def binary_search(arr, target):
2 lo, hi = 0, len(arr) - 1
3 while lo <= hi:
4 mid = (lo + hi) // 2
5 if arr[mid] == target:
6 return mid
7 elif arr[mid] < target:
8 lo = mid + 1
9 else:
10 hi = mid - 1
11 return -1
Low
โ€“
Mid
โ€“
High
โ€“
Steps taken
0
Specimen 03 ยท O(V + E) traversal

Breadth-First SearchUndae Explorans - "exploring in waves"

Start at one node. Visit every direct neighbor first - that's ring one. Then every neighbor-of-a-neighbor - ring two. A queue keeps the order fair: first discovered, first explored.

Learn more โ†’
Pick a starting node, then press Play or Step.
bfs.py
1def bfs(graph, start):
2 visited = {start}
3 queue = deque([start])
4 while queue:
5 node = queue.popleft()
6 for neighbor in graph[node]:
7 if neighbor not in visited:
8 visited.add(neighbor)
9 queue.append(neighbor)
10 return visited
Queue
โ€“
Visiting
โ€“
Visited count
0
Specimen 04 ยท O(n) on sorted input

Two PointersDuo Indices - "the pair that closes in"

On a sorted array, place one pointer at each end. If the pair sums too small, the only fix is to move the left pointer up. Too big, move the right pointer down. Every LeetCode problem titled "pair sum" or "container with most water" is this pattern.

Learn more โ†’
Pick a target sum below, then press Play or Step.
two_sum_sorted.py
1def two_sum_sorted(arr, target):
2 left, right = 0, len(arr) - 1
3 while left < right:
4 s = arr[left] + arr[right]
5 if s == target:
6 return [left, right]
7 elif s < target:
8 left += 1
9 else:
10 right -= 1
11 return []
Left
โ€“
Right
โ€“
Current sum
โ€“
Steps taken
0
Specimen 05 ยท O(n) amortized

Sliding WindowFenestra Mobilis - "the moving frame"

Instead of recomputing a sum for every possible window, keep a running total: drop the value that just left the frame, add the value that just entered. One frame, sliding - not thousands of frames, recomputed.

Learn more โ†’
Press Play or Step to slide the window.
max_sum_subarray.py
1def max_sum_subarray(arr, k):
2 window_sum = sum(arr[:k])
3 max_sum = window_sum
4 for i in range(k, len(arr)):
5 window_sum += arr[i] - arr[i - k]
6 max_sum = max(max_sum, window_sum)
7 return max_sum
Window
โ€“
Window sum
โ€“
Max sum so far
โ€“
Specimen 06 ยท O(n) time, O(1) space

Fast & Slow PointersTestudo et Lepus - "the tortoise and the hare"

Two pointers walk the same linked list - one node at a time, and two nodes at a time. If the list loops back on itself, the fast pointer eventually laps the slow one and they land on the same node. If the list ends, the fast pointer simply runs off the end.

Learn more โ†’
Press Play or Step to send the pointers walking.
has_cycle.py
1def has_cycle(head):
2 slow, fast = head, head
3 while fast and fast.next:
4 slow = slow.next
5 fast = fast.next.next
6 if slow is fast:
7 return True
8 return False
Slow at
โ€“
Fast at
โ€“
Result
running
Specimen 07 ยท O(n) time, O(n) space

Prefix Sum + Hash MapSumma Praeviae - "the sum carried forward"

Keep a running total as you scan. At each position, ask: "have I seen the running total minus k before?" If so, everything between that earlier position and here sums to exactly k. A hash map turns that question into an O(1) lookup.

Learn more โ†’
Press Play or Step to scan the array.
subarray_sum.py
1def subarray_sum(arr, k):
2 count, total = 0, 0
3 seen = {0: 1}
4 for num in arr:
5 total += num
6 need = total - k
7 count += seen.get(need, 0)
8 seen[total] = seen.get(total, 0) + 1
9 return count
Running total
โ€“
Need (total โˆ’ k)
โ€“
Matches found
0
Specimen 08 ยท O(n) time, O(n) space

1D Dynamic ProgrammingTabula Memoriae - "the table that remembers"

Climbing n stairs, one or two steps at a time - how many distinct ways? Rather than re-deriving the answer for every n from scratch, build a table left to right: the answer for n is just the answer for nโˆ’1 plus the answer for nโˆ’2, already sitting in the table.

Learn more โ†’
Press Play or Step to fill the table.
climb_stairs.py
1def climb_stairs(n):
2 if n <= 2:
3 return n
4 dp = [0] * (n + 1)
5 dp[1], dp[2] = 1, 2
6 for i in range(3, n + 1):
7 dp[i] = dp[i-1] + dp[i-2]
8 return dp[n]
Filling dp[i]
โ€“
dp[i-1] + dp[i-2]
โ€“
Ways to climb n=8
โ€“
Specimen 09 ยท O(V + E) traversal

Depth-First SearchAlte Explorans - "exploring the depths"

Commit to one path and follow it as far as it goes. Only when you hit a dead end do you back up to the last fork and try the next branch. A stack remembers where to return - the exact opposite discipline to BFS's queue.

Learn more โ†’
Pick a starting node, then press Play or Step.
dfs.py
1def dfs(graph, start):
2 visited = set()
3 stack = [start]
4 while stack:
5 node = stack.pop()
6 if node in visited:
7 continue
8 visited.add(node)
9 for nb in reversed(graph[node]):
10 if nb not in visited:
11 stack.append(nb)
12 return visited
Stack
โ€“
Visiting
โ€“
Visited count
0

Field notes - specimen comparison

Specimen Best case Worst case Space Requires
Bubble Sort O(n) O(nยฒ) O(1) nothing - works on any order
Binary Search O(1) O(log n) O(1) a sorted array
Breadth-First Search O(V+E) O(V+E) O(V) an adjacency list & a queue
Two Pointers O(n) O(n) O(1) a sorted array
Sliding Window O(n) O(n) O(1) a contiguous window, fixed or growable
Fast & Slow Pointers O(n) O(n) O(1) a linked structure to walk
Prefix Sum + Hash Map O(n) O(n) O(n) a hash map of running totals
1D Dynamic Programming O(n) O(n) O(n) overlapping subproblems
Depth-First Search O(V+E) O(V+E) O(V) an adjacency list & a stack (or recursion)

Go deeper - where to learn next

This guide is a first look. When you're ready to go further, these are the resources worth your time - all free to start, none of them filler.

Visualize more

Practice problems

Read deeper

ยฉ 2026 Davronbek