mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-23 00:37:13 +00:00
deploy
This commit is contained in:
@@ -3584,14 +3584,14 @@
|
||||
|
||||
<!-- Page content -->
|
||||
<h1 id="1110-radix-sort">11.10 Radix sort<a class="headerlink" href="#1110-radix-sort" title="Permanent link">¶</a></h1>
|
||||
<p>The previous section introduced counting sort, which is suitable for scenarios where the data volume <span class="arithmatex">\(n\)</span> is large but the data range <span class="arithmatex">\(m\)</span> is small. Suppose we need to sort <span class="arithmatex">\(n = 10^6\)</span> student IDs, where each ID is an <span class="arithmatex">\(8\)</span>-digit number. This means the data range <span class="arithmatex">\(m = 10^8\)</span> is very large, requiring a significant amount of memory space for counting sort, while radix sort can avoid this situation.</p>
|
||||
<p><u>Radix sort</u> shares the core idea with counting sort, which also sorts by counting the frequency of elements. Building on this, radix sort utilizes the progressive relationship between the digits of numbers, sorting each digit in turn to achieve the final sorted order.</p>
|
||||
<p>The previous section introduced counting sort, which is suitable for scenarios where the data size <span class="arithmatex">\(n\)</span> is large but the data range <span class="arithmatex">\(m\)</span> is small. Suppose we need to sort <span class="arithmatex">\(n = 10^6\)</span> student IDs, where each ID is an <span class="arithmatex">\(8\)</span>-digit number. This means the data range <span class="arithmatex">\(m = 10^8\)</span> is very large. Using counting sort in this case would require significant memory space. Radix sort can avoid this situation.</p>
|
||||
<p><u>Radix sort</u> shares the same core concept as counting sort, which also sorts by counting the frequency of elements. Meanwhile, radix sort builds upon this by utilizing the progressive relationship between the digits of numbers. It processes and sorts the digits one at a time, achieving the final sorted order.</p>
|
||||
<h2 id="11101-algorithm-process">11.10.1 Algorithm process<a class="headerlink" href="#11101-algorithm-process" title="Permanent link">¶</a></h2>
|
||||
<p>Taking the student ID data as an example, assuming the least significant digit is the <span class="arithmatex">\(1^{st}\)</span> and the most significant is the <span class="arithmatex">\(8^{th}\)</span>, the radix sort process is illustrated in Figure 11-18.</p>
|
||||
<p>Taking the student ID data as an example, assume the least significant digit is the <span class="arithmatex">\(1^{st}\)</span> and the most significant is the <span class="arithmatex">\(8^{th}\)</span>, the radix sort process is illustrated in Figure 11-18.</p>
|
||||
<ol>
|
||||
<li>Initialize digit <span class="arithmatex">\(k = 1\)</span>.</li>
|
||||
<li>Perform "counting sort" on the <span class="arithmatex">\(k^{th}\)</span> digit of the student IDs. After completion, the data will be sorted from smallest to largest based on the <span class="arithmatex">\(k^{th}\)</span> digit.</li>
|
||||
<li>Increment <span class="arithmatex">\(k\)</span> by <span class="arithmatex">\(1\)</span>, then return to step <code>2.</code> and continue iterating until all digits have been sorted, then the process ends.</li>
|
||||
<li>Increment <span class="arithmatex">\(k\)</span> by <span class="arithmatex">\(1\)</span>, then return to step <code>2.</code> and continue iterating until all digits have been sorted, at which point the process ends.</li>
|
||||
</ol>
|
||||
<p><a class="glightbox" href="../radix_sort.assets/radix_sort_overview.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Radix sort algorithm process" class="animation-figure" src="../radix_sort.assets/radix_sort_overview.png" /></a></p>
|
||||
<p align="center"> Figure 11-18 Radix sort algorithm process </p>
|
||||
@@ -3840,14 +3840,14 @@ x_k = \lfloor\frac{x}{d^{k-1}}\rfloor \bmod d
|
||||
</div>
|
||||
<div class="admonition question">
|
||||
<p class="admonition-title">Why start sorting from the least significant digit?</p>
|
||||
<p>In consecutive sorting rounds, the result of a later round will override the result of an earlier round. For example, if the result of the first round is <span class="arithmatex">\(a < b\)</span> and the result of the second round is <span class="arithmatex">\(a > b\)</span>, the result of the second round will replace the first round's result. Since the significance of higher digits is greater than that of lower digits, it makes sense to sort lower digits before higher digits.</p>
|
||||
<p>In consecutive sorting rounds, the result of a later round will override the result of an earlier round. For example, if the result of the first round is <span class="arithmatex">\(a < b\)</span> and the second round is <span class="arithmatex">\(a > b\)</span>, the second round's result will replace the first round's result. Since higher-order digits take precedence over lower-order digits, it makes sense to sort the lower digits before the higher digits.</p>
|
||||
</div>
|
||||
<h2 id="11102-algorithm-characteristics">11.10.2 Algorithm characteristics<a class="headerlink" href="#11102-algorithm-characteristics" title="Permanent link">¶</a></h2>
|
||||
<p>Compared to counting sort, radix sort is suitable for larger numerical ranges, <strong>but it assumes that the data can be represented in a fixed number of digits, and the number of digits should not be too large</strong>. For example, floating-point numbers are not suitable for radix sort, as their digit count <span class="arithmatex">\(k\)</span> may be large, potentially leading to a time complexity <span class="arithmatex">\(O(nk) \gg O(n^2)\)</span>.</p>
|
||||
<p>Compared to counting sort, radix sort is suitable for larger numerical ranges, <strong>but it assumes that the data can be represented in a fixed number of digits, and the number of digits should not be too large</strong>. For example, floating-point numbers are unsuitable for radix sort, as their digit count <span class="arithmatex">\(k\)</span> may be large, potentially leading to a time complexity <span class="arithmatex">\(O(nk) \gg O(n^2)\)</span>.</p>
|
||||
<ul>
|
||||
<li><strong>Time complexity is <span class="arithmatex">\(O(nk)\)</span>, non-adaptive sorting</strong>: Assuming the data size is <span class="arithmatex">\(n\)</span>, the data is in base <span class="arithmatex">\(d\)</span>, and the maximum number of digits is <span class="arithmatex">\(k\)</span>, then sorting a single digit takes <span class="arithmatex">\(O(n + d)\)</span> time, and sorting all <span class="arithmatex">\(k\)</span> digits takes <span class="arithmatex">\(O((n + d)k)\)</span> time. Generally, both <span class="arithmatex">\(d\)</span> and <span class="arithmatex">\(k\)</span> are relatively small, leading to a time complexity approaching <span class="arithmatex">\(O(n)\)</span>.</li>
|
||||
<li><strong>Space complexity is <span class="arithmatex">\(O(n + d)\)</span>, non-in-place sorting</strong>: Like counting sort, radix sort relies on arrays <code>res</code> and <code>counter</code> of lengths <span class="arithmatex">\(n\)</span> and <span class="arithmatex">\(d\)</span> respectively.</li>
|
||||
<li><strong>Stable sorting</strong>: When counting sort is stable, radix sort is also stable; if counting sort is unstable, radix sort cannot guarantee a correct sorting outcome.</li>
|
||||
<li><strong>Stable sorting</strong>: When counting sort is stable, radix sort is also stable; if counting sort is unstable, radix sort cannot ensure a correct sorting order.</li>
|
||||
</ul>
|
||||
|
||||
<!-- Source file information -->
|
||||
|
||||
Reference in New Issue
Block a user