自己动手写算法.Sort.HeapSort
2008-10-08 23:30
417 查看
Heap Sort Algorithm
![](http://p.blog.csdn.net/images/p_blog_csdn_net/mythma/EntryImages/20081008/logo.jpg)
heap-sort uses max heap data structure to sort a sequence. The steps are:
We need construct a max heap in place firstly, so that we can make sure that the bigest value locates on the first position of the heap.
Swap the top value with the last value of the sequence, and shrink the size to nSize-1, and reconstruct the max heap.
Swap loopsly and the sequence will be sorted.
The key issue is how to present a max heap data structure by an array.
#define LEFT_CHILD_INDEX(idx) (idx*2 + 1)
#define RIGHT_CHILD_INDEX(idx) (idx*2 + 2)
#define PARENT_INDEX(idx) (idx-1)/2
/*
Given a max-heap, if we changed a value at idx, we should make sure
that all its children are not bigger than it.
*/
void max_heapify( const int idx, int arr[], const int nSize )
{
int nCur = idx;
int nLeft = LEFT_CHILD_INDEX(nCur);
int nRight = RIGHT_CHILD_INDEX(nCur);
if( nRight < nSize-1 )
{
if( arr[nCur] < arr[nRight] )
{
std::swap( arr[nCur], arr[nRight] );
max_heapify(nRight, arr, nSize);
}
}
if( nLeft < nSize-1 )
{
if( arr[nCur] < arr[nLeft] )
{
std::swap( arr[nCur], arr[nLeft] );
max_heapify(nLeft, arr, nSize);
}
}
}
/*
a non complete binary tree has (nSize+1)/2 children
*/
void build_max_heap(int arr[], const int nSize)
{
int nCount = nSize - (nSize+1)/2;
for(int i=0; i<nCount; ++i)
{
max_heapify(i, arr, nSize);
}
}
/*
Build a max heap firstly, so the first one is the biggest one
Swap with the last one, rebuild the max heap, but ignoring the last one
*/
void SortHeap::sort(int arr[], const int nSize)
{
ALog::print(_T("# Heap Sort: "));
ALog::print(arr, nSize);
build_max_heap(arr, nSize);
ALog::print(arr, nSize);
for( int i=nSize-1; i>1; --i )
{
std::swap(arr[0],arr[i]);
max_heapify(0, arr, i);
ALog::print(arr, nSize);
}
}
void SortHeap::test()
{
int arr[] = { 2, 9, 3, 7, 8, 6, 4, 5, 0, 1};
const int nSize = sizeof(arr)/sizeof(int);
SortHeap::sort(arr, nSize);
}
The test result:
# Heap Sort:
2 9 3 7 8 6 4 5 0 1
================
9 8 6 7 3 4 2 5 0 1
8 7 4 6 3 2 1 5 0 9
7 6 2 4 3 1 0 5 8 9
6 5 2 4 3 1 0 7 8 9
5 4 0 3 2 1 6 7 8 9
4 3 0 1 2 5 6 7 8 9
3 2 0 1 4 5 6 7 8 9
2 1 0 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
![](http://p.blog.csdn.net/images/p_blog_csdn_net/mythma/EntryImages/20081008/logo.jpg)
heap-sort uses max heap data structure to sort a sequence. The steps are:
We need construct a max heap in place firstly, so that we can make sure that the bigest value locates on the first position of the heap.
Swap the top value with the last value of the sequence, and shrink the size to nSize-1, and reconstruct the max heap.
Swap loopsly and the sequence will be sorted.
The key issue is how to present a max heap data structure by an array.
#define LEFT_CHILD_INDEX(idx) (idx*2 + 1)
#define RIGHT_CHILD_INDEX(idx) (idx*2 + 2)
#define PARENT_INDEX(idx) (idx-1)/2
/*
Given a max-heap, if we changed a value at idx, we should make sure
that all its children are not bigger than it.
*/
void max_heapify( const int idx, int arr[], const int nSize )
{
int nCur = idx;
int nLeft = LEFT_CHILD_INDEX(nCur);
int nRight = RIGHT_CHILD_INDEX(nCur);
if( nRight < nSize-1 )
{
if( arr[nCur] < arr[nRight] )
{
std::swap( arr[nCur], arr[nRight] );
max_heapify(nRight, arr, nSize);
}
}
if( nLeft < nSize-1 )
{
if( arr[nCur] < arr[nLeft] )
{
std::swap( arr[nCur], arr[nLeft] );
max_heapify(nLeft, arr, nSize);
}
}
}
/*
a non complete binary tree has (nSize+1)/2 children
*/
void build_max_heap(int arr[], const int nSize)
{
int nCount = nSize - (nSize+1)/2;
for(int i=0; i<nCount; ++i)
{
max_heapify(i, arr, nSize);
}
}
/*
Build a max heap firstly, so the first one is the biggest one
Swap with the last one, rebuild the max heap, but ignoring the last one
*/
void SortHeap::sort(int arr[], const int nSize)
{
ALog::print(_T("# Heap Sort: "));
ALog::print(arr, nSize);
build_max_heap(arr, nSize);
ALog::print(arr, nSize);
for( int i=nSize-1; i>1; --i )
{
std::swap(arr[0],arr[i]);
max_heapify(0, arr, i);
ALog::print(arr, nSize);
}
}
void SortHeap::test()
{
int arr[] = { 2, 9, 3, 7, 8, 6, 4, 5, 0, 1};
const int nSize = sizeof(arr)/sizeof(int);
SortHeap::sort(arr, nSize);
}
The test result:
# Heap Sort:
2 9 3 7 8 6 4 5 0 1
================
9 8 6 7 3 4 2 5 0 1
8 7 4 6 3 2 1 5 0 9
7 6 2 4 3 1 0 5 8 9
6 5 2 4 3 1 0 7 8 9
5 4 0 3 2 1 6 7 8 9
4 3 0 1 2 5 6 7 8 9
3 2 0 1 4 5 6 7 8 9
2 1 0 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
相关文章推荐
- 自己动手写算法.Sort.HeapSort
- 自己动手写算法.Sort.HeapSort
- 自己动手写算法.Sort.Bubble
- 自己动手写算法.Sort.QuickSort
- 自己动手写算法.Sort.Bubble
- 自己动手写算法.Sort.QuickSort
- 自己动手写算法.Sort.Bubble
- 自己动手写算法.Sort.QuickSort
- c++堆排序实现(heapsort) (算法导论)
- Java堆排序(HeapSort)算法实现
- 每天一个小算法(Heapsort)
- 每天一个小算法(Heapsort)
- 选择排序算法:堆排序-Heap Sort
- 算法总结系列之一:堆排序(Heap Sort)
- 堆排序(Heap Sort)算法的实现
- 算法总结系列之一:堆排序(Heap Sort)
- HeapSort-伪码的算法描述整理如下
- 算法 Heap sort
- 每天一个小算法(Heapsort)
- 每天一个小算法(Heapsort)