在学校里,我们正在学习Java的排序算法,我得到了我的功课堆排序。我做了我读书,我试图找出尽可能多尽我所能,但似乎我不能把握的概念。
At school we are currently learning sorting algorithms in Java and I got for my homework the Heap Sort. I did my reading, I tried to find out as much as I could, but it seems I just can't grasp the concept.
我不是让你给我写一个Java程序,如果你能向我解释那样简单,你可以怎么堆排序工作。
I'm not asking you to write me a Java program, if you could just explain to me as simply as you can how the Heap Sort works.
右,所以基本上你把一堆,然后拉出第一个节点堆 - 作为第一个节点被保证是最大/最小的视排序的方向。棘手的事情是重新平衡/创建摆在首位堆。
Right, so basically you take a heap and pull out the first node in the heap - as the first node is guaranteed to be the largest / smallest depending on the direction of sort. The tricky thing is re-balancing / creating the heap in the first place.
是需要两个步骤,我明白堆过程 - 首先这想法作为一棵树,让我的头周围,然后转向该树到一个数组,因此它可能是有用的。
Two steps were required for me to understand the heap process - first of all thinking of this as a tree, getting my head around it, then turning that tree into an array so it could be useful.
的该第二部分是基本上首先遍历树广度,从左到右添加每个元件到阵列。所以,下面的树:
The second part of that is to essentially traverse the tree breadth first, left to right adding each element into the array. So the following tree:
73
7 12
2 4 9 10
1
将{73,7,12,2,4,9,10,1}
Would be {73,7,12,2,4,9,10,1}
的第一部分需要两个步骤:
The first part requires two steps:
确保每个节点有两个孩子(除非你没有足够的节点做,因为在上面的树。 确保每个节点较大(或者,如果第一次分拣分钟小于)其子。因此,要heapify号码列表你们每个人添加到堆,然后按照以这两个步骤。
So to heapify a list of numbers you add each one to the heap, then following those two steps in order.
要创建我的堆上面,我会添加10首 - 它是唯一的节点,以便无关。 添加12,因为它是孩子在左:
To create my heap above I will add 10 first - it's the only node so nothing to do. Add 12 as it's child on the left:
10
12
这满足1,而不是2,所以我会交换他们圆:
This satisfies 1, but not 2 so I will swap them round:
12
10
添加7 - 无关
Add 7 - nothing to do
12
10 7
添加73
12
10 7
73
10 LT; 73所以需要调换的:
10 < 73 so need to swap those:
12
73 7
10
12 LT; 73所以需要调换的:
12 < 73 so need to swap those:
73
12 7
10
添加2 - 无关
Add 2 - nothing to do
73
12 7
10 2
添加4 - 无关
Add 4 - nothing to do
73
12 7
10 2 4
添加9
73
12 7
10 2 4 9
7 LT; 9 - 交换
7 < 9 - swap
73
12 9
10 2 4 7
添加1 - 无关
Add 1 - nothing to do
73
12 9
10 2 4 7
1
我们有我们的堆:D
现在你只从顶部取出每个元素,在最后一个元素每次交换到树的顶部,然后重新平衡树:
Now you just remove each element from the top, swapping in the last element to the top of the tree each time, then re-balancing the tree:
取73关 - 把1在它的位置
Take 73 off - putting 1 in its place
1
12 9
10 2 4 7
1 LT; 12 - 所以交换它们
1 < 12 - so swap them
12
1 9
10 2 4 7
1 LT; 10 - 所以交换它们
1 < 10 - so swap them
12
10 9
1 2 4 7
取12关 - 更换7
Take 12 off - replace with 7
7
10 9
1 2 4
7 LT; 10 - 交换它们
7 < 10 - swap them
10
7 9
1 2 4
取10关 - 更换4
Take 10 off - replace with 4
4
7 9
1 2
4℃ 7 - 交换
4 < 7 - swap
7
4 9
1 2
7 LT; 9 - 交换
7 < 9 - swap
9
4 7
1 2
以9折 - 更换2
Take 9 off - replace with 2
2
4 7
1
2版; 4 - 交换它们
2 < 4 - swap them
4
2 7
1
4℃ 7 - 交换它们
4 < 7 - swap them
7
2 4
1
以7折 - 更换1
Take 7 off - replace with 1
1
2 4
1 LT; 4 - 交换它们
1 < 4 - swap them
4
2 1
取4 - 取代1
Take 4 - replace with 1
1
2
1 LT; 2 - 交换它们
1 < 2 - swap them
2
1
取2 - 替换1
Take 2 - replace with 1
1
取1
排序列表中提琴。