PTA 09-Sort 3 Insertion or Heap sort (25 min)

Source: Internet
Author: User

Title Address

https://pta.patest.cn/pta/test/16/exam/4/question/676

5-14 insertion or Heap Sort (25 points)

According to Wikipedia:

Insertion Sort 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 Li St, and inserts it there. It repeats until no input elements remain.

Heap Sort divides its input to a sorted and an unsorted region, and it iteratively shrinks the unsorted region By extracting the largest element and moving, the sorted region. It involves the use of a heap data structure rather than a linear-time search to find the maximum.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorti Ng method, can you tell which sorting method we is using?

Input Specification:

Each input file contains the one test case. For each case, the first line gives a positive integerNN\le 100≤< Span class= "Mord mathrm" >100). Then in the next Line, n N  integers is given as the initial sequence. The last line contains the partially sorted sequence of The nn numbers. It is assumed, the target sequence is always ascending. All the numbers in a line is separated by a space.

Output Specification:

For each test case, print in the first line either "insertion sort" or "Heap sort" to indicate the method used to obtain T He partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed, the answer is unique for each test case. All the numbers in a line must is separated by a space, and there must is no extra space at the end of the line.

Sample Input 1:
103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0
Sample Output 1:
Insertion Sort1 2 3 5 7 8 9 4 6 0
Sample Input 2:
103 1 2 8 7 5 9 4 6 06 4 5 1 0 3 2 7 8 9
Sample Output 2:
Heap Sort5 4 3 1 0 2 6 7 8 9


/* Evaluation result time result score Topic compiler spents (MS) memory (MB) User 2017-07-06 18:04 answer correct 255-14gcc141 test point results Score/out of Use (MS) memory (MB) test point 1 answer correct 7/ 711 Test point 2 answer correct 6/621 test point 3 answer correct 2/211 test point 4 answer correct 2/221 test point 5 answer correct 4/4141 test point 6 answer correct 4/4141*/#include <stdio.h> #define MAXN 100int a[maxn],b[maxn],tmp[maxn];int gnextturntoprint=0;int gprinted=0;int gsorttype=0;void swap (int *a,int *b) {int Temp;temp=*a;*a=*b;*b=temp;} void Dbg_printarray (int N)//debug with function {int d;for (d=0;d<n;d++) {printf ("%d", A[d]);} printf ("\ n");} void Checkmethod (int N) {if (gprinted) Return;int i;if (gnextturntoprint==1) {if (gsorttype==0) printf ("Insertion sort\n" ); elseprintf ("Heap sort\n"); for (i=0;i<n;i++) {printf ("%d", a[i]), if (i!=n-1) printf ("");} Gnextturntoprint=0;gprinted=1;return;} for (i=0;i<n;i++) {if (a[i]!=b[i]) return;} Gnextturntoprint=1;} void Insertionsort (int a[],int left, int right,int N) {int i,j,temp;for (i=left;i<right;i++) {temp=a[i+1];for (j=i+1;j >left;j--) {if (temp<a[j-1]) A[j]=a[j-1];else break;} A[j]=temp; Checkmethod (N);}} void Precdown (int a[],int x,int N) {int temp=a[x]; int parent,child;parent=x;for (child=2*parent+1; child<= N; child=child*2+1) {if (child!=n) {if (a[child+1]>a[ Child]) child++;} if (Temp<a[child]) {a[parent]=a[child];p arent=child;} else break;} A[parent]=temp;//dbg_printarray (n+1);} void heapsort (int a[],int N) {//Build a large top heap and then move the first element back int i,ptrright=n-1,temp,d;for (i= (ptrRight-1)/2;i>=0;i--) {Precdown (A, I,ptrright);} for (i=ptrright;i>0;i--) {temp=a[i];a[i]=a[0];a[0]=temp; Precdown (a,0,i-1); Checkmethod (N);}} int main () {int i,n;scanf ("%d", &n), for (i=0;i<n;i++) {scanf ("%d", &a[i]); tmp[i]=a[i];} for (i=0;i<n;i++) {scanf ("%d", &b[i]);} Insertionsort (A,0,n-1,n); if (!gprinted) {for (i=0;i<n;i++) {a[i]=tmp[i];} gsorttype=1; Heapsort (A,n);}}

  

PTA 09-Sort 3 Insertion or Heap sort (25 min)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.