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 N numbers. 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.
Sample Input 1:
1 2 3 |
10 3 1 2 8 7 5 9 4 6 0 1 2 3 7 8 5 9 4 6 0 |
Sample Output 1:
1 2 |
Insertion Sort 1 2 3 5 7 8 9 4 6 0 |
Sample Input 2:
1 2 3 |
10 3 1 2 8 7 5 9 4 6 0 6 4 5 1 0 3 2 7 8 9 |
Sample Output 2:
1 2 |
Heap Sort 5 4 3 1 0 2 6 7 8 9 |
注意下标 i=0 时不属于插入排序的环节,我一开始偷懒 i=0 ,测试点 2 一直未能
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
#include<stdio.h> #include<stdlib.h> int seqcmp(int *a,int *b,int n) { int i; for(i=0;i<n;i++) { if(a[i]!=b[i]) { return 1; } } return 0; } void print_seq(int *data,int n) { int i; if(n<=0) { return ; } printf("%d",data[0]); for(i=1;i<n;i++) { printf(" %d",data[i]); } printf("\n"); } int insertion(int *data,int times) { int temp; temp=data[times]; while(temp<data[times-1]&×>=1) { data[times]=data[times-1]; times--; } data[times]=temp; } void percolate_down(int *data,int n,int pos) { int child,temp; temp=data[pos]; while(1) { child=pos*2; if(child<n&&data[child]<data[child+1]) { child=child+1; } if(child<=n&&data[child]>temp) { data[pos]=data[child]; } else { break; } pos=child; } data[pos]=temp; } void build_heap(int *data,int n) { int i; for(i=n/2;i>0;i--) { percolate_down(data,n,i); } } int heap(int *data,int n,int times) { int temp; temp=data[n-times]; data[n-times]=data[1]; data[1]=temp; percolate_down(data,n-times-1,1); } int main() { int *initial_seq,*initial_seq_c,*sorted_seq; int n,i; scanf("%d",&n); initial_seq=(int *)malloc(n*sizeof(int)); initial_seq_c=(int *)malloc((n+1)*sizeof(int)); sorted_seq=(int *)malloc(n*sizeof(int)); for(i=0;i<n;i++) { scanf("%d",initial_seq+i); initial_seq_c[i+1]=initial_seq[i]; } for(i=0;i<n;i++) { scanf("%d",sorted_seq+i); } build_heap(initial_seq_c,n); for(i=1;i<n;i++)//i=0 不属于插入排序的环节,我一开始偷懒 i=0 ,测试点 2 一直未能通过 { insertion(initial_seq,i); if(!seqcmp(initial_seq,sorted_seq,n)) { if(i<n-1) insertion(initial_seq,i+1); printf("Insertion Sort\n"); print_seq(initial_seq,n); return 0; } } for(i=0;i<n;i++) { heap(initial_seq_c,n,i); if(!seqcmp(initial_seq_c+1,sorted_seq,n)) { if(i<n-1) heap(initial_seq_c,n,i+1); printf("Heap Sort\n"); print_seq(initial_seq_c+1,n); return 0; } } } |