> For the complete documentation index, see [llms.txt](https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/34.-radix-sorts/34.3-msd-radix-sort.md).

# 34.3 MSD Radix Sort

Basic idea: Just like LSD, but sort from leftmost digit towards the right.

<details>

<summary>Suppose we sort by topmost digit, then middle digit, then rightmost digit. Will we arrive at the correct result?</summary>

No, this will result in sorting the array by right most digit only as we are not saving the topmost and middle digit.

</details>

<figure><img src="https://2512475561-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBGDh1mPw1Aw5u4IlOFCn%2Fuploads%2Fgit-blob-0a3ad64904b059f25fd26816d05e098e851fab9d%2FScreen%20Shot%202023-04-15%20at%202.55.34%20AM.png?alt=media" alt=""><figcaption><p>MSD Radix Sort (correct edition)</p></figcaption></figure>

Notice first we sorted by leftmost digit. Then we grouped the data by the leftmost digit, so one group would start with a's, then the next group would start with b's, and so on and so forth. Then within our subgroups we would order by middle digit, and create newer subgroups. And finally we would break this up into further subgroups until we have all individual subproblems. This final result would be sorted.

### Runtimes

Best Case.

* We finish in one counting sort pass, looking only at the top digit: $$\Theta(N+R)$$

Worst Case.

* We have to look at every character, degenerating to LSD sort: $$\Theta(WN+WR)$$

### Summary of Runtimes

<figure><img src="https://2512475561-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBGDh1mPw1Aw5u4IlOFCn%2Fuploads%2Fgit-blob-10cc7642e69dc86b42365bade2f9300251e15c57%2FScreen%20Shot%202023-04-15%20at%202.58.27%20AM.png?alt=media" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/34.-radix-sorts/34.3-msd-radix-sort.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
