`

堆排序

阅读更多
/**
 * <ul>
 * <li>平均情况:O(nlog(2)n)</li>
 * <li>最好情况:O(nlog(2)n)</li>
 * <li>最坏情况:O(nlog(2)n)</li>
 * <li>辅助存储:O(1)</li>
 * <li>不稳定</li>
 * <ul>
 * 
 * @timestamp Mar 12, 2016 6:56:54 PM
 * @author smallbug
 */
public class HeapSort {
	public static void main(String[] args) {
		int[] data = DataUtil.getData(50);
		System.out.println(Arrays.toString(data));
		long time = System.currentTimeMillis();
		heapSort(data);
		System.out.println(Arrays.toString(data));
		System.out.println("speed " + (System.currentTimeMillis() - time) + " ms");
		System.out.println("排序是否成功:" + (DataUtil.verify(data, DataUtil.ASC) ? "是" : "否"));
	}

	private static void heapSort(int[] data) {
		createHeap(data);
		heapSortDetail(data);
	}

	private static void heapSortDetail(int[] data) {
		// 末尾与头交换,交换后调整最大堆
		for (int i = data.length - 1; i > 0; i--) {
			int temp = data[0];
			data[0] = data[i];
			data[i] = temp;
			maxHeapify(data, i, index2Cood(0));
		}
	}

	private static void createHeap(int[] data) {
		int startIndex = getParentIndex(data.length);
		for (int i = startIndex; i >= 0; i--) {
			maxHeapify(data, data.length, index2Cood(i));
		}
	}

	private static void maxHeapify(int[] data, int heapSize, int cood) {
		// 当前点与左右子节点比较
		int leftIndex = getChildLeftIndex(cood);
		int rightIndex = getChildRightIndex(cood);

		int largest = cood;
		if (leftIndex < heapSize && data[cood2Index(cood)] < data[leftIndex]) {
			largest = index2Cood(leftIndex);
		}
		if (rightIndex < heapSize && data[cood2Index(largest)] < data[rightIndex]) {
			largest = index2Cood(rightIndex);
		}
		// 得到最大值后可能需要交换,如果交换了,其子节点可能就不是最大堆了,需要重新调整
		if (largest != cood) {
			int temp = data[cood2Index(cood)];
			data[cood2Index(cood)] = data[cood2Index(largest)];
			data[cood2Index(largest)] = temp;
			maxHeapify(data, heapSize, largest);
		}
	}

	/**
	 * 索引转坐标
	 * 
	 * @timestamp 2016年8月23日 上午11:12:01
	 * @param index
	 * @return
	 */
	private static int index2Cood(int index) {
		return index + 1;
	}

	/**
	 * 坐标转索引
	 * 
	 * @timestamp 2016年8月23日 上午11:11:51
	 * @param cood
	 * @return
	 */
	private static int cood2Index(int cood) {
		return cood - 1;
	}

	/**
	 * 根据子元素坐标获取父元素索引
	 * 
	 * @timestamp 2016年8月23日 上午11:30:27
	 * @param cood
	 * @return
	 */
	private static int getParentIndex(int cood) {
		return cood2Index(cood >>> 1);
	}

	/**
	 * 左子节点position注意括号,加法优先级更高
	 * 
	 * @param current
	 * @return
	 */
	private static int getChildLeftIndex(int cool) {
		return cood2Index(cool << 1);
	}

	/**
	 * 右子节点position
	 * 
	 * @param current
	 * @return
	 */
	private static int getChildRightIndex(int cool) {
		return cood2Index(cool << 1) + 1;
	}
}

 

1
5
分享到:
评论

相关推荐

    图解堆排序

    在学习堆排序前,我们需要知道顺序存储二叉树和堆的知识点。 一、顺序存储二叉树 1.概念:顺序存储二叉树即用数组的方式存储二叉树的节点 2.顺序存储二叉树的特点: ①顺序二叉树通常只考虑完全二叉树 ②第n个元素的...

    堆排序(C语言实现)

    堆排序(C语言实现)算法思想步骤程序 算法思想 见: 4. 选择排序—堆排序(Heap Sort) 算法导论——堆排序heapsort 步骤 1. 将n个元素建立初始堆,第一个节点放在数组下标1中,因此n个节点对应数组 a[1] ~ a[n],...

    堆排序 减治法——C++代码

    堆排序是一种基于比较的排序算法,它利用了数据结构中的“堆”这一概念。在计算机科学中,堆通常被理解为一个完全二叉树,其中每个父节点的值都大于或等于(大顶堆)或小于或等于(小顶堆)其子节点的值。堆排序算法...

    C++实现堆排序

    1、 实现堆排序算法。 2、 理论分析并实验验证堆排序算法的时间复杂度。

    堆排序代码

    内部排序之堆排序的具体代码实现,简单同时也易于看懂

    C#堆排序实现方法

    主要介绍了C#堆排序实现方法,实例分析了C#对排序的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics