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("%d",&unSorted[i]);
	for(i=1;i<=*n;i++) scanf("%d",&partialSorted[i]);
}

void AdjustDown(int *partialSorted, int k, int n){
	//将元素k向下进行调整
	int i;
	partialSorted[0] = partialSorted[k]; //partialSorted[0]暂存
	for(i=2*k;i<=n;i*=2){ //沿key较大的子结点向下筛选
		if(i<n && partialSorted[i]<partialSorted[i+1])
			i ++; //取key值较大的子结点的下标
		if(partialSorted[0]>=partialSorted[i]) break; //筛选结束
		else{
			partialSorted[k] = partialSorted[i]; //将partialSorted[i]调整到双亲结点上
			k = i; //修改k值,以便继续向下筛选
		}
	}
	partialSorted[k] = partialSorted[0]; //被筛选结点的值放入最终的位置
} 

void BuildMaxHeap(int *partialSorted, int n){
	int i = n/2;
	for(;i>0;i--) //从i=[n/2]~1,反复调整堆
		AdjustDown(partialSorted,i,n);
}

int HeapSort(int *unSorted, int *partialSorted, int n){
	int index = n;
	for(;index>1 && partialSorted[index-1]<=partialSorted[index];index--);
	//进入下一次迭代
	BuildMaxHeap(partialSorted,index); //asending:大顶堆
	SWAP(partialSorted[index],partialSorted[1]); //输出堆顶元素(和堆底元素交换)
	AdjustDown(partialSorted,1,index-1); //整理,把剩余的index-2个元素整理成堆
	printf("Heap Sort\n");
}

int InsertionSort(int unSorted[], int partialSorted[], int n){
	int i,j,index,low,high,mid;
	for(i=1;i<n && partialSorted[i]<=partialSorted[i+1];i++);
	index = ++i;
	for(;i<=n && unSorted[i]==partialSorted[i];i++);
	if(i>n){
		//折半插入(稳定)
		partialSorted[0] = partialSorted[index]; //暂存partialSorted[index]
		low = 1, high = index-1;
		while(low<=high){
			mid = (low+high)/2; //取中间点
			if(partialSorted[mid]>partialSorted[0]) high = mid-1; //查找左半子表
			else low = mid+1; //查找右半子表
		}
		for(j=index-1;j>=high+1;j--)
			partialSorted[j+1] = partialSorted[j]; //统一后移元素,空出插入位置
		partialSorted[high+1] = partialSorted[0];
		printf("Insertion Sort\n");
		return 1;
	}
	return 0;
}

void Show(int partialSorted[], int n){
	int i;
	for(i=1;i<=n;i++){
		printf("%d",partialSorted[i]);
		if(i==n) printf("\n");
		else printf(" ");
	}
}

int main(){
	int unSorted[N],partialSorted[N];
	int n;
//	freopen("Data.txt","r",stdin); //段错误
	Input(unSorted,partialSorted,&n);
	//模拟来判断unSorted和partialSorted相等是非常愚蠢的行为
	//运用一下堆排序和插入排序的特征,处理问题更快捷
	//先做插入排序的判断,然后进行堆排序
	//插入排序不影响全局,而堆排序影响全局(亦即无法根据特征性质判断是否为堆排序)
	if(InsertionSort(unSorted,partialSorted,n));
	else HeapSort(unSorted,partialSorted,n);
	Show(partialSorted,n);
	return 0;
}
时间: 2024-08-06 17:41:44

PAT-1098(Insertion Or Heap Sort)的相关文章

PAT 1098 Insertion or Heap Sort (25)

According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted

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

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

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

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

PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=200; int a[maxn],b[m

【PAT甲级】1098 Insertion or Heap Sort (25 分)

题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[107],b[107]; 5 int c[107][5]; 6 int main(){ 7 ios:

1098 Insertion or Heap Sort

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

1098 Insertion or Heap Sort (25)

According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted

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 li

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