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.