Turn: heap sort algorithm to explain more clearly

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https: //blog.csdn.net/u010452388/article/details/81283998
time complexity of heap sort O (N * logN), additional space complexity is O (1), is a sort of instability

table of Contents

A ready knowledge

1.1 large and small root root heap heap

Heap sort two basic steps

2.1 construction heap

2.2 reconfigurable fixed maximum stack

III summarizes

Four codes

 

A ready knowledge
stack structure can be divided into large and small root root heap heap is a complete binary tree, and heap sort is a sort heap data structure designed according to this, the following first look at what is the root of the heap big and small root stack

1.1 large root heap and stack rootlets
properties: each node values are greater than the values of the left child and right child nodes, the root is called a large stack; value of each node is less than its left child and right child node value, called rootlets stack. As shown below

 

 We number above each figure are labeled, the above configuration becomes mapped into an array like the following

 

There is also a basic concept: Finding a parent node in the array and the number of children around the node, such as the number of known index i, then

1. The parent node index: (i-1) / 2 (where the computer by 2, decimals omitted)

2. Children Left Index: 2 * i + 1

3. The right child index: 2 * i + 2

Therefore, the above two arrays can make piles brain structure, because they meet the definition of the nature of the stack:

大根堆:arr(i)>arr(2*i+1) && arr(i)>arr(2*i+2)

小根堆:arr(i)<arr(2*i+1) && arr(i)<arr(2*i+2)

Heapsort two basic steps
basic idea:

1. First, the array is configured to be sorted into a large heap root, at this time, to the top of the stack structure is a maximum value of the entire array

2. Number and the number of exchange of the tip end of this time, the end of the maximum number, the remaining number of the array to be sorted is n-1

3. The remaining n-1 re-configured to be larger than the number of root stack, then the top Number and n-1 switching position, so repeatedly performed, can obtain a sorted array

2.1 configured stack
disorderly array configured to stack a large root (root with a large heap ascending, descending on the small heap root)

Consider the following array

 

The main idea: the first position to ensure 0~0 large root file structure (nonsense), 0 to 1 to ensure that the second position of the large root stack structure, the third position to ensure that large root 0-2 stack structure until assurance 0 ~ ... N- 1 position of the large root file structure (each newly inserted data with the parent node, and if the insertion of the number of points greater than the parent node, then the switch with the parent node, or has been switched up until less parent node, or He came to the top)

6, when inserted, 6 his parent node is greater than 3, i.e. arr (1)> arr (0), then the switch; At this time, to ensure that the 0-1 position is a large root stack structure, as shown below:

 

                                     (Tips: to be exchanged for the number of blue, green number after the exchange)

 8, when inserted, is greater than 8 parent node 6, i.e. arr (2)> arr (0), then the switch; At this time, to ensure that the 0-2 position is a large root stack structure, following FIG.

 

5, when inserted, its parent node 3 is greater than 5, then the switch, after switching, 5 also found smaller than 8, it is not switching; At this time, the 0-3 positions to ensure large root stack structure, following FIG. 

 

7, when inserted, its parent node 7 is greater than 5, then the switch, after switching, 7 and 8 is smaller than found, it is not switching; at this time the entire array has a large root stack structure 

 

 

2.2 reconfigurable fixed maximum stack
point, we've got a big heap root, and the top will be the number of the last digit of the exchange, then the remaining number of further configured to stack a large root

 

                                    (Tips: Black is a fixed good numbers, no longer involved in sorting) 

 In this case the maximum number of 8 has come to the end, then fixed, behind only the data required to operate the top, take the number of its top about a large number of children are compared, if the number is greater than its top left and right of the child than a large number of stops, if the number is less than the top of the left and right large number of children, the exchange, and then continue following the children compared with the

Below, the left and right child. 5, left child 7 is larger than the right child of 6, then 5 and 7 were compared, 5 <7, then the switch; Following the exchange, found that 5 has been greater than his left child, described the number of remaining have large root stack configuration, the back is fixed repetition maximum, and large root stack configured

 

FIG follows: Number 7 to the top end of the number of exchanged 3, 7 fixed,

 

The remaining number of large root heap start configuration, the top end of the Number and then exchanges, fixed maximum value and then large root stack structure, the above operation is repeatedly performed, eventually resulting ordered array

 

 

III summarizes
here, we should sort heap have their own opinions, our summary of the above processes:

1, the first array configured without a big heap root (the newly inserted data is compared with its parent node)

2, a fixed maximum, the number of remaining re-configured to stack a large root, such a process is repeated

Four codes
Code two main methods:

1, the array is sorted to be configured into a large heap root (element rises)

2, a fixed maximum value, then the number of the remaining root is configured as a large stack (element drops)

Heap sort //
public static void heapSort (int [] ARR) {
// stack configured large root
heapInsert (ARR);
int size = arr.length;
the while (size>. 1) {
// fixed maximum
swap (arr, 0, size -. 1);
size--;
// stack configured large root
heapify (ARR, 0, size);

}

}

// root large stack configuration (increased by the number of newly inserted)
public static void heapInsert (int [] ARR) {
for (int I = 0; I <arr.length; I ++) {
// index of the currently inserted
int the currentIndex = I;
// parent node index
int fatherIndex = (the currentIndex -. 1) / 2;
// if the current value of the inserted value is greater than its parent node, the exchange of value, and an index pointing to a parent node
// parent node and then continues above the comparison value until the parent node is not greater than, the loop is exited
while (arr [currentIndex]> arr [fatherIndex]) {
// current switching node and the parent node value
the swap (ARR, the currentIndex, fatherIndex);
// The current index pointing to a parent index of
the currentIndex = fatherIndex;
// index is recalculated parent index
fatherIndex = (the currentIndex -. 1) / 2;
}
}
}
// the number of remaining configuration larger than the root stack (lowered by several top)
public static void heapify (int [] ARR, int index, int size) {
int left = 2 * index +. 1;
int right = 2 * index + 2;
the while (left <size ) {
int largestIndex;
// child Analyzing larger index value (to ensure that the right child in the size range)
IF (ARR [left] <ARR [right] && right <size) {
largestIndex = right;
} the else {
largestIndex = left;
}
// value of the parent node of the child is larger in value, and determines the maximum index
IF (ARR [index]> ARR [largestIndex]) {
largestIndex = index;
}
// If the parent node index is an index to the maximum value, it has a large root of the heap, then the loop is exited
IF (index == largestIndex) {
BREAK;
}
// parent node is not the maximum value, the larger the value of children switching
the swap (ARR, largestIndex, index);
// index to the index value is larger child
index = largestIndex;
// child recalculated after exchange index
left index = 2 * +. 1;
right index = 2 * + 2;
}

}
// exchange two values in the array elements
public static void the swap (int [] ARR, I int, int J) {
int ARR TEMP = [I];
ARR [I] = ARR [J];
arr [J] = the TEMP;
}
                                                              tips: watch mobile phone, can slide 
----------------
Disclaimer: This article is the original article CSDN bloggers "Ah Gu students", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/u010452388/article/details/81283998

Guess you like

Origin www.cnblogs.com/swing07/p/11444312.html