09-排序3 Insertion or Heap Sort

和前一题差不多,把归并排序换成了堆排序。要点还是每一次排序进行判断

开始犯了个错误 堆排序该用origin2 结果一直在排序origin ,误导了半天以为是逻辑错误。。。一直在检查逻辑

建立最大堆

排序并调整下滤

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, NN 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:

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 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 using namespace std;
 5
 6 typedef int ElementType;
 7 bool Judge(int origin[], int changed[], int len)
 8 {
 9     for(int i = 0; i < len; i++)  {
10         if(origin[i] != changed[i])
11             return false;
12     }
13     return true;
14 }
15
16 //插入排序 (需排序数组,数组长度,排序次数)
17 void isInsertionSort(ElementType origin[], int N, int times)
18 {
19     int i;
20     ElementType temp = origin[times];     //取出未排序序列中的第一个元素
21     for (i = times; i > 0 && origin[i-1] > temp; i-- )
22         origin[i] = origin[i-1];             //依次与已排序序列中元素比较并右移
23     origin[i] = temp;
24 }
25
26 void Swap( ElementType *a, ElementType *b )
27 {
28     ElementType t = *a;
29     *a = *b;
30     *b = t;
31 }
32 /* 改编PercDown( MaxHeap H, int p )*/
33 void PercDown( ElementType A[], int p, int N )
34 {
35   /* 将N个元素的数组中以A[p]为根的子堆调整为最大堆 */
36     int Parent, Child;
37
38     ElementType X = A[p]; /* 取出根结点存放的值 */
39     for( Parent=p; (Parent*2+1) < N; Parent=Child ) {
40         Child = Parent * 2 + 1;
41         if( (Child != N-1) && (A[Child] < A[Child+1]) )
42             Child++;  /* Child指向左右子结点的较大者 */
43         if( X >= A[Child] ) break; /* 找到了合适位置 */
44         else  /* 下滤X */
45             A[Parent] = A[Child];
46     }
47     A[Parent] = X;
48 }
49
50 void HeapSort(ElementType A[], int N, int changed[])
51 {
52      for(int i = N/2-1; i >= 0; i--) /* 建立最大堆 */
53          PercDown( A, i, N );
54
55      for(int i = N-1; i > 0; i--) {
56          /* 删除最大堆顶 */
57          Swap(&A[0], &A[i] );
58          PercDown(A, 0, i);
59
60          if( Judge(A, changed, N) ) {    //是堆排序
61              printf("Heap Sort\n");
62
63              Swap(&A[0], &A[i-1] );         //在执行一次堆排序
64              PercDown(A, 0, i-1);
65              for(int j = 0; j < N-1; j++)
66                  printf("%d ",A[j]);
67              printf("%d\n",A[N-1]);
68              return;
69          }
70      }
71 }
72
73 int main()
74 {
75     int N;
76     int origin[105],origin2[105],changed[105];
77     scanf("%d", &N);
78     for(int i = 0; i < N; i++) {    //origin origin1 初始序列
79         scanf("%d",&origin[i]);
80         origin2[i] = origin[i];
81     }
82     for(int i = 0; i < N; i++)         //changed 排序后序列
83         scanf("%d",&changed[i]);
84
85     for(int i = 1; i < N; i++) {
86         isInsertionSort(origin, N, i);
87         if( Judge(origin, changed,N) ) {    //是插入排序
88             printf("Insertion Sort\n");
89             isInsertionSort(origin, N, i+1);
90             for(int j = 0; j < N-1; j++)
91                 printf("%d ",origin[j]);
92             printf("%d\n",origin[N-1]);
93             return 0;
94         }
95     }
96
97     HeapSort(origin2,N,changed);
98     return 0;
99 }
 
时间: 2024-11-01 13:37:24

09-排序3 Insertion or Heap Sort的相关文章

1098. Insertion or Heap Sort (25)【排序】——PAT (Advanced Level) Practise

题目信息 1098. Insertion or Heap Sort (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one el

pat1098. Insertion or Heap Sort (25)

1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, inse

PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 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 iterati

PTA Insertion or Heap Sort

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

PTA 09-排序3 Insertion or Heap Sort (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 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 s

PAT_A1098#Insertion or Heap Sort

Source: PAT_A1098 Insertion or Heap Sort (25 分) Description: 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 i

PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是,后面是从小到大排列的最大的几个数p~n-1,前面第一位则是p-1. 所以只要先按照插入排序的特点来判断是否为插入排序,接下来再进行操作就可以了,这里要手动写下最大堆的更新操作. 代码: #include <iostream> #include <cstdio> #include <

PAT-1098(Insertion Or Heap Sort)

题目链接见这里 分析:考察的是插入排序和堆排序两种基本的数据结构,注意利用两种排序的基本特征----插入排序不影响全局,而堆排序影响全局 #include <stdio.h> #define SWAP(a,b) a = b-a+(b=a) #define N 105 void Input(int *unSorted, int partialSorted[], int *n){ int i; scanf("%d",n); for(i=1;i<=*n;i++) scanf

1098 Insertion or Heap Sort

题意:给出两组数,第一组数为原始序列,第二组数为经过若干次插入排序或堆排序后的序列,判定到底是属于哪一种排序的结果,然后输出以这种排序的下一次迭代后的序列. 思路:就是练习插入排序和堆排序,没什么好说的.不过有一个小细节,我被扣了2分.见代码. 代码: #include <cstdio> #include <vector> #include <algorithm> using namespace std; vector<int> ori,data,temp;