堆排序

An Example of Heapsort:

Given an array of 6 elements: 15, 19, 10, 7, 17, 16, sort it in ascending order using heap sort.

Steps:

  1. Consider the values of the elements as priorities and build the heap tree.
  2. Start deleteMin operations, storing each deleted element at the end of the heap array.

After performing step 2, the order of the elements will be opposite to the order in the heap tree. 
Hence, if we want the elements to be sorted in ascending order, we need to build the heap tree 
in descending order - the greatest element will have the highest priority.

Note that we use only one array , treating its parts differently:

  1. when building the heap tree, part of the array will be considered as the heap, 
    and the rest part - the original array.
  2. when sorting, part of the array will be the heap, and the rest part - the sorted array.

This will be indicated by colors: white for the original array, blue for the heap and red for the sorted array

Here is the array: 15, 19, 10, 7, 17, 6

  1. Building the heap treeThe array represented as a tree, complete but not ordered:

    Start with the rightmost node at height 1 - the node at position 3 = Size/2. 
    It has one greater child and has to be percolated down:

    After processing array[3] the situation is:

    Next comes array[2]. Its children are smaller, so no percolation is needed.

    The last node to be processed is array[1]. Its left child is the greater of the children. 
    The item at array[1] has to be percolated down to the left, swapped with array[2].

    As a result the situation is:

    The children of array[2] are greater, and item 15 has to be moved down further, swapped with array[5].

    Now the tree is ordered, and the binary heap is built.

  2. Sorting - performing deleteMax operations:

    1. Delete the top element 19.

    1.1. Store 19 in a temporary place. A hole is created at the top

    1.2. Swap 19 with the last element of the heap. 

    As 10 will be adjusted in the heap, its cell will no longer be a part of the heap. 
    Instead it becomes a cell from the sorted array

    1.3. Percolate down the hole

    1.4. Percolate once more (10 is less that 15, so it cannot be inserted in the previous hole)

    Now 10 can be inserted in the hole

    2. DeleteMax the top element 17

    2.1. Store 17 in a temporary place. A hole is created at the top

    2.2. Swap 17 with the last element of the heap. 

    As 10 will be adjusted in the heap, its cell will no longer be a part of the heap.
    Instead it becomes a cell from the sorted array

    2.3. The element 10 is less than the children of the hole, and we percolate the hole down:

    2.4. Insert 10 in the hole

    3. DeleteMax 16

    3.1. Store 16 in a temporary place. A hole is created at the top

    3.2. Swap 16 with the last element of the heap. 

    As 7 will be adjusted in the heap, its cell will no longer be a part of the heap.
    Instead it becomes a cell from the sorted array

    3.3. Percolate the hole down (7 cannot be inserted there - it is less than the children of the hole)

    3.4. Insert 7 in the hole

    4. DeleteMax the top element 15

    4.1. Store 15 in a temporary location. A hole is created.

    4.2. Swap 15 with the last element of the heap. 

    As 10 will be adjusted in the heap, its cell will no longer be a part of the heap. 
    Instead it becomes a position from the sorted array

    4.3. Store 10 in the hole (10 is greater than the children of the hole)

    5. DeleteMax the top element 10.

    5.1. Remove 10 from the heap and store it into a temporary location.

    5.2. Swap 10 with the last element of the heap. 

    As 7 will be adjusted in the heap, its cell will no longer be a part of the heap. Instead it becomes a cell from the sorted array

    5.3. Store 7 in the hole (as the only remaining element in the heap

    7 is the last element from the heap, so now the array is sorted

Java实现:

public class HeapSort {
	private static int[] sort = new int[]{1,0,10,20,3,5,6,4,9,8,12,17,34,11};
	public static void main(String[] args) {
		buildMaxHeapify(sort);
		heapSort(sort);
		print(sort);
	}

	private static void buildMaxHeapify(int[] data){
		//没有子节点的才需要创建最大堆,从最后一个的父节点开始
		int startIndex = getParentIndex(data.length - 1);
		//从尾端开始创建最大堆,每次都是正确的堆
		for (int i = startIndex; i >= 0; i--) {
			maxHeapify(data, data.length, i);
		}
	}
	
	/**
	 * 创建最大堆
	 * @param data
	 * @param heapSize需要创建最大堆的大小,一般在sort的时候用到,因为最多值放在末尾,末尾就不再归入最大堆了
	 * @param index当前需要创建最大堆的位置
	 */
	private static void maxHeapify(int[] data, int heapSize, int index){
		// 当前点与左右子节点比较
		int left = getChildLeftIndex(index);
		int right = getChildRightIndex(index);
		
		int largest = index;
		if (left < heapSize && data[index] < data[left]) {
			largest = left;
		}
		if (right < heapSize && data[largest] < data[right]) {
			largest = right;
		}
		//得到最大值后可能需要交换,如果交换了,其子节点可能就不是最大堆了,需要重新调整
		if (largest != index) {
			int temp = data[index];
			data[index] = data[largest];
			data[largest] = temp;
			maxHeapify(data, heapSize, largest);
		}
	}
	
	/**
	 * 排序,最大值放在末尾,data虽然是最大堆,在排序后就成了递增的
	 * @param data
	 */
	private static void heapSort(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, 0);
		}
	}
	
	/**
	 * 父节点位置
	 * @param current
	 * @return
	 */
	private static int getParentIndex(int current){
		return (current - 1) >> 1;
	}
	
	/**
	 * 左子节点position注意括号,加法优先级更高
	 * @param current
	 * @return
	 */
	private static int getChildLeftIndex(int current){
		return (current << 1) + 1;
	}
	
	/**
	 * 右子节点position
	 * @param current
	 * @return
	 */
	private static int getChildRightIndex(int current){
		return (current << 1) + 2;
	}
	
	private static void print(int[] data){
		int pre = -2;
		for (int i = 0; i < data.length; i++) {
			if (pre < (int)getLog(i+1)) {
				pre = (int)getLog(i+1);
				System.out.println();
			} 
			System.out.print(data[i] + " |");
		}
	}
	
	/**
	 * 以2为底的对数
	 * @param param
	 * @return
	 */
	private static double getLog(double param){
		return Math.log(param)/Math.log(2);
	}
}


内容概要:本文介绍了一项创新性未发表的研究,即利用多元宇宙优化算法(Multiverse Optimizer, MVO)对分时电价下的需求响应与综合能源系统调度问题进行建模与求解,旨在实现能源系统的经济性、高效性与可持续性运行。该研究构建了包含多种能源设备(如光伏、风机、燃气轮机、储能系统等)及可调节负荷的综合能源系统模型,充分考虑了用户侧的需求响应行为在分时电价机制下的响应特性,通过MVO算法对系统运行成本、能源利用率、碳排放等多目标进行协同优化,实现了日前调度计划的智能决策。研究还提供了完整的MATLAB代码实现,便于研究人员复现实验、验证算法性能,并为进一步研究提供可靠的仿真基础。; 适合人群:具备一定电力系统、优化算法及MATLAB编程基础的科研人员、研究生以及从事能源互联网、综合能源系统规划与运行的技术工程师。; 使用场景及目标:① 学习并掌握多元宇宙优化算法在复杂能源系统调度中的具体应用方法;② 研究分时电价机制如何通过需求响应引导用户参与电网互动,实现削峰填谷;③ 实现综合能源系统(IES)中冷、热、电、气等多种能源的协同优化调度,以降低运行成本、提高新能源消纳能力和系统可靠性;④ 为相关领域的学术研究提供可复现的代码实例和仿真平台。; 阅读建议:此资源以MATLAB代码为核心载体,深入剖析了算法应用与系统建模的全过程。建议读者在学习时,不仅应关注代码的实现细节,更要理解其背后的数学模型、优化目标设定和约束条件的物理意义。建议结合文档中的模型描述,逐步调试代码,观察不同参数和场景下的优化结果,从而深刻掌握综合能源系统优化调度的设计思想与关键技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值