This commit is contained in:
krahets
2025-03-14 17:51:07 +08:00
parent 1b1e1e354a
commit 3b08169043
39 changed files with 956 additions and 958 deletions
+10 -10
View File
@@ -3604,8 +3604,8 @@
<h1 id="116-merge-sort">11.6 &nbsp; Merge sort<a class="headerlink" href="#116-merge-sort" title="Permanent link">&para;</a></h1>
<p><u>Merge sort</u> is a sorting algorithm based on the divide-and-conquer strategy, involving the "divide" and "merge" phases shown in Figure 11-10.</p>
<ol>
<li><strong>Divide phase</strong>: Recursively split the array from the midpoint, transforming the sorting problem of a long array into that of shorter arrays.</li>
<li><strong>Merge phase</strong>: Stop dividing when the length of the sub-array is 1, start merging, and continuously combine two shorter ordered arrays into one longer ordered array until the process is complete.</li>
<li><strong>Divide phase</strong>: Recursively split the array from the midpoint, transforming the sorting problem of a long array into shorter arrays.</li>
<li><strong>Merge phase</strong>: Stop dividing when the length of the sub-array is 1, and then begin merging. The two shorter sorted arrays are continuously merged into a longer sorted array until the process is complete.</li>
</ol>
<p><a class="glightbox" href="../merge_sort.assets/merge_sort_overview.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="The divide and merge phases of merge sort" class="animation-figure" src="../merge_sort.assets/merge_sort_overview.png" /></a></p>
<p align="center"> Figure 11-10 &nbsp; The divide and merge phases of merge sort </p>
@@ -3614,9 +3614,9 @@
<p>As shown in Figure 11-11, the "divide phase" recursively splits the array from the midpoint into two sub-arrays from top to bottom.</p>
<ol>
<li>Calculate the midpoint <code>mid</code>, recursively divide the left sub-array (interval <code>[left, mid]</code>) and the right sub-array (interval <code>[mid + 1, right]</code>).</li>
<li>Continue with step <code>1.</code> recursively until the sub-array interval length is 1 to stop.</li>
<li>Continue with step <code>1.</code> recursively until sub-array length becomes 1, then stops.</li>
</ol>
<p>The "merge phase" combines the left and right sub-arrays into a single ordered array from bottom to top. Note that merging starts with sub-arrays of length 1, and each sub-array is ordered during the merge phase.</p>
<p>The "merge phase" combines the left and right sub-arrays into a sorted array from bottom to top. It is important to note that, merging starts with sub-arrays of length 1, and each sub-array is sorted during the merge phase.</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:10"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">&lt;1&gt;</label><label for="__tabbed_1_2">&lt;2&gt;</label><label for="__tabbed_1_3">&lt;3&gt;</label><label for="__tabbed_1_4">&lt;4&gt;</label><label for="__tabbed_1_5">&lt;5&gt;</label><label for="__tabbed_1_6">&lt;6&gt;</label><label for="__tabbed_1_7">&lt;7&gt;</label><label for="__tabbed_1_8">&lt;8&gt;</label><label for="__tabbed_1_9">&lt;9&gt;</label><label for="__tabbed_1_10">&lt;10&gt;</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
@@ -3653,10 +3653,10 @@
</div>
<p align="center"> Figure 11-11 &nbsp; Merge sort process </p>
<p>It is observed that the order of recursion in merge sort is consistent with the post-order traversal of a binary tree.</p>
<p>It can be observed that the order of recursion in merge sort is consistent with the post-order traversal of a binary tree.</p>
<ul>
<li><strong>Post-order traversal</strong>: First recursively traverse the left subtree, then the right subtree, and finally handle the root node.</li>
<li><strong>Merge sort</strong>: First recursively handle the left sub-array, then the right sub-array, and finally perform the merge.</li>
<li><strong>Post-order traversal</strong>: First recursively traverse the left subtree, then the right subtree, and finally process the root node.</li>
<li><strong>Merge sort</strong>: First recursively process the left sub-array, then the right sub-array, and finally perform the merge.</li>
</ul>
<p>The implementation of merge sort is shown in the following code. Note that the interval to be merged in <code>nums</code> is <code>[left, right]</code>, while the corresponding interval in <code>tmp</code> is <code>[0, right - left]</code>.</p>
<div class="tabbed-set tabbed-alternate" data-tabs="2:14"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><input id="__tabbed_2_13" name="__tabbed_2" type="radio" /><input id="__tabbed_2_14" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label><label for="__tabbed_2_14">Zig</label></div>
@@ -3863,12 +3863,12 @@
<li><strong>Stable sort</strong>: During the merging process, the order of equal elements remains unchanged.</li>
</ul>
<h2 id="1163-linked-list-sorting">11.6.3 &nbsp; Linked List sorting<a class="headerlink" href="#1163-linked-list-sorting" title="Permanent link">&para;</a></h2>
<p>For linked lists, merge sort has significant advantages over other sorting algorithms, <strong>optimizing the space complexity of the linked list sorting task to <span class="arithmatex">\(O(1)\)</span></strong>.</p>
<p>For linked lists, merge sort has significant advantages over other sorting algorithms. <strong>It can optimize the space complexity of the linked list sorting task to <span class="arithmatex">\(O(1)\)</span></strong>.</p>
<ul>
<li><strong>Divide phase</strong>: "Iteration" can be used instead of "recursion" to perform the linked list division work, thus saving the stack frame space used by recursion.</li>
<li><strong>Merge phase</strong>: In linked lists, node addition and deletion operations can be achieved by changing references (pointers), so no extra lists need to be created during the merge phase (combining two short ordered lists into one long ordered list).</li>
<li><strong>Merge phase</strong>: In linked lists, node insertion and deletion operations can be achieved by changing references (pointers), so no extra lists need to be created during the merge phase (combining two short ordered lists into one long ordered list).</li>
</ul>
<p>Detailed implementation details are complex, and interested readers can consult related materials for learning.</p>
<p>The implementation details are relatively complex, and interested readers can consult related materials for learning.</p>
<!-- Source file information -->