数据结构堆(代码ampampamp测试结果)
#include "pch.h"
#include
#include
#include
#include
using namespace std;
void pushHeap(vector& v)
{
int currentPos, parentPos;
int target, last = v.size();
currentPos = last - 1;
parentPos = (currentPos - 1) / 2;
target = v[last - 1];
while (currentPos != 0)
{
if (target > v[parentPos])
{
v[currentPos] = v[parentPos];
currentPos = parentPos;
parentPos = (currentPos - 1) / 2;
}
else break;
}
v[currentPos] = target;
}
void pushHeap2(vector& v)
{
int currentPos, parentPos;
int target, last = v.size();
currentPos = last - 1;
parentPos = (currentPos - 1) / 2;
target = v[last - 1];
while (currentPos != 0)
{
if (target < v[parentPos])
{
v[currentPos] = v[parentPos];
currentPos = parentPos;
parentPos = (currentPos - 1) / 2;
}
else break;
}
v[currentPos] = target;
}
bool Compare(int a1,int a2)
{
if (a1 > a2)
{
return true;
}
else
{
return false;
}
}
bool Compare2(int a1, int a2)
{
if (a1 < a2)
{
return true;
}
else
{
return false;
}
}
void adjustHeap1(vector& v, int first, int last)
{
int currentPos, childPos;
int target;
currentPos = first;
target = v[first];
childPos = 2 * currentPos + 1;
while (childPos <= last - 1)
{
if ((childPos + 1 <= last - 1) && Compare(v[childPos + 1], v[childPos]))
childPos = childPos + 1;
if (Compare(v[childPos], target))
{
v[currentPos] = v[childPos];
currentPos = childPos;
childPos = 2 * currentPos + 1;
}
else break;
}
v[currentPos] = target;
}
void adjustHeap2(vector& v, int first, int last)
{
int currentPos, childPos;
int target;
currentPos = first;
target = v[first];
childPos = 2 * currentPos +1;
while (childPos <= last - 1)
{
if ((childPos + 1 <= last - 1) && Compare2(v[childPos + 1], v[childPos]))
childPos = childPos + 1;
if (Compare2(v[childPos], target))
{
v[currentPos] = v[childPos];
currentPos = childPos;
childPos = 2 * currentPos + 1;
}
//if ((childPos + 1 >= last - 1) && Compare2(v[childPos + 1], v[childPos]))
//childPos = childPos + 1;
else break;
}
v[currentPos] = target;
}
void popHeap1(vector& v)
{
if (v.size() <= 0)
{
return;
}
int size = v.size();
swap(v[0], v[size - 1]);
adjustHeap1(v, 0, size -1);
v.pop_back();
}
void popHeap2(vector& v)
{
if (v.size() <= 0)
{
return;
}
int size = v.size();
swap(v[0], v[size - 1]);
adjustHeap2(v, 0, size - 1);
v.pop_back();
}
int main()
{
vectorv;
srand(int(time(0)));
for (int i = 0; i < 10; i++)
{
v.push_back(rand()%100);
pushHeap2(v);
}
cout << "最小堆展示:" << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
cout << "删除掉一个元素后的最小堆:" << endl;
popHeap2(v);
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << " -----------------------------------------------" << endl;
vectorv2;
for (int i = 0; i < 1000000; i++)
{
v2.push_back(rand());
pushHeap2(v2);
if (v2.size() > 10)
{
popHeap2(v2);
}
}
cout << "一百万个数据最小堆的结果:" << endl;
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] <<" ";
}
cout << endl;
}
测试结果
苏炳添全亚洲跑的最快的教授,用自己的体育生涯书写人生的辉煌虽然今年的奥运会东道主日本气势汹汹,生硬的从中国人手里夺走该有的荣誉,但是中国奥运团队依然不断奋勇前进,不断创造纪录,就在昨天刚刚举行的东京奥运会男子100米半决赛上,苏炳添以个人
毛坦厂中学复读很累,读书很苦,人生并没有容易的路8月的天,骄阳烤热了这个城市,在不大的教室里却驻扎了一帮拼命学习的孩子,他们是来自今年高考的学生,因不满足自己的成绩而踏上了高考复读之路,复读这一年,很多孩子比同龄人多付出一年的青
广西考生以556分捡漏上海交通大学,这份运气是如何得来的?高考已经结束,8月份也即将迎来大学入学高潮,在七月份上大学的很多孩子都收到了大学录取通知书,不管是名牌大学也好三流大学也罢,每个人也要走上属于自己的人生道路,进入大学就是进入了一个
美文赏析挖笋人作者明前茶来源意林全彩版2021年7月正月一过,60岁的农妇祁妈妈迎来了忙碌的季节安吉县承包竹园的人家,都会雇佣她这种有经验的挖笋人。祁妈妈是这一带的春笋猎手。她只要攀上山腰,抬眼
大脑愚蠢吗作者贝小戎来源意林原创版2021年07期计算机刚出现时,为了理解其特性,我们把它比作电脑。但随着电脑的普及,我们又反过来把人脑比作电脑。专家认为这个比喻是有问题的。自然杂志一篇文章
美文赏析鱼头火锅作者小云猫猫来源意林全彩版2021年7月鱼里面,我最爱的是鱼头。最爱的吃法是鱼头火锅。记忆里第一次对鱼头火锅有印象还是很小的时候。我姨妈家在水乡,家家户户有鱼塘,鱼塘上搭着葡萄架,
美文赏析桂花藕作者黄磊来源意林原创版2021年07期江南风味,源于草木之香。香味是可以食用的,入味的香如同吃下一缕诗意。玫瑰露鲜竹汁草莓酱茅根香,再到菜肴的调味葱姜蒜酱。自然的馈赠经人为的料理,
养一朵雪花作者石兵来源意林全彩版2021年7月少年时,我曾悄悄养了一朵雪花。如今想来,这是一件多么不可思议的事,可在当年,我竟真的做到了。那年冬天,雪下得特别多特别大,让我萌生了养一朵雪花的
美文赏析错位之思作者凸凹来源意林杂志2021年6月上记忆里,母亲有一双美丽的手,却不善女红。纳鞋底时,常把针尖扎到自己的手上,但她依然要勤勉地纳,因为有三个顽皮小儿等鞋穿,待手艺渐渐娴熟起来,她的
杨开慧写给毛主席的情书,去世50年后才被人发现这是一封永远寄不出去的信。写给一个永远也收不到的人。信纸已经泛黄发脆,写信的人,用娟秀的小楷右列竖写,由于年代久远,有些字迹已模糊不清。在信中,写信者倾诉着自己的痛苦挣扎对丈夫的热
性格测试之二经典隶书名作,您最喜欢哪个,测测您的性格特点隶书,亦称汉隶,是汉字中常见的一种庄重的字体,书写效果略微宽扁,横画长而直画短,呈长方形状,讲究蚕头雁尾一波三折。隶书起源于秦朝,由程邈规制而成,在东汉时期达到顶峰,书法界有汉唐风