mirror of
https://github.com/xlucn/PAT.git
synced 2026-11-05 06:37:35 +08:00
26 lines
3.9 KiB
HTML
26 lines
3.9 KiB
HTML
Insertion or Heap Sort
|
|
<div class="ques-view"><p>According to Wikipedia:</p>
|
|
<p><strong>Insertion sort</strong> iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.</p>
|
|
<p><strong>Heap sort</strong> divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.</p>
|
|
<p>Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?</p>
|
|
<h3 id="input-specification-">Input Specification:</h3>
|
|
<p>Each input file contains one test case. For each case, the first line gives a positive integer <span class="katex"><span class="katex-mathml"><math><mrow><mi>N</mi></mrow>N</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.68333em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10903em;">N</span></span></span></span> (<span class="katex"><span class="katex-mathml"><math><mrow><mo>≤</mo><mn>1</mn><mn>0</mn><mn>0</mn></mrow>\le 100</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.78041em;vertical-align:-0.13597em;"></span><span class="base textstyle uncramped"><span class="mrel">≤</span><span class="mord mathrm">1</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span></span></span></span>). Then in the next line, <span class="katex"><span class="katex-mathml"><math><mrow><mi>N</mi></mrow>N</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.68333em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10903em;">N</span></span></span></span> integers are given as the initial sequence. The last line contains the partially sorted sequence of the <span class="katex"><span class="katex-mathml"><math><mrow><mi>N</mi></mrow>N</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.68333em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10903em;">N</span></span></span></span> numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.</p>
|
|
<h3 id="output-specification-">Output Specification:</h3>
|
|
<p>For each test case, print in the first line either "Insertion Sort" or "Heap Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.</p>
|
|
<h3 id="sample-input-1-">Sample Input 1:</h3>
|
|
<pre><code class="lang-in">10
|
|
3 1 2 8 7 5 9 4 6 0
|
|
1 2 3 7 8 5 9 4 6 0
|
|
</code></pre>
|
|
<h3 id="sample-output-1-">Sample Output 1:</h3>
|
|
<pre><code class="lang-out">Insertion Sort
|
|
1 2 3 5 7 8 9 4 6 0
|
|
</code></pre>
|
|
<h3 id="sample-input-2-">Sample Input 2:</h3>
|
|
<pre><code>10
|
|
3 1 2 8 7 5 9 4 6 0
|
|
6 4 5 1 0 3 2 7 8 9
|
|
</code></pre><h3 id="sample-output-2-">Sample Output 2:</h3>
|
|
<pre><code>Heap Sort
|
|
5 4 3 1 0 2 6 7 8 9
|
|
</code></pre></div> |