Basic steps of heap sorting: 1. Create a unordered sequence as a heap. 2: swap the heap top element and the last element. After the swap, reset the heap due to the heap structure failure. The difference between the reset heap after the heap initialization and the SWAp is that when the heap is initialized, the node seat is adjusted from the last non-leaf node, after switching the heap top element, you only need to adjust the seat of the heap top element .? Ph
Basic steps of heap sorting: 1. Create a unordered sequence as a heap. 2: swap the heap top element and the last element. After the swap, reset the heap due to the heap structure failure. The difference between the reset heap after the heap initialization and the SWAp is that when the heap is initialized, the node seat is adjusted from the last non-leaf node, after switching the heap top element, you only need to adjust the seat of the heap top element. ? Ph
Basic steps of heap sorting:
1: Create a heap of unordered sequences.
2: swap the heap top element and the last element. After the swap, reset the heap due to the heap structure failure.
The difference between the reset heap after the heap initialization and the SWAp is that when the heap is initialized, the node seat is adjusted from the last non-leaf node, after switching the heap top element, you only need to adjust the seat of the heap top element.
0; $ end --) {// exchange the heap top and last element $ tmp = $ arr [$ end]; $ arr [$ end] = $ arr [0]; $ arr [0] = $ tmp; // adjust the heap. The heap top element destroys the Heap Structure adjustHeap ($ arr, 0, $ end-1 ); // $ end-1} return $ arr;} function initHeap (& $ arr) {$ len = count ($ arr); // the last non-leaf node starts, to the root node for ($ start = floor ($ len/2)-1; $ start> = 0; $ start --) {adjustHeap ($ arr, $ start, $ len-1);}/** $ arr to be adjusted array * $ start subscript of the node to be adjusted (different from the number in the binary tree,-1) * $ end subscript */function adjustHeap (& $ arr, $ start, $ end) {$ max = $ start; $ Lchild_index = 2 * ($ start + 1)-1; $ rchild_index = 2 * ($ start + 1); if ($ lchild_index <= $ end) {if ($ arr [$ lchild_index]> $ arr [$ max]) {$ max = $ lchild_index ;} if ($ rchild_index <= $ end & $ arr [$ rchild_index]> $ arr [$ max]) {$ max = $ rchild_index;} if ($ max! = $ Start) {$ tmp = $ arr [$ start]; $ arr [$ start] = $ arr [$ max]; $ arr [$ max] = $ tmp; adjustHeap ($ arr, $ max, $ end) ;}$ arr = array (, 8); echo count ($ arr ); print_r (heapSort ($ arr);?>