pat1098. Insertion or Heap Sort
来源:互联网 发布:软件渠道代理 编辑:程序博客网 时间:2024/05/16 08:54
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:
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
思路;模拟插入排序和堆排序,注意插入排序下一个序列可能相同,要一直循环到不相同为止(PS:STL用久了写个插入排序都压力山大)
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn=105;int n;int a[maxn],b[maxn],c[maxn];void print(){ printf("%d",a[0]); for(int i=1;i<n;i++){ printf(" %d",a[i]); } printf("\n");}bool is_sorted(){ for(int i=0;i<n-1;i++){ if(a[i]<a[i+1]) return false; } return true;}bool judge(){ for(int i=0;i<n;i++){ if(a[i]!=b[i]) return false; } return true;}bool insert_sort(){ if(is_sorted()) return true; bool ok=false; int j,x; for(int i=1;i<n;i++){ ok=judge(); x=a[i]; for(j=i;j>0&&x<a[j-1];j--){ a[j]=a[j-1]; } a[j]=x; if(ok&&(!judge())) return true;//下一次不能和这次相同 } return false;}//下次试试make_heap和pop_heap这两个标准库函数void build_Heap(){ int sz=n-1,pos=(n-1)/2; while(sz>=0){ if(judge()) break; int x=(sz-1)/2; sz--; while(true){ int lc=2*x+1,rc=2*x+2; if(lc>=n) break; //注意左右子树条件 else if((a[lc]>a[x])&&(rc>=n||a[lc]>a[rc])){ swap(a[lc],a[x]); x=lc; } else if(rc<n&&a[rc]>a[x]){ swap(a[rc],a[x]); x=rc; } else{ break; } } }}void solve(int sz){ if(!sz) return; swap(a[0],a[sz]); int x=0; while(x<sz){ int lc=2*x+1,rc=2*x+2; if(lc>=sz) break; else if(a[lc]>a[x]&&(rc>=sz||a[lc]>a[rc])){ swap(a[lc],a[x]); x=lc; } else if(rc<sz&&a[rc]>a[x]){ swap(a[rc],a[x]); x=rc; } else{ break; } }}void Heap_sort(){ build_Heap(); bool ok=false; int sz=n-1; while(!ok&&sz>=0){ ok=judge(); solve(sz); //printf("sz==%d\n",sz); //print(); sz--; } print();}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d",&n)==1){ for(int i=0;i<n;i++){ scanf("%d",&a[i]); } for(int i=0;i<n;i++){ scanf("%d",&b[i]); } memcpy(c,a,sizeof(a)); if(insert_sort()){ printf("Insertion Sort\n"); print(); } else{ memcpy(a,c,sizeof(c)); printf("Heap Sort\n"); Heap_sort(); } } return 0;}
- PAT1098 Insertion or Heap Sort
- pat1098. Insertion or Heap Sort
- 1098.Insertion or Heap Sort
- 1098. Insertion or Heap Sort
- 1098 Insertion or Heap Sort
- 1098. Insertion or Heap Sort
- Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort
- 1098. Insertion or Heap Sort
- 1098. Insertion or Heap Sort
- PAT-Insertion or Heap Sort
- PAT 1098. Insertion or Heap Sort
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- PAT 1098. Insertion or Heap Sort (25)
- pat 1098 Insertion or Heap Sort
- 09-排序3 Insertion or Heap Sort
- MySQL锁解决并发问题详解
- 微信里的隐藏功能
- Xib使用之TableViewCell.xib中创建多个Cell
- 主成分分析PCA
- Java在linux下抓取12306车票信息
- pat1098. Insertion or Heap Sort
- Kubernetes 容器之rkt
- 进程间的通信方式之无名管道
- codeforces #Round 368 div2-C(勾股数)
- caffe.proto 源码解析
- epoll简单注释
- mysql中间件研究(Atlas,cobar,TDDL、sharding-jdbc)
- JavaScript toFixed() 方法
- 利用 LeakCanary 来检查 Android 内存泄漏