HeapSort 堆排序 基于伪代码实现
来源:互联网 发布:株洲市工商局网络核名 编辑:程序博客网 时间:2024/05/24 01:45
GIF 动态图
伪代码
/* From Wikipedia, the free encyclopedia */
1.父子节点特征
iParent = floor((i-1) / 2);
iLeftChild = 2*i + 1;
iRightChild = 2*i + 2;
2.算法伪代码
/* 保持原汁原味就不翻译了 =。= */
procedure heapsort(a, count) is input: an unordered array a of length count (Build the heap in array a so that largest value is at the root) heapify(a, count) (The following loop maintains the invariants that a[0:end] is a heap and every element beyond end is greater than everything before it (so a[end:count] is in sorted order)) end ← count - 1 while end > 0 do (a[0] is the root and largest value. The swap moves it in front of the sorted elements.) swap(a[end], a[0]) (the heap size is reduced by one) end ← end - 1 (the swap ruined the heap property, so restore it) siftDown(a, 0, end)(Put elements of 'a' in heap order, in-place)procedure heapify(a, count) is (start is assigned the index in 'a' of the last parent node) (the last element in a 0-based array is at index count-1; find the parent of that element) start ← floor ((count - 2) / 2) while start ≥ 0 do (sift down the node at index 'start' to the proper place such that all nodes below the start index are in heap order) siftDown(a, start, count - 1) (go to the next parent node) start ← start - 1 (after sifting down the root all nodes/elements are in heap order)(Repair the heap whose root element is at index 'start', assuming the heaps rooted at its children are valid)procedure siftDown(a, start, end) is root ← start while root * 2 + 1 ≤ end do (While the root has at least one child) child ← root * 2 + 1 (Left child) swap ← root (Keeps track of child to swap with) if a[swap] < a[child] swap ← child (If there is a right child and that child is greater) if child+1 ≤ end and a[swap] < a[child+1] swap ← child + 1 if swap = root (The root holds the largest element. Since we assume the heaps rooted at the children are valid, this means that we are done.) return else swap(a[root], a[swap]) root ← swap (repeat to continue sifting down the child now)
Java实现
<span style="font-size:18px;">public void heapsort(int[] a, int count) {if (count < 2)return;heapify(a, count);int end = count - 1;while (end > 0) {swap(a, 0, end);end--;siftdown(a, 0, end);}}public void heapify(int[] a, int count) {int start = (count - 2) / 2;while (start >= 0) {siftdown(a, start, count - 1);start--;}}public void siftdown(int[] a, int start, int end) {int root = start;while (root * 2 + 1 <= end) {int child = root * 2 + 1;int swap = root;if (a[swap] < a[child]) {swap = child;}if (child + 1 <= end && a[swap] < a[child + 1]) {swap = child + 1;}if (root == swap) {return;} else {swap(a, root, swap);}root = swap;}}public void swap(int[] a, int i, int j) {int t = a[i];a[i] = a[j];a[j] = t;}</span>
0 0
- HeapSort 堆排序 基于伪代码实现
- heapsort(堆排序)的代码实现
- 堆排序heapSort java实现
- HeapSort堆排序Java实现图文代码详解
- 堆排序(Heapsort)之Java实现
- 堆排序(Heapsort)之Java实现
- 算法学习 - 堆排序 ( HeapSort ) C++实现
- 堆排序(HeapSort) Java实现
- 堆排序(Heapsort)之Java实现
- Java堆排序(HeapSort)算法实现
- 堆排序(HeapSort)之java实现
- Heapsort 堆排序算法详解(Java实现)
- HeapSort堆排序的理解和实现
- 堆排序(Heapsort)之Java实现
- 堆排序算法的实现(HeapSort)
- Heapsort 堆排序算法详解(Java实现)
- 堆排序 HeapSort C语言实现 附严蔚敏数据结构P282 代码错误
- 堆排序(HeapSort)
- restful webService 方法跳转错误的解决方案
- 一个数只有三个约数的特征
- AndroidManifest.xml 文件详解
- ZOJ 1610 Count the Colors
- 将字符串切割成数组 componentsSeparatedByString
- HeapSort 堆排序 基于伪代码实现
- iOS开发常用快捷键
- linux下C语言中的flock函数用法 .
- Fragment StartActivityForResult
- 阻止事件冒泡stopPropagation的兼容写法
- 在关机界面添加重启菜单功能
- 黑马程序员_java集合下
- Android log日志记录方法
- 最小生成树~Prim算法