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?
Each input file contains one test case. For each case, the first line gives a positive integer N (≤). 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.
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 resulting 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.
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
Insertion Sort
1 2 3 5 7 8 9 4 6 0
10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9
Heap Sort
5 4 3 1 0 2 6 7 8 9
1 #include <cstdio> 2 #define MAXN 101 3 #include <algorithm> 4 using namespace std; 5 6 7 typedef int ElementType; 8 9 10 int Targ[MAXN]; 11 12 13 bool isSame(int *a, int *b, int N); 14 void Output(int *A, int N); 15 16 17 void isInsertionSort(int* A, int N); 18 19 int isMergeSort(int *A, int N); 20 21 void Percdown(int *A, int root, int N); 22 void BuildMaxHeap(int *A, int N); 23 void isHeapSort(int *A, int N); 24 25 26 int main() 27 { 28 int N; 29 int Ori[MAXN]; 30 int Ori2[MAXN]; 31 32 33 scanf("%d", &N); 34 for (int i=0; i<N; i++) { 35 scanf( "%d", &Ori[i] ); 36 } 37 for (int i=0; i<N; i++) { 38 scanf( "%d", &Targ[i] ); 39 } 40 for (int i=0; i<N; i++) { 41 Ori2[i] = Ori[i]; 42 } 43 44 isInsertionSort(Ori, N); 45 isHeapSort(Ori2, N); 46 47 } 48 void isInsertionSort(int A[], int N) 49 { 50 int i, j, temp; 51 int flag = 0; 52 for (i=1; i<N; i++) { 53 temp = A[i]; 54 for (j=i; j>0 && A[j-1] > temp; j--) { 55 A[j] = A[j-1]; 56 } 57 A[j] = temp; 58 59 if (flag) { 60 printf("Insertion Sort\n"); 61 Output(A, N); 62 break; 63 } 64 65 if (isSame(A, Targ, N)) { 66 flag = 1; 67 } 68 } 69 70 71 } 72 73 int isMergeSort(int *A, int N) 74 { 75 int P, i; 76 int flag = 0; 77 for (P=1; P<N; P*=2) { 78 for (i=0; i<N; i+=2*P) { 79 sort( A + i, A + min(N, i+2*P) ); 80 } 81 82 if (flag) { 83 Output(A, N); 84 break; 85 } 86 if( isSame(A, Targ, N) ){ 87 printf("Merge Sort\n"); 88 flag = 1; 89 } 90 } 91 return flag; 92 93 } 94 95 void Percdown(int *A, int root, int N) //N is number of elements 96 { 97 int Parent, Child; 98 ElementType Top; 99 Top = A[root]; 100 101 for (Parent = root; Parent*2+1 < N; Parent = Child) { 102 Child = 2*Parent+1; 103 if (Child != N-1 and A[Child+1]>A[Child]) { 104 Child++; 105 } 106 if (A[Child] > Top) { 107 A[Parent] = A[Child]; 108 } 109 else break; 110 } 111 A[Parent] = Top; 112 113 } 114 void BuildMaxHeap(int *A, int N) 115 { 116 for (int i=(N-1)/2; i>=0; i--) { 117 Percdown(A, i, N); 118 } 119 } 120 void isHeapSort(int *A, int N) 121 { 122 int flag; 123 flag = 0; 124 125 BuildMaxHeap(A, N); 126 127 for (int i=N-1; i>0; i--) { 128 swap(A[0], A[i]); 129 Percdown(A, 0, i); 130 131 if (flag) { 132 printf("Heap Sort\n"); 133 Output(A, N); 134 break; 135 } 136 137 if(isSame(A, Targ, N)) { 138 flag = 1; 139 } 140 } 141 142 143 } 144 145 146 147 148 149 bool isSame(int *a, int *b, int N) 150 { 151 int i; 152 for (i=0; i<N; i++) { 153 if(a[i] != b[i]) break; 154 } 155 if (i == N) { 156 return true; 157 } 158 else 159 return false; 160 } 161 void Output(int *A, int N) 162 { 163 for (int i=0; i<N; i++) { 164 if (i != 0) { 165 printf(" "); 166 } 167 printf("%d", A[i]); 168 } 169 printf("\n"); 170 }
一、思路
模拟整个排序过程,每排一次都比较两个数组,O(N^2),很笨的算法,正在努力改进中
09-排序3 Insertion or Heap Sort (25 分)
原文:https://www.cnblogs.com/acoccus/p/10935734.html