堆排序(1)HeapSort
2012-04-30 02:13
323 查看
heap_sort.h
utility.h
heap_sort.cc
main.cc
#ifndef HEAP_SORT_H #define HEAP_SORT_H #include "utility.h" typedef int ElemType; class Sort { public: static void HeapSort(ElemType *arr,size_t n); private: static void BuildMaxHeap(ElemType *arr,size_t n); //数组应该有(n+1)个元素,第0个置空 static void HeapAdjust(ElemType *arr,size_t i,size_t n); static void Swap(ElemType &e1,ElemType &e2); DISALLOW_COPY_AND_ASSIGN(Sort); }; #endif
utility.h
/*----------------------------------------------- Created By EverSteins Email:EverSteins@gmail.com 转载请注明出处 ------------------------------------------------*/ #ifndef UTILITY_H #define UTILITY_H #include <cstddef> #include <cstdlib> #include <cassert> #include <stdlib.h> #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&); \ void operator=(const TypeName&) #endif
heap_sort.cc
/*----------------------------------------------- Created By EverSteins Email:EverSteins@gmail.com 转载请注明出处 ------------------------------------------------*/ #include "stdafx.h" #include <iostream> #include "utility.h" #include "heap_sort.h" using namespace std; void Sort::HeapSort(ElemType *arr,size_t n) { BuildMaxHeap(arr,n); for (size_t i=n;i>1;--i) //注意i>1,循环(n-1)次。而不是循环(n-2)次 { //key:将堆顶记录和当前未排序子序列的最后一个记录交换 Swap(arr[1],arr[i]); //注意下标是1 HeapAdjust(arr,1,i-1); //注意是i-1 } } void Sort::BuildMaxHeap(ElemType *arr,size_t n) { //构造一个大顶堆 for (size_t i=n/2;i>0;--i) HeapAdjust(arr,i,n); } void Sort::HeapAdjust(ElemType *arr,size_t i,size_t n) { //summmary:确保i结点的子树中(包括i结点)i结点是最大的 //pre:1<=i<=n,数组arr应该有(n+1)个空间 //已知[i,n]中的记录除i外均满足堆的定义 //j总是为2*i或2*i+1 ElemType tmp=arr[i]; for (size_t j=2*i;j<=n;j*=2) { if (j<n && arr[j]<arr[j+1]) ++j; //j为i左右子结点中较大的结点 if (arr[j]<=tmp) break; arr[i]=arr[j]; //将左右子结点中较大的结点赋值给父结点 i=j; //i为变量是因为要始终成为j的父结点以赋值 } //3种情况:1)传进来的参数i没有子结点 2)传进来的参数i是三结点(或两结点)中最大值 //3)(至少进入2次for循环)处于中间链,比左右子结点大 ;或处于底层结点 arr[i]=tmp; } void Sort::Swap(ElemType &e1,ElemType &e2) { ElemType tmp=e1; e1=e2; e2=tmp; }
main.cc
/*----------------------------------------------- Created By EverSteins Email:EverSteins@gmail.com 转载请注明出处 ------------------------------------------------*/ #include "stdafx.h" #include <iostream> #include "utility.h" #include "heap_sort.h" using namespace std; typedef int ElemType; void PrintArray(ElemType *arr,size_t n); int _tmain(int argc, _TCHAR* argv[]) { //int arr[]={0,9,8,7,23,6,4,3,2,1,0}; //注意第一个元素没有意义,为哑元素。i从1开始 int arr[]={0,8}; size_t n=sizeof(arr)/sizeof(arr[0])-1; PrintArray(arr,n); Sort::HeapSort(arr,n); PrintArray(arr,n); system("pause"); return 0; } void PrintArray(ElemType *arr,size_t n) { for (size_t i=1;i<=n;++i) cout<<arr[i]<<' '; cout<<endl; }
相关文章推荐
- PHP实现排序算法----堆排序(Heap Sort)
- 堆--堆排序--heap--heap_sort
- HeapSort 堆排序
- 堆排序(Heap Sort)
- 堆排序(HeapSort)
- 算法----堆排序(heap sort)
- 堆排序(Heap Sort) 算法实现 C语言版
- algorithm: heap sort in python 算法导论 堆排序
- 堆排序_heap_sort
- 堆排序 Heap Sort
- 堆排序(HeapSort)
- 堆排序Heap sort
- 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)
- 算法总结系列之一:堆排序(Heap Sort)
- 堆排序(HeapSort)
- php堆排序(heapsort)练习
- 堆排序(Heap Sort)
- Java堆排序(HeapSort)算法实现
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
- 堆排序(Heap sort)