最大堆的堆顶是整个堆中的最大元素。
最小堆的堆顶是整个堆中的最小元素。
以最大堆为例,如果删除一个最大堆的堆顶(并不是完全删除,而是跟末尾的节点交换位置),经过自我调整,第2大的元素就会被交换上来,成为最大堆的新堆顶。
Class PHA.YX.Arithmetic.HeapSort Extends %RegisteredObject
{
/// 下沉调整
Method downAjustArrayDesc(array As %ArrayOfDataTypes, parentIndex As %Integer, length As %Integer)
{
#dim temp as %Integer = array.GetAt(parentIndex)
#dim childIndex as %Integer = 2 * parentIndex + 1
while(childIndex < length){
;w "childIndex i:"_childIndex _ " array.GetAt(childIndex + 1):" _array.GetAt(childIndex + 1)_ " array.GetAt(childIndex):" _array.GetAt(childIndex),!
/* 如果有右孩子,且右孩子小于左孩子的值,则定位到右孩子 */
if (((childIndex + 1) < length)&&(array.GetAt(childIndex + 1) < array.GetAt(childIndex))){
s childIndex = childIndex + 1
}
;b:temp=1
;w "temp:"_temp _ " array.GetAt(childIndex):"_array.GetAt(childIndex),!
;w "childIndex:"_childIndex,!
;w array.GetAt(childIndex),!
/* 如果父节点小于任何一个孩子的值,直接跳出 */
if (temp <= array.GetAt(childIndex)){
quit /* 这一定是quit 而不是continue */
}
/* 无需真正交换,单向赋值即可 */
d array.SetAt(array.GetAt(childIndex), parentIndex)
s parentIndex = childIndex
s childIndex = 2 * childIndex + 1
}
d array.SetAt(temp, parentIndex)
}
/// 下沉调整
Method downAjustArrayAsc(array As %ArrayOfDataTypes, parentIndex As %Integer, length As %Integer)
{
#dim temp as %Integer = array.GetAt(parentIndex)
#dim childIndex as %Integer = 2 * parentIndex + 1
while(childIndex < length){
;w "childIndex i:"_childIndex _ " array.GetAt(childIndex + 1):" _array.GetAt(childIndex + 1)_ " array.GetAt(childIndex):" _array.GetAt(childIndex),!
/* 如果有右孩子,且右孩子小于左孩子的值,则定位到右孩子 */
if (((childIndex + 1) < length)&&(array.GetAt(childIndex + 1) > array.GetAt(childIndex))){
s childIndex = childIndex + 1
}
;b:temp=1
;w "temp:"_temp _ " array.GetAt(childIndex):"_array.GetAt(childIndex),!
;w "childIndex:"_childIndex,!
;w array.GetAt(childIndex),!
/* 如果父节点小于任何一个孩子的值,直接跳出 */
if (temp >= array.GetAt(childIndex)){
quit /* 这一定是quit 而不是continue */
}
/* 无需真正交换,单向赋值即可 */
d array.SetAt(array.GetAt(childIndex), parentIndex)
s parentIndex = childIndex
s childIndex = 2 * childIndex + 1
}
d array.SetAt(temp, parentIndex)
}
Method heapSortDesc(array As %ArrayOfDataTypes)
{
/* 从最后一个非叶子节点开始,依次下沉调整 */
f i = (array.Count() - 2) \ 2 : -1 : 0 d
.;w "i:"_ i,!
.d ..downAjustArrayDesc(array, i, array.Count())
zw array
f i = (array.Count() - 1) : -1 : 1 d
.s temp = array.GetAt(i)
.d array.SetAt(array.GetAt(0), i)
.d array.SetAt(temp, 0)
.d ..downAjustArrayDesc(array, 0, i)
q array
}
Method heapSortAsc(array As %ArrayOfDataTypes)
{
/* 从最后一个非叶子节点开始,依次下沉调整 */
f i = (array.Count() - 2) \ 2 : -1 : 0 d
.;w "i:"_ i,!
.d ..downAjustArrayAsc(array, i, array.Count())
zw array
f i = (array.Count() - 1) : -1 : 1 d
.s temp = array.GetAt(i)
.d array.SetAt(array.GetAt(0), i)
.d array.SetAt(temp, 0)
.d ..downAjustArrayAsc(array, 0, i)
q array
}
}
Method heapSortDesc(array As %ArrayOfDataTypes)
{
/* 从最后一个非叶子节点开始,依次下沉调整 */
f i = (array.Count() - 2) \ 2 : -1 : 0 d
.;w "i:"_ i,!
.d ..downAjustArrayDesc(array, i, array.Count())
zw array
f i = (array.Count() - 1) : -1 : 1 d
.s temp = array.GetAt(i)
.d array.SetAt(array.GetAt(0), i)
.d array.SetAt(temp, 0)
.d ..downAjustArrayDesc(array, 0, i)
q array
}
DHC-APP>w ##class(PHA.YX.Arithmetic).HeapSortDesc()
array=<OBJECT REFERENCE>[1@%Library.ArrayOfDataTypes]
+----------------- general information ---------------
| oref value: 1
| class name: %Library.ArrayOfDataTypes
| reference count: 3
+----------------- attribute values ------------------
| Data(0) = 0
| Data(1) = 1
| Data(2) = 2
| Data(3) = 6
| Data(4) = 3
| Data(5) = 7
| Data(6) = 8
| Data(7) = 9
| Data(8) = 10
| Data(9) = 5
| ElementType = "%String"
+-----------------------------------------------------
array=<OBJECT REFERENCE>[1@%Library.ArrayOfDataTypes]
+----------------- general information ---------------
| oref value: 1
| class name: %Library.ArrayOfDataTypes
| reference count: 2
+----------------- attribute values ------------------
| Data(0) = 10
| Data(1) = 9
| Data(2) = 8
| Data(3) = 7
| Data(4) = 6
| Data(5) = 5
| Data(6) = 3
| Data(7) = 2
| Data(8) = 1
| Data(9) = 0
| ElementType = "%String"
+-----------------------------------------------------
Method heapSortAsc(array As %ArrayOfDataTypes)
{
/* 从最后一个非叶子节点开始,依次下沉调整 */
f i = (array.Count() - 2) \ 2 : -1 : 0 d
.;w "i:"_ i,!
.d ..downAjustArrayAsc(array, i, array.Count())
zw array
f i = (array.Count() - 1) : -1 : 1 d
.s temp = array.GetAt(i)
.d array.SetAt(array.GetAt(0), i)
.d array.SetAt(temp, 0)
.d ..downAjustArrayAsc(array, 0, i)
q array
}
DHC-APP>w ##class(PHA.YX.Arithmetic).HeapSortAsc()
array=<OBJECT REFERENCE>[1@%Library.ArrayOfDataTypes]
+----------------- general information ---------------
| oref value: 1
| class name: %Library.ArrayOfDataTypes
| reference count: 3
+----------------- attribute values ------------------
| Data(0) = 10
| Data(1) = 9
| Data(2) = 8
| Data(3) = 6
| Data(4) = 5
| Data(5) = 7
| Data(6) = 2
| Data(7) = 3
| Data(8) = 1
| Data(9) = 0
| ElementType = "%String"
+-----------------------------------------------------
array=<OBJECT REFERENCE>[1@%Library.ArrayOfDataTypes]
+----------------- general information ---------------
| oref value: 1
| class name: %Library.ArrayOfDataTypes
| reference count: 2
+----------------- attribute values ------------------
| Data(0) = 0
| Data(1) = 1
| Data(2) = 2
| Data(3) = 3
| Data(4) = 5
| Data(5) = 6
| Data(6) = 7
| Data(7) = 8
| Data(8) = 9
| Data(9) = 10
| ElementType = "%String"
+-----------------------------------------------------