数据结构-选择排序-树形选择排序

选择排序

①简单选择排序。

②树形选择排序。←this

③堆排序。


树形选择排序(Tree Selection Sort),又称锦标赛排序(Tournament Sort)是一种按照锦标赛的思想进行选择排序的方法。

描述过程:首先对n个记录的关键字进行两两比较,然后在其中[n/2](取上界)个较大(小)者之间再进行两两比较,选出最大(小)关键字的记录为止。取出最大(小)关键字,将其设为INF,重复上面,选出次大(小)关键字。(咋感觉这么像线段树呢。。。)

时间复杂度为O(nlogn)

                             10

             10                              9

      0              10                       6

  0      0      0     10     9      8     3    

0  0  0  0  0  10  4  7  9  8 1  2    5(←倒着的原始数组)

输出10后

                             9

             0                              9

      0              0                        6

  0      0      0     0      9      8      3    

0  0  0  0  0  0  0  4  7  9   1  2    5

输出10,9后

                             8

             0                              8

      0              0              8           6

  0      0      0     0      0      8      3    

0  0  0  0  0  0  0  4  7  0   1  2    5

下面代码用的是每次找出最大值

#include <cstdio>  
#include <cstring>  
#include <iostream>  
using namespace std;  
const int N=10;
void print(int *tree){
	int altm=0;
	for(int j=1;j<6;j++){
		int tm=1;
		for(int i=1;i<j;i++)
			tm*=2;
		for(int i=1;i<=tm;++i)
		{
			printf("%d ",tree[altm+i]);
		}
		printf("\n");altm+=tm;
	}
} 
void TreeSelectionSort(int *mData)
{
	int MinValue = 0;
	int tree[N*4]; // 树的大小
	int max,maxIndex,treeSize;
	int i,n = N,baseSize = 1;
	while (baseSize < n)
		baseSize *= 2;
	treeSize = baseSize * 2 - 1;
	//printf("treeSize:%d,baseSize:%d\n",treeSize,baseSize);
	for (i = 0; i < n; i++){//将要排的数字填到树上 
		tree[treeSize - i] = mData[i];
	}
	for (; i < baseSize; i++){//其余的地方都填上最小值 
		tree[treeSize - i] = MinValue;
	}
	// 构造一棵树,大的值向上构造 
	for (i = treeSize; i > 1; i -= 2)
	{
		tree[i / 2] = (tree[i] > tree[i - 1] ? tree[i] : tree[i - 1]);
	}
	
	n -= 1;
	while (n != -1)
	{
		max = tree[1];        //树顶为最大值 
		mData[n--] = max;     //从大到小倒着贴到原数组上 
		maxIndex = treeSize;  //最大值下标 
		//print(tree);//打印树 
		while (tree[maxIndex] != max){
			maxIndex--;
		}//找最大值下标 
		tree[maxIndex] = MinValue;
		while (maxIndex > 1){
			if (maxIndex % 2 == 0){
				tree[maxIndex / 2] = (tree[maxIndex] > tree[maxIndex + 1] ? tree[maxIndex] : tree[maxIndex + 1]);
			}else{
				tree[maxIndex / 2] = (tree[maxIndex] > tree[maxIndex - 1] ? tree[maxIndex] : tree[maxIndex - 1]);
			}
			maxIndex /= 2;
		}
	}
}
int main()
{
	int a[N]={5,6,3,2,1,8,9,7,4,10};
	TreeSelectionSort(a);
	for(int i=0;i<N;i++){
		printf("%d ",a[i]);
	}
	return 0;
}




  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值