堆(一种完全二叉树)
-
大顶堆 :父亲节点的指大于等于孩子结点的值
-
小顶堆 :父亲节点的值小于等于孩子结点的值
用处:优先队列的实现 -
对于一个给定初始序列,调整为堆的方法:从下到上,从右到左,如果有调整,在新位置看看有没有其他调整。
//堆的数据结构实现(数组实现)
const int maxn=100;//最大结点数
int heap[maxn];
int n= 10;//n为当前用的节点个数,可以更改
//建堆的过程是向下调整,写的是大顶堆的
void downAdjust(int low,int high)//low为开始结点,high为结束结点
{
int i=low;/欲调整结点
int j=i*2;
while(j<=high)//孩子结点在范围内
{
//确认左孩子右孩子的大小
if(j+1<=high&&heap[j]<heap[j+1])
j=j+1;
if(heap[i]<heap[j])
{
swap(heap[i],heap[j]);//交换两个结点的值
//更新i,j为下次循环做准备
i=j;
j=2*i;
}
else
break;
}
}
//建堆,根据从下往上,从左往右的原则,所以倒着调整,非叶子结点的个数是n/2,从1开始为有效值
void create()
{
for(int i=n/2;i>=1;i--)
{
downAdjust(i,n);
}
}
//删除堆顶元素,将最后一个元素放到第一个,然后调整
void deleteTop()
{
heap[1]=heap[n--];
downAdjust(1,n);
}
//向上调整,用于添加一个元素
void upAdjust(int low,int high)
{
int i=high;//欲调整结点
int j=high/2;//该节点的父亲
while(j>=low)
{
if(heap[j]<heap[i])
{
swap(heap[i],heap[j]);//交换对应节点
//为下次循环调整i,j的值
i=j;
j=i/2;
}
else
break;
}
}
//添加元素x
void insert(int x)
{
heap[++n]=x;
upAdjust(1,n);
}
- 堆排序 : 利用根节点值最大的特性,我们交换根节点和最后一个节点,然后向上调整从1到n-1的范围。
void heapSort()
{
creatHeap();//建堆
for(int i=n;i>1;i--)
{
swap(heap[i],heap[1]);
downAdjust(1,i-1);
}
}
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 (≤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 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
AC代码:
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=110;
int heap[maxn];
int arr[maxn];
int backup[maxn];
int n;
/*记得判断给的序列是否为初始序列
如input:
3 4 2 1
3 4 2 1
正确output:
Insertion Sort
2 3 4 1*/
bool judge(int n)
{
for(int i=1;i<=n;i++)
{
if(arr[i]!=heap[i])
return false;
}
return true;
}
void downAdjust(int low,int high)
{
int i=low,j=2*i;
while(j<=high)
{
if(j+1<=high&&heap[j+1]>heap[j])
{
j=j+1;
}
if(heap[i]<heap[j])
{
swap(heap[i],heap[j]);
i=j;
j=2*i;
}
else
break;
}
}
void createHeap()
{
for(int i=n/2;i>=1;i--)
{
downAdjust(i,n);
}
}
int heapSort()
{
createHeap();
bool flag=false;
for(int i=n;i>1;i--)
{
if(judge(n)&&i!=n)
{
printf("Heap Sort\n");
flag=true;
}
swap(heap[1],heap[i]);
downAdjust(1,i-1);
if(flag)
return 1;
}
return 0;
}
int insertSort()
{
//默认第一个是最小的
bool flag_end=false;
for(int i=2;i<=n;i++)
{
if(judge(n)&&i!=2)
{
flag_end=true;
printf("Insertion Sort\n");
}
int j=i-1;
int now=heap[i];
int flag=0;
for(j=i-1;j>=1;j--)
{
if(now>heap[j])
{
flag=1;
break;
}
}
if(flag||j==0)
{
for(int k=i;k>j;k--)
{
heap[k]=heap[k-1];
}
heap[j+1]=now;
}
if(flag_end)
return 1;
}
return 0;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&heap[i]);
backup[i]=heap[i];
}
for(int i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
}
if(insertSort())
{
for(int i=1;i<n;i++)
printf("%d ",heap[i]);
printf("%d",heap[n]);
}
for(int i=1;i<=n;i++)
{
heap[i]=backup[i];
}
if(heapSort())
{
for(int i=1;i<n;i++)
printf("%d ",heap[i]);
printf("%d",heap[n]);
}
return 0;
}
来源:CSDN
作者:宣橙多
链接:https://blog.csdn.net/weixin_42183760/article/details/104138592