Data Structure example-heap sorting process

Source: Internet
Author: User

Data Structure example-heap sorting process

For the complete algorithm, see [routine]. This article uses an example to demonstrate the heap sorting process.

For example, heap sorting is performed on {57, 40, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7.

The algorithm is as follows:

Void HeapSort (RecType R [], int n) {int I; RecType temp; // (1) create an initial heap for (I = n/2; I> = 1; I --) sift (R, I, n); // (2) perform n-1 cycles to complete the push sorting for (I = n; I> = 2; I --) {temp = R [1]; // Replace the first element with R [1] In the current interval with R [1] = R [I]; R [I] = temp; sift (R, 1, I-1); // screening R [1] node, get the I-1 node heap }}

(1) Establish the initial heap cyclically

For (I = n/2; I> = 1; I --) sift (R, I, n );

The initial state of heap construction with the given sequence is as follows:

Based on the above Code, the goal is to get a large heap from the last branch node. The process is as follows:

The storage structure of this heap is:

(2) perform n-1 cycles to complete push sorting

For (I = n; I> = 2; I --) {temp = R [1]; // The maximum value is switched to the last R [1] = R [I]; R [I] = temp; sift (R, 1, I-1); // adjust unordered area to heap}

The process diagram is as follows:

Continue to complete the painting.

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.