Heap sorting (php implementation)

Source: Internet
Author: User
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);?>

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.