heapsort1.cpp
来自「数据结构算法 vc++6.0 的程序集包含所有章节,适合学习数据结构,把数据结构」· C++ 代码 · 共 47 行
CPP
47 行
//利用最大堆相关操作进行堆排序heapsort1.cpp
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<conio.h>
typedef int ElemType;
#include "maxheap.cpp"
#include "linelist.cpp"
void PrintList(List *L)
{int i,n;
ElemType e;
n=L->ListLength();
for(i=0;i<n;i++)
cout<<setw(3)<<L->GetElem(i,&e);
cout<<endl;
}
void HeapSort(List *L)
{int i,n;
ElemType e;
n=L->ListLength();
maxheap H(n);
for(i=0;i<n;i++)
H.heapInsert(L->GetElem(i,&e));
L->ClearList();i=0;
while(!H.heapEmpty())
L->ListInsert(i++,H.heapDelete());
H.Destroyheap();
}
void main()
{cout<<"heapsort1.cpp运行结果:\n";
int i;
List L;
L.init(&L);
srand(350);
for(i=0;i<HeapSIZE;i++)
L.ListInsert(i,rand()%100);
cout<<"原表:\n";
PrintList(&L);
HeapSort(&L);
cout<<"排序后表:\n";
PrintList(&L);
L.DestroyList(L);
getch();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?