Program work 13. Heap Sort in Java
来源:互联网 发布:java 哪里用到反射 编辑:程序博客网 时间:2024/05/16 11:07
关键在于最大堆的建立和重构 (其实两个过程是一样的)
因为堆是平衡二叉树, 所以数组可以直接用下标表示父子的节点关系, 数组要从下标1开始.
将最大值移到数组只需要O(n)
重构堆需要O(lgn)
所以upper bound is O(n*logn)
事实上, lower bound is Ω(n*logn)
最好最坏最差都是O(n*logn)
Pseudocode
heapSort (arr, len){ buildMaxHeap() for () { remove the root element(the max) to the end of array reconstructHeap(); }}reconsturctHeap(arr, len){ tmp = arr[root] get the children of root while () { get the bigger child between left and right child if the element of that child is larger than root replace its parent element with is move to next children } place the tmp to the apporpriate pos}
Java
package ncku.cdc.sorting;import java.util.Random;public class HeapSort { private int[] sequence; public HeapSort(int size, int range) { sequence = new int[size + 1]; Random rand = new Random(); for (int i = 1; i <= size; i++) { sequence[i] = rand.nextInt(range); } } public static void main(String[] args) { int size = Integer.valueOf(args[0]); int range = Integer.valueOf(args[1]); HeapSort heap = new HeapSort(size, range); System.out.println("before heapSort:"); SortingTools.validation(heap.getSequence(), 1); heap.heapSort(heap.getSequence(), size); System.out.println("after heapSort:"); SortingTools.validation(heap.getSequence(), 1); } public void heapSort(int[] arr, int len) { for (int i = len / 2; i > 0; i--) { retifyHeap(arr, i, len); } for (int j = len - 1; j > 0; j--) { swap(arr, 1, j + 1); retifyHeap(arr, 1, j); } } private void retifyHeap(int[] arr, int root, int len) { int tmp = arr[root]; int child = root * 2; while (child <= len) { if ((child < len) && (arr[child] < arr[child + 1])) { child++; } if (tmp > arr[child]) { break; } else { arr[child / 2] = arr[child]; child *= 2; } } arr[child / 2] = tmp; } private void swap(int[] arr, int i, int j) { int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } public int[] getSequence() { return sequence; }}
程序输出:
before heapSort:
67 18 192 142 136 30 144 6 158 11 123 34 36 155 97 12 7 159 81 135 160 102 48 42 0 155 77 114 139 146 186 32 42 144 100
after heapSort:
0 6 7 11 12 18 30 32 34 36 42 42 48 67 77 81 97 100 102 114 123 135 136 139 142 144 144 146 155 155 158 159 160 186 192
0 0
- Program work 13. Heap Sort in Java
- Program work 11. Quick Sort in Java
- Program work 12. Merge Sort in Java
- Program work 14. Insertion Sort in Java
- Program work 15. Shell Sort in Java
- Program work 16. Radix Sort in Java
- Program work 17. Selection Sort in Java
- Program work 18. Bubble Sort in Java
- Heap Sort in C++
- heap sort java
- path in java program
- Encoding in Java program
- heap sort in python :-) just for fun
- Stack & Heap in Java
- Heap Sort
- Heap Sort
- heap sort
- heap sort
- HDU ACM 1281 棋盘游戏->二分图最大匹配(匈牙利算法实践)
- 【最短路】 ZOJ 3877 Earthstone Keeper
- 普通树的非递归遍历
- 春漫湖㳇 带我回家
- 英语——突破的一步
- Program work 13. Heap Sort in Java
- Java NIO使用及原理分析(二)
- B\S备忘录13——终于有时间看看MVC了R2
- 广播接收者 -- BroadcastReceiver
- HashMap时间复杂度问题
- 在JSP页面更改数据库信息后点击确定成功后怎么刷新
- 创建线程的第二种方法:实现Runnable接口
- ThinkPHP获取带条件数据总条数(行数)
- Java NIO使用及原理分析(三)