Heapsort problem

Heapsort

Heapsort is built on the concept of a binary tree sort, use a binary tree complete binary tree; two kinds of perfect binary tree structure to complete HEAPSORT

About the concept of complete binary tree can go to access some here not described in detail, based on perfect binary tree from top to bottom, from left to right of way can be converted into a storage array structure, each node is a corresponding array index .

basic concept:

  0. heap sort heap into the big top, the top of a small heap, each heap is descending and ascending (top of the heap> about | top of the heap <about)

  1. In accordance with the index 0 starts from top to bottom, a complete binary tree of N nodes, a non-leaf node of the last N / 2 - 1, the range of non-leaf nodes = [0, N / 2 -1]

  2. The relationship between the non-leaf nodes (N) and the point of his left (L) and right (R) node: L = N * 2 + 1 R = N * 2 + 2

  3. Heap Sort: we look from the bottom, each time to complete a non-leaf nodes of the heap sort, the maximum value of the root node, the exchange value of the root node and the last node

For the derivation from the last non-leaf nodes like:

/ ** 
* us from most non-leaf nodes under the leftmost start building the heap, a heap leaf node itself.
* We need to build a heap last non-leaf node
* /
void heap public (int [] ARR) { 
int A = arr.length / 2-1; // find the last non-leaf node index
int l = a * 2 + 1 ; // left child node index
int r = a * 2 + 2; // right child node index
int I = 0;
iF (ARR [a] <ARR [L] || ARR [a] <ARR [R & lt]) {// we have determined that it does only when a ratio wherein small when we come Analyzing
IF (ARR [L]> R & lt) {
I = ARR [a];
ARR [a] = ARR [L];
ARR [L] = I;
} the else IF (ARR [L] < ARR [R & lt]) {
I = ARR [A];
ARR [A] = ARR [R & lt];
ARR [R & lt] = I;
}
}
}
/ ** 
* penultimate non-leaf node
* /
void heap_1 public (int [] ARR) { 
int A = arr.length / 2-2; // find the non-leaf nodes penultimate index
int l = a * 2 + 1 ; // left child node index
int r = a * 2 + 2; // right child node index
int I = 0;
iF (ARR [A] <ARR [L] || ARR [A] <ARR [R & lt]) {// we determined only if it is indeed better than wherein a small when we come Analyzing
IF (ARR [L]> R & lt) {
I = ARR [a];
ARR [a] = ARR [L];
ARR [L] = I;
} the else IF (ARR [L ] <ARR [R & lt]) {
I = ARR [A];
ARR [A] = ARR [R & lt];
ARR [R & lt] = I;
}
}
}
Question: When we change the second non-leaf node, if the node and non-leaf nodes that depend on how the previous deal? 
   If there is a dependency description left or the right, we just need to pile on the non-leaf nodes exchange took place the node to determine whether the node is a non-leaf node, because the characteristics of complete binary tree, if a non-leaf node, then there must be a left node.
  / ** 
* be perfect for the code, heap_2 is a complete process of building a large stack top
* solve the problem: If you create dependency how to handle / control through all the non-leaf nodes / The code can be used as building the top of the heap , but the lack of optimization heap_2
* * /
public heap_2 void () {
int I = 0;
for (int = a arr.length / 2-1; a> = 0; a -) {// the control of non-leaf nodes traversing
C int = a;
the while (. 1 + 2 * C <arr.length) {// this is our C heap top node, if the exchange takes place, we will exchange node and the top of the stack node replacement
int l . 1 + 2 * C =;
int R & lt = 2 + 2 * C;
IF (ARR [C] <ARR [L] || ARR [C] <ARR [R & lt]) {
IF (ARR [L]> ARR [R & lt ]) {
I = ARR [c];
ARR [c] = ARR [l];
ARR [l] = I;
c = l; // if l and c are swapped
IF the else} (L <r) {
I = ARR [c];
ARR [c] = ARR [r];
ARR [r] = I;
c = r; // if r and c are swapped
}
}
}
}
}
/ ** 
* Optimized Code
* * /
public void heap_3(){
int i = 0;
for(int a=arr.length/2-1;a>=0;a--){
int c = a;
int b = a;
while((c = c * 2 + 1) < arr.length){
if(arr[c] < arr[c+1]){
c++;
}
if(arr[b] < arr[c]){
i = arr[b];
arr[b] = arr[c];
arr[c] = i;
b = c;
}
}
}
}
When we oriented the large / small pile been constructed from the top, the need is to keep the top of the stack and the end of the exchange, and interspersed among the large heap dependent remodeling / small pile
/ ** 
* at the full version of the heap sort
* * /
public void heap_4 (int [] ARR, C int, int length) {
int I = ARR [C];
for (int = B + 2 * C. 1; B <length; B = C * 2 +. 1) {
B = B +. 1 <length ARR [B] <ARR [B +. 1] B +. 1: B:?? B;
IF (ARR [B]> I) {
ARR [C] = ARR [B];
C = B;
}
}
ARR [C] = I;
}
public void heap_5 () {
int TEMP = 0;
// big top pile finishing
for (int a = arr. length / 2-1; A> = 0; A -) {
heap_4 (ARR, A, arr.length);
}
// exchanged
for (int b = arr.length; b > 0; b -) {
temp = arr [0];
ARR [0] = ARR [. 1-B];
ARR [. 1-B] = TEMP;
  // sort from top to bottom, the top of the stack as it has a large, now only needs to be changed dependent node heap big top processing

heap_4 (ARR, 0,. 1-B);
}
}
 
 
 
 

Guess you like

Origin www.cnblogs.com/gongzhichang/p/11906620.html