Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Python is the book’s main explanation and tracing language. Every existing Python listing has a parallel JavaScript and Julia tab. Pages that explain a concept without code do not add empty panels. Choose the language you want to read; clicking or tapping a language in one panel also selects it in the other panels on that page. The tabs display code rather than execute it.

Figures, exact trace indices and self-check answers retain their Python conventions. The other implementations preserve the algorithm or demonstrate the corresponding language feature; they do not make Python-specific behaviour universal.

Shared ideas, native conventions

ConcernPythonJavaScriptJulia
Sequence positionsStart at 0Start at 0Ordinary vectors start at 1
Explicit absent resultNonenullnothing
Teaching mutator with no resultReturns NoneReturns undefined implicitlyExplicitly returns nothing
Public operationsUsually instance methodsUsually instance methodsFunctions dispatched on the container; ! marks mutation where used
Multiple resultsTupleArrayTuple
Value versus identity== versus isPrimitive equality versus reference identity; array contents need explicit comparison== versus ===
Unordered map/set iterationFollow the locally stated guaranteesMap and Set preserve insertion orderDict and Set order is not promised to match Python
Sorting APIssorted copies; list.sort mutates and returns NoneCopy first when needed; Array.sort returns the mutated arraysort copies; sort! mutates and returns the array

Read the actual function names in the selected listing. For example, Julia queue operations are enqueue!(queue, item), queue_front(queue), and dequeue!(queue); public counts use length and isempty. A successful Julia boundary-search result can be length(a)+1, just as the zero-based versions can return len(a). Union–find members and quadrant/coordinate indices follow the native index base too.

The brief built-in API examples are comparisons, not claims that JavaScript or Julia uses Python’s Timsort/Powersort implementation. The supplied teaching sort functions still implement the named algorithm explicitly.

Comparison and arithmetic assumptions

Parallel numeric examples use finite values whose ordering and equality agree across the languages. JavaScript Number integers and all intermediate exact integer operations must stay within their safe representable range. Julia Int is bounded; Python’s arbitrary-size integer behaviour should not be assumed for it. Use appropriate big-integer arithmetic when the problem requires it. Rolling fingerprints and the integer R-tree orientation predicate show that choice explicitly.

JavaScript Map/Set compares array/object keys by identity. The spatial code therefore compares two coordinates explicitly and uses a canonical coordinate key for duplicate tracking; ordinary map examples use primitive keys. The teaching hash helpers use h(k)=k for integer traces and make their alternative string hashing explicit. Do not expect a native runtime’s physical hash table to reproduce a diagram’s slot layout.

Strings need a position model

The string algorithms match Unicode code points. JavaScript and Julia convert text and pattern to character arrays before indexed scanning. This adds O(n+m) preparation time and storage even where the Python scan itself needs constant working space. Trie prefix helpers that collect text also pay for that buffer. The extra preparation is part of these implementations’ full costs.

JavaScript match outputs are zero-based code-point positions, not necessarily offsets accepted by native UTF-16 string slicing. Julia match outputs are one-based positions in the collected character vector, not byte indices into a UTF-8 String. A prefix-function table stores prefix lengths, so its values do not shift with the index base. Normalisation and case transformation remain explicit modelling decisions.

Run code with its prerequisites

Each code-bearing page links complete runnable Python, JavaScript and Julia topic files. Each file includes the earlier definitions it needs and runs without third-party packages. Use Python 3 for a .py file, Node for a .js file and Julia for a .jl file. The Python files use the standard library, including collections.deque and heapq where needed. The JavaScript and Julia files embed the JavaScript support or Julia support helpers. The source listings keep helpers separate so their role remains visible.

For example, download sorting.py and run python sorting.py. To explore its functions interactively after loading the file, run python -i sorting.py. Some topic files define functions without printing results. The transport topic file includes its prerequisite algorithms; no separate transport_network.py download is needed.

The JavaScript/Julia shared support contains a linked deque and a binary min-heap. Deque endpoints update a fixed number of links; heap insertion/removal repair a logarithmic path. Neither implementation repeatedly shifts an array’s front or sorts all priorities. Payload allocation, numeric operations and dictionary/set assumptions still need the cost qualifications on the relevant reading.

Topic downloads collect independent examples as well as definitions. A repeated name can start a fresh example; do not carry a changed dataset into a later figure unless its page says to do so. JavaScript’s generated topic files use var for repeated top-level demonstration bindings so the collected file can run; algorithm-local declarations remain unchanged. Julia examples use local scopes where a top-level loop would otherwise make assignment ambiguous.

JavaScript and Julia are checked against shared behavioural fixtures where contracts agree: empty/singleton boundaries, duplicates, rejected requests, mutation/ownership, traversal and query results. Valid tie outcomes can differ with map iteration; the tests compare the promised result rather than inventing a stronger tie rule. Python-only conceptual explanations remain labelled, especially object-model and mutable-default discussions.

The original standalone spatial and transport Python scripts remain available. The three-language binary-search example is a short starting point.

Complete topic downloads

TopicPythonJavaScriptJulia
1. Foundations and Python’s object modelPythonJavaScriptJulia
2. Analysing algorithmsPythonJavaScriptJulia
3. Recursion and divide-and-conquerPythonJavaScriptJulia
4. SortingPythonJavaScriptJulia
5. Sequences, stacks, queues and dequesPythonJavaScriptJulia
6. Maps, sets and hashingPythonJavaScriptJulia
7. Searching and binary search treesPythonJavaScriptJulia
8. Balanced search treesPythonJavaScriptJulia
9. Spatial search and indexingPythonJavaScriptJulia
10. Priority queues and heapsPythonJavaScriptJulia
11. Graphs and traversalPythonJavaScriptJulia
12. Union–find and disjoint setsPythonJavaScriptJulia
13. Graph ordering, paths and spanning treesPythonJavaScriptJulia
14. Algorithmic problem-solving strategiesPythonJavaScriptJulia
15. Strings, tries and pattern matchingPythonJavaScriptJulia
16. Integrated applications: a transport networkPythonJavaScriptJulia