You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<spanid="cb9-7"><ahref="#cb9-7" aria-hidden="true" tabindex="-1"></a><spanclass="cf">return</span><spanclass="dv">1</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
377
-
<p>Let’s write the <code>minimum</code> method using this convention. Since we have no knowledge of the structure of the sequence other than it supports partial ordering, we have to test all possible items, like before. But now, instead of returning as soon as we find the “correcOf course, we t” item, we simply store the minimum item we’ve seen so far, and return at the end of the <code>for</code> loop. This guarantees we have seen all the items, and thus the minimum among them must be the one we have marked.</p>
377
+
<p>Let’s write the <code>minimum</code> method using this convention. Since we have no knowledge of the structure of the sequence other than it supports partial ordering, we have to test all possible items, like before. But now, instead of returning as soon as we find the correct item, we simply store the minimum item we’ve seen so far, and return at the end of the <code>for</code> loop. This guarantees we have seen all the items, and thus the minimum among them must be the one we have marked.</p>
<p>Selection, Insertion, and Bubble sort are all <spanclass="math inline">\(O(n^2)\)</span> algorithms. The reason is structural: in the worst case, a list of size can have <spanclass="math inline">\(O(n^2)\)</span> inversions. Since each swap in these algorithms only fixes one inversion at a time–in the best case–we are forced to perform a quadratic number of operations.</p>
430
-
<p>To break this ceiling and reach the theoretical limit of <spanclass="math inline">\(O(n^2)\)</span>, we need to be more clever. We need algorithms that can fix <em>many</em> inversions with a single operation. This “divide and conquer” approach will be the focus of our next chapter: <strong>Efficient Sorting</strong>.</p>
430
+
<p>To break this ceiling and reach the theoretical limit of <spanclass="math inline">\(O(n \log n)\)</span>, we need to be more clever. We need algorithms that can fix <em>many</em> inversions with a single operation. This “divide and conquer” approach will be the focus of our next chapter: <strong>Efficient Sorting</strong>.</p>
<spanid="cb1-39"><ahref="#cb1-39" aria-hidden="true" tabindex="-1"></a> k <spanclass="op">+=</span><spanclass="dv">1</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
375
375
<p>In Merge Sort, the structural strategy is to <strong>fix inversions within each half first</strong>. We recursively dive deep into the sequence, sorting smaller and smaller sub-sequences. Only when the sub-sequences are internally free of inversions do we perform the merge step to fix the “global” inversions between the two halves.</p>
376
-
<p>Because the list is halved at each step, the recursion depth is . Since we perform work at each level of the tree to merge the results, the total complexity is .</p>
376
+
<p>Because the list is halved at each step, the recursion depth is <spanclass="math inline">\(log(n)\)</span>. Since we perform <spanclass="math inline">\(O(n)\)</span>work at each level of the tree to merge the results, the total complexity is <spanclass="math inline">\(O(n ĺog n)\)</span>.</p>
<spanid="cb2-28"><ahref="#cb2-28" aria-hidden="true" tabindex="-1"></a> _quick_sort(<spanclass="dv">0</span>, <spanclass="bu">len</span>(items) <spanclass="op">-</span><spanclass="dv">1</span>)</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
410
410
<p>The striking difference here is the order of operations. Quick Sort <strong>fixes inversions between both halves first</strong>. By partitioning, we ensure that there are zero inversions between the left “half” and the right “half” (no item in the left is greater than an item in the right). Once this global structure is established, we recursively fix the remaining inversions <strong>within</strong> each half.</p>
411
-
<p>This approach allows Quick Sort to operate <strong>in-place</strong>, requiring only auxiliary space for the recursion stack. While its worst-case is , its average-case performance is a highly efficient .</p>
411
+
<p>This approach allows Quick Sort to operate <strong>in-place</strong>, requiring only <spanclass="math inline">\(O(\log n)\)</span>auxiliary space for the recursion stack. While its worst-case is <spanclass="math inline">\(O(n^2)\)</span>, its average-case performance is a highly efficient <spanclass="math inline">\(O(n \log n)\)</span>.</p>
<p>Both Merge Sort and Quick Sort reaffirm our central theme: <strong>structure matters</strong>. By moving away from the unstructured swaps of Bubble Sort and adopting a rigorous divide-and-conquer strategy, we change how we interact with the “geometry of inversions.”</p>
432
+
<p>Both Merge Sort and Quick Sort reaffirm our central theme: <strong>structure matters</strong>. By moving away from the unstructured swaps of Bubble Sort and adopting a rigorous divide-and-conquer strategy, we change how we interact with the geometry of inversions.</p>
433
433
<p>Whether we fix local inversions first (Merge Sort) or global ones first (Quick Sort), the result is a massive leap in efficiency. We have traded the simplicity of a double-loop for the power of recursion and structural partitioning.</p>
<spanid="cb1-45"><ahref="#cb1-45" aria-hidden="true" tabindex="-1"></a><spanclass="cf">return</span> i</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
351
-
<p>The probabilistic analysis of Quick Select is striking. In the worst case—where we consistently pick the worst possible pivot—the complexity is still . However, on average, the size of the search space follows a geometric series: , which converges to . This means that on average, Quick Select finds the -th order statistic in **** time.</p>
380
+
<p>The probabilistic analysis of Quick Select is striking. In the worst case—where we consistently pick the worst possible pivot—the complexity is still <spanclass="math inline">\(O(n^2)\)</span>. However, on average, the size of the search space follows a geometric serieswhich converges to <spanclass="math inline">\(O(n)\)</span>. This means that on average, Quick Select finds the <spanclass="math inline">\(k\)</span>-th order statistic in <spanclass="math inline">\(O(n)\)</span> time.</p>
<h2data-number="5.2" class="anchored" data-anchor-id="median-of-medians-deterministic-selection"><spanclass="header-section-number">5.2</span> Median of Medians: Deterministic Selection</h2>
0 commit comments