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 (≤). 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 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.

Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10
3 1 2 8 7 5 9 4 6 0 6 4 5 1 0 3 2 7 8 9 

Sample Output 2:

Heap Sort
5 4 3 1 0 2 6 7 8 9

  1 #define _CRT_SECURE_NO_WARNINGS  
  2 #include<stdio.h>
  3 #include<malloc.h>
  4 #include<stdlib.h>
  5 int A[110];
  6 int B[110];
  7 int L;
  8 void Swap(int *C,int i, int j)
  9 {
 10     int temp = C[i];
 11     C[i] = C[j];
 12     C[j] = temp;
 13 }
 14 void Print(int N)
 15 {
 16     int i;
 17     for (i = 0; i < N - 1; i++)
 18         printf("%d ", B[i]);
 19     printf("%d", B[i]);
 20 }
 21 
 22 int Charge(int N)
 23 {
 24     int i;
 25     if (B[0] > B[1] && B[0] > B[2])
 26         return 1;
 27     else
 28         return 0;
 29 }
 30 
 31 void Insert_Once(int i, int N)
 32 {
 33     int j;
 34     int Temp = B[i];
 35     for (j = i; j > 0 && B[j - 1] > Temp; j--)
 36         B[j] = B[j - 1];
 37     B[j] = Temp;
 38 }
 39 
 40 void PrecDown(int i,int N)
 41 {
 42     int Parent, Child;
 43     int Tmp = A[i];
 44     for (Parent = i; Parent * 2 + 1 <= N; Parent = Child)
 45     {
 46         Child = Parent * 2 + 1;
 47         if (Child != N && A[Child] < A[Child + 1])
 48             Child++;
 49         if (Tmp >= A[Child])break;
 50         else
 51             A[Parent] = A[Child];
 52     }
 53     A[Parent] = Tmp;
 54 }
 55 void BuildMaxHeap(int N)
 56 {
 57     for (int i = (N - 1) / 2; i >= 0; i--)
 58         PrecDown(i, N - 1);
 59 }
 60 void Heap_Sort(int N)
 61 {
 62     for (int i = N - 1; i >= 0; i--)
 63     {
 64         Swap(A, 0, i);
 65         PrecDown(0, i-1);
 66     }
 67 }
 68 int FindI(int N)
 69 {
 70     int i;
 71     for (i = N - 1; i >= 0 && A[i] == B[i]; i--);
 72     return i;
 73 }
 74 void PrecDown_Once(int i)
 75 {
 76     Swap(B,0, i);
 77     int Parent, Child;
 78     int tmp;
 79     tmp = B[0];
 80     for (Parent = 0; (Parent * 2) + 1 <= i-1; Parent = Child)
 81     {
 82         Child = (Parent * 2) + 1;
 83         if (Child != i-1&& B[Child]<B[Child + 1])
 84             Child++;
 85         if (tmp >= B[Child])break;
 86         else
 87             B[Parent] = B[Child];
 88     }
 89     B[Parent] = tmp;
 90 }
 91 void Heap_Once(int N)
 92 {
 93     BuildMaxHeap(N);
 94     Heap_Sort(N);
 95     int i = FindI(N);
 96     PrecDown_Once(i);
 97 }
 98 int main()
 99 {
100     int N;
101     int Flag = 0;
102     int OrderPosition = 0;
103     scanf("%d", &N);
104     for (int i = 0; i < N; i++)
105         scanf("%d", &A[i]);
106     for (int i = 0; i < N; i++)
107         scanf("%d", &B[i]);
108     if (Flag = Charge(N))
109         printf("Heap Sort\n");
110     else
111         printf("Insertion Sort\n");
112     for (int i = 0; i < N - 1; i++)
113         if (B[i] <= B[i + 1])     //这里必须是大于等于
114             OrderPosition = i + 1;
115         else
116             break;
117     if (Flag)
118         Heap_Once(N);
119     else
120         Insert_Once(OrderPosition + 1, N);
121     Print(N);
122     return 0;
123 }
View Code

猜你喜欢

转载自www.cnblogs.com/57one/p/12071453.html