堆排序算法实现
来源:互联网 发布:java 如何实现方法重载 编辑:程序博客网 时间:2024/05/18 00:06
堆排序算法代码实现
class HeapSort { public static void main(String[] args) { int[] src = {1, 5, 7, 2, 3, 8, 27, 19,1, 5, 7, 2, 3, 8, 27, 19,1, 5, 7, 2, 3, 8, 27, 19,1, 5, 7, 2, 3, 8, 27, 19,1, 5, 7, 2, 3, 8, 27, 19}; heapSort(src); for(int num : src){ System.out.print(num + " "); } } public static void heapSort(int[] nums){ buildMaxHeap(nums,nums.length); for(int i = nums.length - 1; i >= 0; i--){ swap(nums,i,0); buildMaxHeap(nums,i); } } public static void buildMaxHeap(int[] nums,int length){ for(int i = (length - 2) / 2; i >= 0; i--){ maxHeapfy(nums,i,length - 1); } } public static void maxHeapfy(int[] nums,int index,int end){ if(index >= end){ return; } int max = index; int left = index * 2 + 1; int right = index * 2 + 2; if(left <= end && nums[left] > nums[max]) max = left; if(right <= end && nums[right] > nums[max]) max = right; if(max != index) { swap(nums, index, max); maxHeapfy(nums, max, end); } } public static void swap(int[] nums,int i,int j){ int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; }}
0 0
- 堆排序算法实现
- 堆排序算法实现
- 堆排序算法实现
- 堆排序算法实现
- 堆排序算法实现
- 堆排序算法实现
- 堆排序算法实现
- 堆排序算法实现
- 堆排序算法实现
- 堆排序算法(c++实现)
- 堆排序算法(c++实现)
- 堆排序算法的实现
- 堆排序算法及实现
- 堆排序算法的实现
- 【算法导论】堆排序实现
- 堆排序算法的实现
- 堆排序的算法实现
- 堆排序算法-java实现
- 代表月亮消灭你
- 【C++】实现对称矩阵以及压缩存储
- 求n次方的高效算法
- HDU
- Java程序的执行顺序以及赋值顺序
- 堆排序算法实现
- Stay hungry, Stay foolish
- java多线程(三) 之 对象的共享
- MySQL语句操作(日结二)
- DataGridView 保存编辑的行
- redis概念、做什么及其应用场景
- OpenAI Gym 入门与提高(一) Gym环境构建与最简单的RL agent
- 452. Minimum Number of Arrows to Burst Balloons
- spring <context:component-scan>使用说明