mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 23:16:07 +00:00
deploy
This commit is contained in:
@@ -3584,16 +3584,16 @@
|
||||
|
||||
<!-- Page content -->
|
||||
<h1 id="111-sorting-algorithms">11.1 Sorting algorithms<a class="headerlink" href="#111-sorting-algorithms" title="Permanent link">¶</a></h1>
|
||||
<p><u>Sorting algorithms (sorting algorithm)</u> are used to arrange a set of data in a specific order. Sorting algorithms have a wide range of applications because ordered data can usually be searched, analyzed, and processed more efficiently.</p>
|
||||
<p>As shown in Figure 11-1, the data types in sorting algorithms can be integers, floating point numbers, characters, or strings, etc. Sorting rules can be set according to needs, such as numerical size, character ASCII order, or custom rules.</p>
|
||||
<p><u>Sorting algorithms</u> are used to arrange a set of data in a specific order. Sorting algorithms have a wide range of applications because ordered data can usually be searched, analyzed, and processed more efficiently.</p>
|
||||
<p>As shown in Figure 11-1, the data types in sorting algorithms can be integers, floating point numbers, characters, or strings, etc. Sorting criterion can be set according to needs, such as numerical size, character ASCII order, or custom criterion.</p>
|
||||
<p><a class="glightbox" href="../sorting_algorithm.assets/sorting_examples.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Data types and comparator examples" class="animation-figure" src="../sorting_algorithm.assets/sorting_examples.png" /></a></p>
|
||||
<p align="center"> Figure 11-1 Data types and comparator examples </p>
|
||||
|
||||
<h2 id="1111-evaluation-dimensions">11.1.1 Evaluation dimensions<a class="headerlink" href="#1111-evaluation-dimensions" title="Permanent link">¶</a></h2>
|
||||
<p><strong>Execution efficiency</strong>: We expect the time complexity of sorting algorithms to be as low as possible, with a lower number of overall operations (reduction in the constant factor of time complexity). For large data volumes, execution efficiency is particularly important.</p>
|
||||
<p><strong>In-place property</strong>: As the name implies, <u>in-place sorting</u> is achieved by directly manipulating the original array, without the need for additional auxiliary arrays, thus saving memory. Generally, in-place sorting involves fewer data movement operations and is faster.</p>
|
||||
<p><strong>Execution efficiency</strong>: We expect the time complexity of sorting algorithms to be as low as possible, as well as a lower number of overall operations (lowering the constant term of time complexity). For large data volumes, execution efficiency is particularly important.</p>
|
||||
<p><strong>In-place property</strong>: As the name implies, <u>in-place sorting</u> is achieved by directly manipulating the original array, without the need for additional helper arrays, thus saving memory. Generally, in-place sorting involves fewer data moving operations and is faster.</p>
|
||||
<p><strong>Stability</strong>: <u>Stable sorting</u> ensures that the relative order of equal elements in the array does not change after sorting.</p>
|
||||
<p>Stable sorting is a necessary condition for multi-level sorting scenarios. Suppose we have a table storing student information, with the first and second columns being name and age, respectively. In this case, <u>unstable sorting</u> might lead to a loss of orderedness in the input data:</p>
|
||||
<p>Stable sorting is a necessary condition for multi-key sorting scenarios. Suppose we have a table storing student information, with the first and second columns being name and age, respectively. In this case, <u>unstable sorting</u> might lead to a loss of order in the input data:</p>
|
||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="c1"># Input data is sorted by name</span>
|
||||
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="c1"># (name, age)</span>
|
||||
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="o">(</span><span class="s1">'A'</span>,<span class="w"> </span><span class="m">19</span><span class="o">)</span>
|
||||
@@ -3612,7 +3612,7 @@
|
||||
<a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a><span class="w"> </span><span class="o">(</span><span class="s1">'E'</span>,<span class="w"> </span><span class="m">23</span><span class="o">)</span>
|
||||
</code></pre></div>
|
||||
<p><strong>Adaptability</strong>: <u>Adaptive sorting</u> leverages existing order information within the input data to reduce computational effort, achieving more optimal time efficiency. The best-case time complexity of adaptive sorting algorithms is typically better than their average-case time complexity.</p>
|
||||
<p><strong>Comparison-based</strong>: <u>Comparison-based sorting</u> relies on comparison operators (<span class="arithmatex">\(<\)</span>, <span class="arithmatex">\(=\)</span>, <span class="arithmatex">\(>\)</span>) to determine the relative order of elements and thus sort the entire array, with the theoretical optimal time complexity being <span class="arithmatex">\(O(n \log n)\)</span>. Meanwhile, <u>non-comparison sorting</u> does not use comparison operators and can achieve a time complexity of <span class="arithmatex">\(O(n)\)</span>, but its versatility is relatively poor.</p>
|
||||
<p><strong>Comparison or non-comparison-based</strong>: <u>Comparison-based sorting</u> relies on comparison operators (<span class="arithmatex">\(<\)</span>, <span class="arithmatex">\(=\)</span>, <span class="arithmatex">\(>\)</span>) to determine the relative order of elements and thus sort the entire array, with the theoretical optimal time complexity being <span class="arithmatex">\(O(n \log n)\)</span>. Meanwhile, <u>non-comparison sorting</u> does not use comparison operators and can achieve a time complexity of <span class="arithmatex">\(O(n)\)</span>, but its versatility is relatively poor.</p>
|
||||
<h2 id="1112-ideal-sorting-algorithm">11.1.2 Ideal sorting algorithm<a class="headerlink" href="#1112-ideal-sorting-algorithm" title="Permanent link">¶</a></h2>
|
||||
<p><strong>Fast execution, in-place, stable, adaptive, and versatile</strong>. Clearly, no sorting algorithm that combines all these features has been found to date. Therefore, when selecting a sorting algorithm, it is necessary to decide based on the specific characteristics of the data and the requirements of the problem.</p>
|
||||
<p>Next, we will learn about various sorting algorithms together and analyze the advantages and disadvantages of each based on the above evaluation dimensions.</p>
|
||||
|
||||
Reference in New Issue
Block a user