排序算法:堆排序

在没有学习堆排序算法的时候,将堆排序理解成了二叉排序树。

堆的定义为:二叉树中任意具有子节点的节点,它的两个子节点的元素值都不大于(或不小于)当前节点的元素值,称为大顶堆(或小顶堆);

那么堆排序算法的基本原理是每次构造一个小顶堆,那么堆的根节点就是序列的最小值,然后利用剩余的元素再构造一个小顶堆,获得第二个较小的元素,递归运行直到剩余1个元素返回,最终将得到升序排列的序列。


C++代码实现:

#include <iostream>
#include <vector>

using namespace std;

template<typename T>
void HeapSort(T* vec, int VSize);

int main()
{
	int att[] = { 10, 4, 23, 46, 20, 5, 3, 88, 8, 44, 53, 25, 86, 32, 16, 11, 100, 42, 17 };
	int VSize = sizeof(att) / sizeof(int);
	HeapSort(att, VSize);

	for (int vIdx = 0; vIdx < VSize; vIdx++)
		cout << "vIdx = " << vIdx << " value = " << att[vIdx] << endl;
 
	return 0;
}


template<typename T>
void HeapSort(T* vec, int VSize)
{
	if (VSize <= 1)
		return;

	// Create Heap
	for (int vIdx = 0; vIdx < VSize/2; vIdx++)
	{
		bool upgrade = false;
		if ((vIdx * 2 + 1 < VSize) && (vec[vIdx] > vec[vIdx * 2 + 1]))
		{
			vec[vIdx] ^= vec[vIdx * 2 + 1];
			vec[vIdx * 2 + 1] ^= vec[vIdx];
			vec[vIdx] ^= vec[vIdx * 2 + 1];
			upgrade = true;
		}

		int tmpIdx = vIdx;
		while (upgrade)
		{
			upgrade = false;
			if (tmpIdx > 0)
			{
				if (vec[tmpIdx] < vec[tmpIdx / 2])
				{
					vec[tmpIdx] ^= vec[tmpIdx / 2];
					vec[tmpIdx / 2] ^= vec[tmpIdx];
					vec[tmpIdx] ^= vec[tmpIdx / 2];
					upgrade = true;
					tmpIdx /= 2;
				}
			}
		}

		if ((vIdx * 2 + 2 < VSize) && (vec[vIdx] > vec[vIdx * 2 + 2]))
		{
			vec[vIdx] ^= vec[vIdx * 2 + 2];
			vec[vIdx * 2 + 2] ^= vec[vIdx];
			vec[vIdx] ^= vec[vIdx * 2 + 2];
			upgrade = true;
		}

		tmpIdx = vIdx;
		while (upgrade)
		{
			upgrade = false;
			if (tmpIdx > 0)
			{
				if (vec[tmpIdx] < vec[tmpIdx / 2])
				{
					vec[tmpIdx] ^= vec[tmpIdx / 2];
					vec[tmpIdx / 2] ^= vec[tmpIdx];
					vec[tmpIdx] ^= vec[tmpIdx / 2];
					upgrade = true;
					tmpIdx /= 2;
				}
			}
		}
	}

	HeapSort(vec + 1, VSize - 1);

	//for (int vIdx = 0; vIdx < VSize; vIdx++)
	//{
	//	cout << "vIdx = " << vIdx << " value = " << vec[vIdx] << endl;
	//}

	return;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 C++ 实现的两种排序算法,选择排序和快速排序。建立一个包含10000个随机整数的数组,然后对其进行排序,最后输出排序结果。 选择排序: ```cpp #include <iostream> #include <cstdlib> #include <ctime> using namespace std; void selectionSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { int min_idx = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[min_idx]) { min_idx = j; } } swap(arr[i], arr[min_idx]); } } int main() { const int n = 10000; int arr[n]; srand(time(NULL)); for (int i = 0; i < n; i++) { arr[i] = rand() % 1000; } selectionSort(arr, n); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; } ``` 快速排序: ```cpp #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; swap(arr[i], arr[j]); } } swap(arr[i + 1], arr[high]); return i + 1; } void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } int main() { const int n = 10000; int arr[n]; srand(time(NULL)); for (int i = 0; i < n; i++) { arr[i] = rand() % 1000; } quickSort(arr, 0, n - 1); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; } ``` 注意:以上代码是对随机生成的整数进行排序,如果是对其他类型的数据进行排序,则需要修改代码中的数据类型和比较方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值