非递归最大堆
2016-03-04 21:41
281 查看
#include <algorithm>
#include <windows.h> // 用来计时 GetTickCount函数
#include <cstdlib>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <stdlib.h>
using namespace std;
/*
注意:发现有问题要马上纠正,否则往后看就会忘记了。
*/
// 维护堆的性质
void heapAdjust(int* p, int size, int i)
{ //本段代码,先取出父子之间最大的数的下标,
//从[0]开始取而非[1]
//然后判断该下标是不是父亲的下标,若是,
int lagest;
//临时变量,保存最大下标
while (i <= (size - 1) / 2)
{
int left = i * 2 + 1;//左子下标
//说明父亲最大,不是,说明子最大,子最大
int right = i * 2 + 2;//右子下标
//就要交换,同时
//if (i <= (size - 1) / 2)
//*size - 1容易错误的地方,如果i是叶子的下标,就不用交换,因为他没有子树
//{
// 24-36把父子之间最大值得下标找出来
if ((left < size) && (p[left] > p[i]))
// left < size 注意 < 不取等号,因为size是数组大小
lagest = left;
//父与左子比较大小
else
lagest = i;
if ((right < size) && (p[right] > p[lagest])) //***********一直的错误所在
lagest = right;
//**************************
if (lagest != i)
// 父子之间交换值,使父亲的值最大
{
swap(p[i], p[lagest]);
//heapAdjust(p, size, lagest); // 交换后要使子树满足最大堆的性质
i = lagest;
continue;
}
else
break;
//}
}
}
// 创建最大堆
void buildHeap(int *p, int size)
{
for (int i = (size - 1) / 2; i >= 0; i--)
// 从最后一个叶子的父亲开始递减,i 可以取到 0
{ // 调整每个子树的大小性质
heapAdjust(p, size, i);
}
}
// 排序堆,升序
void heapSort(int *p, int size)
{
buildHeap(p, size);
for (int i = size - 1; i > 0; i--)
//* i 要不能取等号 =,i要降到1
{ //* 将剩下的i-1个数重新调整为最大堆,
// 也可以利用buildHeap()重新创建 ,
swap(p[0], p[i]);
// 但是由于只有第一和最后一个交换,最大的放最后
heapAdjust(p, i, 0); //*********这里应该是i而不是i-1
// 并不影响其他数据,直接将根节点
// 重新调整一下就行
}
}
inline void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
//****************************************
DWORD start_time, end_time;
unsigned int total_1, total_2;
//**************主程序插入下***************
typedef unsigned int intdata;
//const unsigned int LENGTH = 10000;
//int A[LENGTH];
DWORD LENGTH = 100000000;
int *A = NULL;
A = (int *)malloc(LENGTH * sizeof(int));
if (NULL == A)
{
free(A);
printf("内存分配失败,程序退出!\n");
system("pause");
return -1;
}
srand((unsigned)time(0));
start_time = GetTickCount();
for (intdata i = 0; i < LENGTH; i++)
{
*(A + i) = rand();
}
end_time = GetTickCount();
total_1 = end_time - start_time;
start_time = GetTickCount();
heapSort(A, LENGTH);
end_time = GetTickCount();
total_2 = end_time - start_time;
cout << endl << "生成随机数耗时:" << total_1 << "毫秒" << endl;
cout << "堆排序时间为:" << total_2 << "毫秒!" << endl;
//**************主程序插入上***************
free(A);
for (int i = 0; i < 10; i++)
{
cout << endl;
}
system("pause");
return 0;
}
#include <windows.h> // 用来计时 GetTickCount函数
#include <cstdlib>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <stdlib.h>
using namespace std;
/*
注意:发现有问题要马上纠正,否则往后看就会忘记了。
*/
// 维护堆的性质
void heapAdjust(int* p, int size, int i)
{ //本段代码,先取出父子之间最大的数的下标,
//从[0]开始取而非[1]
//然后判断该下标是不是父亲的下标,若是,
int lagest;
//临时变量,保存最大下标
while (i <= (size - 1) / 2)
{
int left = i * 2 + 1;//左子下标
//说明父亲最大,不是,说明子最大,子最大
int right = i * 2 + 2;//右子下标
//就要交换,同时
//if (i <= (size - 1) / 2)
//*size - 1容易错误的地方,如果i是叶子的下标,就不用交换,因为他没有子树
//{
// 24-36把父子之间最大值得下标找出来
if ((left < size) && (p[left] > p[i]))
// left < size 注意 < 不取等号,因为size是数组大小
lagest = left;
//父与左子比较大小
else
lagest = i;
if ((right < size) && (p[right] > p[lagest])) //***********一直的错误所在
lagest = right;
//**************************
if (lagest != i)
// 父子之间交换值,使父亲的值最大
{
swap(p[i], p[lagest]);
//heapAdjust(p, size, lagest); // 交换后要使子树满足最大堆的性质
i = lagest;
continue;
}
else
break;
//}
}
}
// 创建最大堆
void buildHeap(int *p, int size)
{
for (int i = (size - 1) / 2; i >= 0; i--)
// 从最后一个叶子的父亲开始递减,i 可以取到 0
{ // 调整每个子树的大小性质
heapAdjust(p, size, i);
}
}
// 排序堆,升序
void heapSort(int *p, int size)
{
buildHeap(p, size);
for (int i = size - 1; i > 0; i--)
//* i 要不能取等号 =,i要降到1
{ //* 将剩下的i-1个数重新调整为最大堆,
// 也可以利用buildHeap()重新创建 ,
swap(p[0], p[i]);
// 但是由于只有第一和最后一个交换,最大的放最后
heapAdjust(p, i, 0); //*********这里应该是i而不是i-1
// 并不影响其他数据,直接将根节点
// 重新调整一下就行
}
}
inline void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
//****************************************
DWORD start_time, end_time;
unsigned int total_1, total_2;
//**************主程序插入下***************
typedef unsigned int intdata;
//const unsigned int LENGTH = 10000;
//int A[LENGTH];
DWORD LENGTH = 100000000;
int *A = NULL;
A = (int *)malloc(LENGTH * sizeof(int));
if (NULL == A)
{
free(A);
printf("内存分配失败,程序退出!\n");
system("pause");
return -1;
}
srand((unsigned)time(0));
start_time = GetTickCount();
for (intdata i = 0; i < LENGTH; i++)
{
*(A + i) = rand();
}
end_time = GetTickCount();
total_1 = end_time - start_time;
start_time = GetTickCount();
heapSort(A, LENGTH);
end_time = GetTickCount();
total_2 = end_time - start_time;
cout << endl << "生成随机数耗时:" << total_1 << "毫秒" << endl;
cout << "堆排序时间为:" << total_2 << "毫秒!" << endl;
//**************主程序插入上***************
free(A);
for (int i = 0; i < 10; i++)
{
cout << endl;
}
system("pause");
return 0;
}
相关文章推荐
- 第一周作业
- java.lang.NoClassDefFoundError 怎么解决
- Assignment to name ignored, since the identifier is never used
- 快速地搜索手机文件引擎(JavaIO的应用)
- uva10905连接n个数字,使得连成的数字最大
- RAC 和 Oracle Clusterware 最佳实践和初学者指南 (Solaris) (文档 ID 1526517.1)
- 寒假刷题-最小生成树
- IOS 开发之 界面传值
- maven 父子项目 聚合与继承 生命周期 打包插件 依赖打包 jar pom war
- react学习札记(二)
- 上传文件
- CSS制作水平垂直居中对齐 多种方式各有千秋
- Eclipse快捷键
- Floating Action Button-Android M新控件
- 1355: [Baltic2009]Radio Transmission|Kmp
- leetcode-Lowest Common Ancestor of a Binary Search Tree
- Linux日常运维管理命令
- iOS陆先森开发随笔(四)tablview点击push到下一个页面,pop出来时位置下移问题
- ZOJ 2770 Burn the Linked Camp【差分约束 SPFA】
- OSWatcher Analyzer User Guide (文档 ID 461053.1)