您的位置:首页 > 其它

自己动手写算法.Sort.HeapSort

2008-10-08 23:30 363 查看
Heap Sort Algorithm



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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: