📄 heapsort.cpp
字号:
// heapsort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
void adjust_heap(int *heap,int root,int len)
{
int done;
int temp;
int j;
j=2*root;
temp=heap[root];
done=0;
while (j<=len&&!done)
{
if(j<len)
if(heap[j]<heap[j+1])
j++;
if(temp>=heap[j])
done=1;
else
{
heap[j/2]=heap[j];
j=2*j;
}
}
heap[j/2]=temp;
}
void heap(int *heap,int len)
{
int temp;
for(int i=(len/2);i>=1;i--)
adjust_heap(heap,i,len);
printf("\n堆的内容:");
for(int j=1;j<10;j++)
printf("[%d]",heap[j]);
printf("\n");
for (i=len-1;i>=1;i--)
{
temp=heap[i+1];
heap[i+1]=heap[1];
heap[1]=temp;
adjust_heap(heap,1,i);
printf("\n处理内容:");
for(j=1;j<10;j++)
printf("[%d]",heap[j]);
}
}
int main(int argc, char* argv[])
{ int data[10]={0,5,6,4,8,2,3,7,1,9};
printf("\n二叉树的内容:");
for(int j=1;j<10;j++)
printf("[%d]",data[j]);
heap(data,9);
printf("\n\n输出排序结果:");
for(j=1;j<10;j++)
printf("[%d]",data[j]);
printf("\n");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -