您的位置:首页 > 其它

c编写 Insertion or Heap Sort

2017-02-25 12:13 204 查看
Insertion or Heap Sort   (9分)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 insertsit 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 thana 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 NN (\le100≤100).Then in the next line, NN integersare given as the initial sequence. The last line contains the partially sorted sequence of the NN 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 thatthe 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
要注意的是N=1的时候只能是堆排
排序过程中存在未改变的状态,要输出的是最后改变的那一步后的结果:
如:4
1 2 4 3
1 2 4 3
要输出的是1 2 3 4
代码如下:
#include<stdio.h>int N;int flag=0;int input[1000];int insert_sort[1000];int heap_sort[1000];void checkinsert();void checkheap();void percdown(int i,int n);void output(int out[]);int cmp(int a[],int b[]);int main(void){int i=0,num=0;scanf("%d",&N);for(i=0;i<N;i++){scanf("%d",&num);insert_sort[i]=heap_sort[i]=num;}for(i=0;i<N;i++){scanf("%d",&num);input[i]=num;}if(N==1){printf("Heap Sort\n");printf("%d",num);}else{checkinsert();if(flag==1){output(insert_sort);}else{checkheap();output(heap_sort);}}return 0;}int cmp(int a[],int b[]){int i=0;for(i=0;i<N;i++){if(a[i]!=b[i])return 1;}return 0;}void checkinsert(){int j,p;int tmp;for(p=1;p<N;p++){tmp=insert_sort[p];for(j=p;j>0&&insert_sort[j-1]>tmp;j--){insert_sort[j]=insert_sort[j-1];}insert_sort[j]=tmp;if(flag==1){if(cmp(insert_sort,input)==1)return;}if(flag==0&&cmp(insert_sort,input)==0){printf("Insertion Sort\n");flag=1;}}}void checkheap(){int i;int tmp;for(i=N/2;i>=0;i--){percdown(i,N);}if(cmp(heap_sort,input)==0){printf("Heap Sort\n");flag=1;}for(i=N-1;i>0;i--){tmp=heap_sort[0];heap_sort[0]=heap_sort[i];heap_sort[i]=tmp;percdown(0,i);if(flag==1){if(cmp(heap_sort,input)==1)return;}if(flag==0&&cmp(heap_sort,input)==0){printf("Heap Sort\n");flag=1;}}}void percdown(int i,int n){int child;int tmp;for(tmp=heap_sort[i];i*2+1<n;i=child){child=i*2+1;if(child<n-1&&heap_sort[child+1]>heap_sort[child])child++;if(tmp<heap_sort[child])heap_sort[i]=heap_sort[child];else break;}heap_sort[i]=tmp;}void output(int out[]){int i=0;for(i=0;i<N;i++){if(i==0){printf("%d",out[i]);}else{printf(" %d",out[i]);}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: