查找与排序 堆排序

简述堆排序

1、首先了解堆是什么
堆是一种数据结构,一种叫做完全二叉树的数据结构。

2、堆的性质
这里我们用到两种堆,其实也算是一种。

大顶堆:每个节点的值都大于或者等于它的左右子节点的值。

小顶堆:每个节点的值都小于或者等于它的左右子节点的值。
在这里插入图片描述
对于大顶堆:arr[i] >= arr[2i + 1] && arr[i] >= arr[2i + 2]

对于小顶堆:arr[i] <= arr[2i + 1] && arr[i] <= arr[2i + 2]

3、堆排序的基本思想

堆排序的基本思想是:
1、将带排序的序列构造成一个大顶堆,根据大顶堆的性质,当前堆的根节点(堆顶)就是序列中最大的元素;
2、将堆顶元素和最后一个元素交换,然后将剩下的节点重新构造成一个大顶堆;
3、重复步骤2,如此反复,从第一次构建大顶堆开始,每一次构建,我们都能获得一个序列的最大值,然后把它放到大顶堆的尾部。最后,就得到一个有序的序列了。


在这里插入图片描述

实现代码

/**
	 *********************
	 * Heap sort. Maybe the most difficult sorting algorithm.
	 *********************
	 */
	public void heapSort() {
		DataNode tempNode;
		// Step 1.Construct the initial heap.
		for (int i = length / 2 - 1; i >= 0; i--) {
			adjustHeap(i, length);
		} // Of for i
		System.out.println("The initial heap:" + this + "\r\n");

		// Step 2.Swap and reconstruct.
		for (int i = length - 1; i > 0; i--) {
			tempNode = data[0];
			data[0] = data[i];
			data[i] = tempNode;

			adjustHeap(0, i);
			System.out.println("Round " + (length - i) + ":" + this);
		} // Of for i
	}// Of heapSort

	/**
	 *********************
	 * Adjust the heap.
	 * 
	 * @param paraStart  The Start of the index.
	 * @param paraLength the length of the adjusted sequence.
	 *********************
	 */
	public void adjustHeap(int paraStart, int paraLength) {
		DataNode tempNode = data[paraStart];
		int tempParent = paraStart;
		int tempKey = data[paraStart].key;

		for (int tempChild = paraStart * 2 + 1; tempChild < paraLength; tempChild = tempChild * 2 + 1) {
			// The right child is bigger.
			if (tempChild + 1 < paraLength) {
				if (data[tempChild].key < data[tempChild + 1].key) {
					tempChild++;
				} // Of if
			} // Of if

			System.out.println("The parent position is" + tempParent + "and the child is " + tempChild);
			if (tempKey < data[tempChild].key) {
				// The child is bigger.
				data[tempParent] = data[tempChild];
				System.out.println("Move " + data[tempChild].key + "to position " + tempParent);
				tempParent = tempChild;
			} else {
				break;
			} // Of if
		} // Of for tempChild

		data[tempParent] = tempNode;

		System.out.println("Adjust " + paraStart + " to " + paraLength + ";" + this);
	}// Of adjustHeap

	/**
	 *********************
	 * Test the method.
	 *********************
	 */
	public static void heapSortTest() {
		int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);

		System.out.println(tempDataArray);

		tempDataArray.heapSort();
		System.out.println("Result\r\n" + tempDataArray);
	}// Of heapSortTest

运行结果

-------HeapSortTest-------
I am a data array with 7 items.
(5, if)  (3, then)  (6, else)  (10, switch)  (7, case)  (1, for)  (9, while)  
The parent position is2and the child is 6
Move 9to position 2
Adjust 2 to 7;I am a data array with 7 items.
(5, if)  (3, then)  (9, while)  (10, switch)  (7, case)  (1, for)  (6, else)  
The parent position is1and the child is 3
Move 10to position 1
Adjust 1 to 7;I am a data array with 7 items.
(5, if)  (10, switch)  (9, while)  (3, then)  (7, case)  (1, for)  (6, else)  
The parent position is0and the child is 1
Move 10to position 0
The parent position is1and the child is 4
Move 7to position 1
Adjust 0 to 7;I am a data array with 7 items.
(10, switch)  (7, case)  (9, while)  (3, then)  (5, if)  (1, for)  (6, else)  
The initial heap:I am a data array with 7 items.
(10, switch)  (7, case)  (9, while)  (3, then)  (5, if)  (1, for)  (6, else)  

The parent position is0and the child is 2
Move 9to position 0
The parent position is2and the child is 5
Adjust 0 to 6;I am a data array with 7 items.
(9, while)  (7, case)  (6, else)  (3, then)  (5, if)  (1, for)  (10, switch)  
Round 1:I am a data array with 7 items.
(9, while)  (7, case)  (6, else)  (3, then)  (5, if)  (1, for)  (10, switch)  
The parent position is0and the child is 1
Move 7to position 0
The parent position is1and the child is 4
Move 5to position 1
Adjust 0 to 5;I am a data array with 7 items.
(7, case)  (5, if)  (6, else)  (3, then)  (1, for)  (9, while)  (10, switch)  
Round 2:I am a data array with 7 items.
(7, case)  (5, if)  (6, else)  (3, then)  (1, for)  (9, while)  (10, switch)  
The parent position is0and the child is 2
Move 6to position 0
Adjust 0 to 4;I am a data array with 7 items.
(6, else)  (5, if)  (1, for)  (3, then)  (7, case)  (9, while)  (10, switch)  
Round 3:I am a data array with 7 items.
(6, else)  (5, if)  (1, for)  (3, then)  (7, case)  (9, while)  (10, switch)  
The parent position is0and the child is 1
Move 5to position 0
Adjust 0 to 3;I am a data array with 7 items.
(5, if)  (3, then)  (1, for)  (6, else)  (7, case)  (9, while)  (10, switch)  
Round 4:I am a data array with 7 items.
(5, if)  (3, then)  (1, for)  (6, else)  (7, case)  (9, while)  (10, switch)  
The parent position is0and the child is 1
Move 3to position 0
Adjust 0 to 2;I am a data array with 7 items.
(3, then)  (1, for)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
Round 5:I am a data array with 7 items.
(3, then)  (1, for)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
Adjust 0 to 1;I am a data array with 7 items.
(1, for)  (3, then)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
Round 6:I am a data array with 7 items.
(1, for)  (3, then)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
Result
I am a data array with 7 items.
(1, for)  (3, then)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值