首页 > 其他 > 详细

最大子矩阵和问题

时间:2014-04-09 01:08:44      阅读:422      评论:0      收藏:0      [点我收藏+]

1.算法概述


相较于归并排序,堆排序的时间复杂度也为O(n*log n),但空间复杂度远小于归并排序。堆排序用到了实用的数据结构——堆(heap),关于堆的详细介绍参看这里。堆排序基本思想:

  • 将待排序表建成一个大顶堆;
  • 取堆顶元素与堆的最后一个元素交换,删除最后一个元素,向下调整使得继续保持堆的特性;
  • 如此往复,直至堆剩下最后一个元素

待排序表(26,5,77,1,61,11,59,15,48,19),堆排序过程如下:
bubuko.com,布布扣
1.建立大顶堆

bubuko.com,布布扣
2. 堆排序


/*adjust to keep the characters of the heap*/
void adjust(int a[],int root,int n)
{
	int rootkey=a[root];
	int child=2*root;      //left child
	while(child<=n)
	{
		if(child<n&&a[child]<a[child+1])
			child++;       //if right-child>left-child
		if(rootkey>a[child]) 
			break;
		else
		{
			a[child/2]=a[child];   //move to parent
			child*=2;
		}
	}	
	a[child/2]=rootkey;
}

void heap_sort(int a[],int n)
{
	int i;
	for(i=n/2;i>0;i--) 
		adjust(a,i,n);      //establish the heap
	for(i=n-1;i>0;i--)
	{
		swap(&a[1],&a[i+1]); 
		adjust(a,1,i);  
	}
}


2.Referrence


[1] Horowitz, et al., 《数据结构基础》, 清华出版社.

最大子矩阵和问题,布布扣,bubuko.com

最大子矩阵和问题

原文:http://blog.csdn.net/u013240812/article/details/23203537

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!