LoKwongho

mm

1098 Insertion or Heap Sort

1098 Insertion or Heap Sort (25 分)

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

Heap sort 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.

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?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the Nnumbers. It is assumed that the target sequence is always ascending. All the numbers in a line are 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 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.

* 注意标记行

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <queue>
 5 using namespace std;
 6 const int maxn = 105;
 7 
 8 int n;
 9 int a[maxn];
10 int b[maxn];
11 
12 void InsertSort(int v){
13     int tmp=b[v], i;
14     for(i=v-1;i>=0;i--){
15         if(b[i]>tmp){
16             b[i+1]=b[i];
17         }else{
18             break;
19         }
20     }
21     b[i+1] = tmp;
22     
23     for(int i=0;i<n;i++){
24         if(i!=0) printf(" ");
25         printf("%d",b[i]);
26     }
27 }
28 
29 void HeapSort(int v){ // MAX HEAP
30     int tmp=b[0];
31     b[0]=b[v];
32     b[v]=tmp;
33     tmp = b[0];
34     int i=0, child;
35     for(;(i+1)*2-1<=v-1; i=child){
36         child = (i+1)*2-1;
37         if(b[child+1]>b[child] && child+1<=v-1){
38             child++;
39         }
40         if(tmp<b[child]){
41             b[i]=b[child];
42         }else{
43             break;
44         }
45     }
46     b[i] = tmp;
47     for(int i=0;i<n;i++){
48         if(i!=0) printf(" ");
49         printf("%d",b[i]);
50     }
51 }
52 
53 void Judge(){
54     int i,ptr;;
55     for(i=1;i<n&&b[i-1]<=b[i];i++); // !!!注意这里
56     ptr=i;
57     for(; i<n&&a[i]==b[i];i++);
58     if(i==n){
59         printf("Insertion Sort\n");
60         InsertSort(ptr);
61     }else{
62         sort(a, a+n);
63         for(i=n-1;i>=0&&a[i]==b[i];i--);
64         printf("Heap Sort\n");
65         HeapSort(i);
66     }
67 }
68 
69 int main(){
70     cin>>n;
71     for(int i=0; i<n; i++){
72         scanf("%d",&a[i]);
73     }
74     for(int i=0; i<n; i++){
75         scanf("%d",&b[i]);
76     }
77     Judge();
78 }

 

posted on 2018-11-10 15:29  iojafekniewg  阅读(143)  评论(0编辑  收藏  举报

导航

My Email guangho2743##foxmail.com : )