读书笔记 算法导论 堆排序
2011-04-05 15:56
197 查看
感觉自己算法不给力额...对基本概念和基本操作都不够熟悉
准备吧整个算法导论过一遍...把大部分常用的东西自己写一遍
做it真是辛苦啊...
先弄个常用的堆吧
堆可以用作堆排序,优先队列等等情况
PS:看伪代码有的时候很郁闷 例如C#数组下标开始是0,伪代码中一般是1 转换来转换去就乱了.......尴尬
准备吧整个算法导论过一遍...把大部分常用的东西自己写一遍
做it真是辛苦啊...
先弄个常用的堆吧
堆可以用作堆排序,优先队列等等情况
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace IntroduceToAlgorithm { public class MaxHeap { /// <summary> /// check the node is obey Max_Heap_property or not /// </summary> /// <param name="A">an int array represent a heap</param> /// <param name="i">the node index</param> /// <param name="aLength">the arry length , (some time, we will reduce the length of array)</param> public void MaxHeapify(int[] A, int i, int aLength = -1) { int length = aLength <= -1 ? A.Length : aLength; var l = Left(i); var r = Right(i); int largest = i; if (l < length && A[l] > A[i]) { largest = l; } if (r < length && A[r] > A[largest]) { largest = r; } if (largest != i) { Exchange(A, i, largest); MaxHeapify(A, largest, aLength); } //MaxHeapify(A, l); //MaxHeapify(A, i); } public int Left(int index) { return (index + 1) * 2 - 1; } public int Right(int index) { return (index + 1) * 2; } public void Exchange(int[] A, int i, int i2) { int val = A[i]; A[i] = A[i2]; A[i2] = val; } /// <summary> /// build a max-heap, all element obey max-heap-property /// wo only need check element which index less than A.Length/2 -1 /// </summary> /// <param name="A"></param> public void Build_Max_Heap(int[] A) { for (int i = (A.Length / 2 - 1); i >= 0; i--) { MaxHeapify(A, i); } } public void HeapSort(int[] A) { int length = A.Length; Build_Max_Heap(A); for (int i = A.Length - 1; i >= 1; i--) { Exchange(A, 0, i); MaxHeapify(A, 0, --length); } } } }
PS:看伪代码有的时候很郁闷 例如C#数组下标开始是0,伪代码中一般是1 转换来转换去就乱了.......尴尬
相关文章推荐
- 数据挖掘——概念、模型和算法读书笔记
- 《大话数据结构》读书笔记 第二章 算法 函数的渐近增长
- 《大话数据结构》读书笔记之冒泡排序和源码及优化算法源码
- 读书笔记_算法第四版(二)
- 《统计学习方法》读书笔记-----决策树:ID3,C4.5生成算法和剪枝
- 《统计学习方法》读书笔记-----决策树:CART算法
- 《C++Primer》读书笔记——附录A.2 标准库算法概览
- 《Python机器学习实战》第一章读书笔记:k-近邻算法
- 分布式系统的一致性算法------《Designing Data-Intensive Applications》读书笔记13
- 【机器学习实战系列】读书笔记之KNN算法(三)
- 算法(读书笔记):4.图
- opencv2对读书笔记——使用均值漂移算法查找物体
- 大话数据结构 第二章 算法(读书笔记)
- 《统计学习方法》读书笔记-----决策树:ID3,C4.5生成算法和剪枝
- 《统计学习方法》读书笔记-----决策树:CART算法
- 《数据算法-Hadoop/Spark大数据处理技巧》读书笔记(一)——二次排序
- 读书笔记:算法导论第2章 第2节分析算法
- 机器学习实战第二章——学习KNN算法,读书笔记
- 《数据算法-Hadoop/Spark大数据处理技巧》读书笔记(四)——移动平均
- <<C++标准STL>>读书笔记--算法之比较