永发信息网

谁能帮我看看我的堆排序~~~!

答案:1  悬赏:40  手机版
解决时间 2021-01-13 11:18
谁能帮我看看我的堆排序~~~!
最佳答案
我看了一遍你的程序,感觉堆的数据结构你没有体现出来。
所以我找了一个我们课本上的源程序,你看看行不行。

#include
#include

template
class MaxHeap {
public:
MaxHeap(int MaxHeapSize = 10);
~MaxHeap() {delete [] heap;}
int Size() const {return CurrentSize;}
T Max() {if (CurrentSize == 0)
throw OutOfBounds();
return heap[1];}
MaxHeap& Insert(const T& x);
MaxHeap& DeleteMax(T& x);
void Initialize(T a[], int size, int ArraySize);
void Deactivate() {heap = 0;}
void Output() const;
private:
int CurrentSize, MaxSize;
T *heap; // element array
};

template
MaxHeap::MaxHeap(int MaxHeapSize)
{// Max heap constructor.
MaxSize = MaxHeapSize;
heap = new T[MaxSize+1];
CurrentSize = 0;
}

template
MaxHeap& MaxHeap::Insert(const T& x)
{// Insert x into the max heap.
// find place for x
// i starts at new leaf and moves up tree
int i = ++CurrentSize;
while (i != 1 && x > heap[i/2]) {
// cannot put x in heap[i]
heap[i] = heap[i/2]; // move element down
i /= 2; // move to parent
}

heap[i] = x;
return *this;
}

template
MaxHeap& MaxHeap::DeleteMax(T& x)
{// Set x to max element and delete
// max element from heap.
// check if heap is empty
if (CurrentSize == 0)
return *this; // empty

x = heap[1]; // max element

// restucture heap
T y = heap[CurrentSize--]; // last element

// find place for y starting at root
int i = 1, // current node of heap
ci = 2; // child of i
while (ci <= CurrentSize) {
// heap[ci] should be larger child of i
if (ci < CurrentSize &&
heap[ci] < heap[ci+1]) ci++;

// can we put y in heap[i]?
if (y >= heap[ci]) break; // yes

// no
heap[i] = heap[ci]; // move child up
i = ci; // move down a level
ci *= 2;
}
heap[i] = y;

return *this;
}

template
void MaxHeap::Initialize(T a[], int size,
int ArraySize)
{// Initialize max heap to array a.
delete [] heap;
heap = a;
CurrentSize = size;
MaxSize = ArraySize;

// make into a max heap
for (int i = CurrentSize/2; i >= 1; i--) {
T y = heap[i]; // root of subtree

// find place to put y
int c = 2*i; // parent of c is target
// location for y
while (c <= CurrentSize) {
// heap[c] should be larger sibling
if (c < CurrentSize &&
heap[c] < heap[c+1]) c++;

// can we put y in heap[c/2]?
if (y >= heap[c]) break; // yes

// no
heap[c/2] = heap[c]; // move child up
c *= 2; // move down a level
}
heap[c/2] = y;
}
}

template
void MaxHeap::Output() const
{
cout << "The " << CurrentSize
<< " elements are"<< endl;
for (int i = 1; i <= CurrentSize; i++)
cout << heap[i] << ' ';
cout << endl;
}

template
void HeapSort(T a[], int n)
{// Sort a[1:n] using the heap sort method.
// create a max heap of the elements
MaxHeap H(1);
H.Initialize(a,n,n);

// extract one by one from the max heap
T x;
for (int i = n-1; i >= 1; i--) {
H.DeleteMax(x);
a[i+1] = x;
}

// save array a from heap destructor
H.Deactivate();
}

void main(void)
{
int a[11], i, n = 10;
// 测试数据
for (i = 1; i <= 10; i++)
a[i] = n - i + 1;

HeapSort(a,10);

// 输出结果
for (i = 1; i <= n; i++)
cout << a[i] << ' ';
cout << endl;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
qq打不开怎么办
我的海信电视看4k怎么黑屏
初犬全集下载的BT种子
留下到乔司三角村石塘一路38号的公交车路线
人称超级别墅的深圳天琴湾别墅是不是很高端的
名字中有理字的名人有哪些?
我de轮胎195-50-15 可以换成 195-60-15 的吗?
长安山麓它都有什么优势啊?好在哪里?
六年级下册阅读材的成语
人类大脑究竟能记住多少东西
mysql如何做读写分离啊 ?
tdm-gcc和mingw的gcc有什么区别? wxWidgets提
K624次列车容易晚点吗?谁坐过这趟车啊
某市去年人口为50万,今年因各种原因死亡1000
阿根廷有什么民族
推荐资讯
大郊亭 公务员小区最东边距大郊亭桥有多远
工作调离申请书怎么写?
故意盗窃工程沙石可以拘留我工地在村里存放沙
红菌菇有人种吗?
谁有剑与家园野区boss分布地
— Have you heard of the story of a bowl o
单选题Pleasegivemethree________oftea
怎样能用一个手指将篮球顶起来并让其旋转?
DOTA沙王
一个男生发信息说 我们打一架吧 是什么意思(&
P2 水泥是什么水泥
求师父指点这是变频塔吊吗?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?