liveBook · Manning

Merge sort

As explained in Clojure: The Essential Reference

Merge sort is a comparison-sorting algorithm that recursively divides input into smaller portions and merges ordered portions back together.

Merge sort addresses sorting problems where the full dataset should not remain in memory. It divides input into smaller chunks, sorts those chunks, and merges their ordered results into one sequence. section 8.3.5

Sorting a large range

The expression `(take 10 (psort (IdRange. 0 10000)))` produces `(0 1 2 3 4 5 6 7 8 9)`, while the implementation keeps only a bounded number of chunks actively loaded.

How Clojure: The Essential Reference covers it

How deep it goes
Explains chunk splitting, parallel processing, disk storage, and lazy merging, stopping short of lower-level algorithm analysis.
Examples
Uses IdRange, temporary files, parallel chunk processing, and a psort call returning the first ten values.
What it assumes
Assumes familiarity with sort and sort-by while demonstrating reducers, protocols, records, temporary files, and lazy sequences.
What you can do after
Build it from scratch with reducers, temporary files, chunk sorting, and a lazy multiway merge.
How it connects
Connects divide and conquer with Clojure reducers, lazy sequences, sort, and sort-by.

Also explained in

Compare how 2 books explain merge sort →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.