最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

排序

互联网 admin 42浏览 0评论

排序

package org.lion.euler.study.sort;/*** 堆排序* * 原理:建立大顶堆,交换第一个与最后一个值,重新维护大顶堆。* @author lion**/
public class HeapSort extends AbstractSort {@Overridepublic void sort(Integer[] array) {int len = array.length/2;while(len >= 0){this.heapMax(array, len, array.length);len -= 1;}int curLen = array.length - 1;while(curLen >= 0){this.swap(array, 0, curLen);this.heapMax(array, 0, curLen);curLen -= 1;}}private void heapMax(Integer[] array, int index, int maxLen){int len = maxLen;int curIndex = index;while(curIndex < len){int left = curIndex * 2;int right = left + 1;int maxIndex = curIndex;if(left < len && array[left] > array[maxIndex]){maxIndex = left;}if(right < len && array[right] > array[maxIndex]){maxIndex = right;}if(maxIndex > curIndex){this.swap(array, curIndex, maxIndex);curIndex = maxIndex;}else{break;}}}}

排序

package org.lion.euler.study.sort;/*** 堆排序* * 原理:建立大顶堆,交换第一个与最后一个值,重新维护大顶堆。* @author lion**/
public class HeapSort extends AbstractSort {@Overridepublic void sort(Integer[] array) {int len = array.length/2;while(len >= 0){this.heapMax(array, len, array.length);len -= 1;}int curLen = array.length - 1;while(curLen >= 0){this.swap(array, 0, curLen);this.heapMax(array, 0, curLen);curLen -= 1;}}private void heapMax(Integer[] array, int index, int maxLen){int len = maxLen;int curIndex = index;while(curIndex < len){int left = curIndex * 2;int right = left + 1;int maxIndex = curIndex;if(left < len && array[left] > array[maxIndex]){maxIndex = left;}if(right < len && array[right] > array[maxIndex]){maxIndex = right;}if(maxIndex > curIndex){this.swap(array, curIndex, maxIndex);curIndex = maxIndex;}else{break;}}}}

发布评论

评论列表 (0)

  1. 暂无评论