mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-23 03:46:05 +00:00
deploy
This commit is contained in:
@@ -3662,7 +3662,7 @@
|
||||
<li><strong>Divide (partition phase)</strong>: Recursively decompose the original problem into two or more sub-problems until the smallest sub-problem is reached and the process terminates.</li>
|
||||
<li><strong>Conquer (merge phase)</strong>: Starting from the smallest sub-problem with a known solution, merge the solutions of the sub-problems from bottom to top to construct the solution to the original problem.</li>
|
||||
</ol>
|
||||
<p>As shown in the Figure 12-1 , "merge sort" is one of the typical applications of the divide and conquer strategy.</p>
|
||||
<p>As shown in Figure 12-1, "merge sort" is one of the typical applications of the divide and conquer strategy.</p>
|
||||
<ol>
|
||||
<li><strong>Divide</strong>: Recursively divide the original array (original problem) into two sub-arrays (sub-problems), until the sub-array has only one element (smallest sub-problem).</li>
|
||||
<li><strong>Conquer</strong>: Merge the ordered sub-arrays (solutions to the sub-problems) from bottom to top to obtain an ordered original array (solution to the original problem).</li>
|
||||
@@ -3687,7 +3687,7 @@
|
||||
<p><strong>Divide and conquer can not only effectively solve algorithm problems but often also improve algorithm efficiency</strong>. In sorting algorithms, quicksort, merge sort, and heap sort are faster than selection, bubble, and insertion sorts because they apply the divide and conquer strategy.</p>
|
||||
<p>Then, we may ask: <strong>Why can divide and conquer improve algorithm efficiency, and what is the underlying logic?</strong> In other words, why are the steps of decomposing a large problem into multiple sub-problems, solving the sub-problems, and merging the solutions of the sub-problems into the solution of the original problem more efficient than directly solving the original problem? This question can be discussed from the aspects of the number of operations and parallel computation.</p>
|
||||
<h3 id="1-optimization-of-operation-count">1. Optimization of operation count<a class="headerlink" href="#1-optimization-of-operation-count" title="Permanent link">¶</a></h3>
|
||||
<p>Taking "bubble sort" as an example, it requires <span class="arithmatex">\(O(n^2)\)</span> time to process an array of length <span class="arithmatex">\(n\)</span>. Suppose we divide the array from the midpoint into two sub-arrays as shown in the Figure 12-2 , then the division requires <span class="arithmatex">\(O(n)\)</span> time, sorting each sub-array requires <span class="arithmatex">\(O((n / 2)^2)\)</span> time, and merging the two sub-arrays requires <span class="arithmatex">\(O(n)\)</span> time, with the total time complexity being:</p>
|
||||
<p>Taking "bubble sort" as an example, it requires <span class="arithmatex">\(O(n^2)\)</span> time to process an array of length <span class="arithmatex">\(n\)</span>. Suppose we divide the array from the midpoint into two sub-arrays as shown in Figure 12-2, then the division requires <span class="arithmatex">\(O(n)\)</span> time, sorting each sub-array requires <span class="arithmatex">\(O((n / 2)^2)\)</span> time, and merging the two sub-arrays requires <span class="arithmatex">\(O(n)\)</span> time, with the total time complexity being:</p>
|
||||
<div class="arithmatex">\[
|
||||
O(n + (\frac{n}{2})^2 \times 2 + n) = O(\frac{n^2}{2} + 2n)
|
||||
\]</div>
|
||||
@@ -3708,7 +3708,7 @@ n(n - 4) & > 0
|
||||
<h3 id="2-optimization-through-parallel-computation">2. Optimization through parallel computation<a class="headerlink" href="#2-optimization-through-parallel-computation" title="Permanent link">¶</a></h3>
|
||||
<p>We know that the sub-problems generated by divide and conquer are independent of each other, <strong>thus they can usually be solved in parallel</strong>. This means that divide and conquer can not only reduce the algorithm's time complexity, <strong>but also facilitate parallel optimization by the operating system</strong>.</p>
|
||||
<p>Parallel optimization is especially effective in environments with multiple cores or processors, as the system can process multiple sub-problems simultaneously, making fuller use of computing resources and significantly reducing the overall runtime.</p>
|
||||
<p>For example, in the "bucket sort" shown in the Figure 12-3 , we distribute massive data evenly across various buckets, then the sorting tasks of all buckets can be distributed to different computing units, and the results are merged after completion.</p>
|
||||
<p>For example, in the "bucket sort" shown in Figure 12-3, we distribute massive data evenly across various buckets, then the sorting tasks of all buckets can be distributed to different computing units, and the results are merged after completion.</p>
|
||||
<p><a class="glightbox" href="../divide_and_conquer.assets/divide_and_conquer_parallel_computing.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Bucket sort's parallel computation" class="animation-figure" src="../divide_and_conquer.assets/divide_and_conquer_parallel_computing.png" /></a></p>
|
||||
<p align="center"> Figure 12-3 Bucket sort's parallel computation </p>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user