Java Sorting Algorithm (III): heap sorting, java Sorting Algorithm heap sorting

**Java Sorting Algorithm (3) Heap sorting**

HeapSort is a sort algorithm designed by the structure of the accumulation tree. It can quickly locate the elements of a specified index using the characteristics of arrays. Heap sorting is an unstable sorting method. The auxiliary space is O (1). The worst time complexity is O (nlog2n)

The average performance of heap sorting is closer to the worst performance.

Heap sorting utilizes the largest (or least) keyword of the top record of a large (or small) Heap heap. This simplifies the selection of records with the maximum or minimum keyword in the unordered area.

(1) sorting of the largest heap

· 1. First, build the initial file R [1. n] into a large root heap, which is the initial unordered zone.

2. Replace the record R [1] with the largest keyword, that is, the record R [n] of the heap top and unordered area. The new unordered zone R [1 .. n-1] And the ordered zone R [n] are obtained, and the value meets R [1 .. n-1]. keys <= R [n]. key.

3. Because the New Root R [1] After switching may violate the heap nature, the current unordered zone R [1 .. n-1] changed to heap, and then re-set R [1 .. n-1] exchange with the last record R [n + 1] in the region. The new disordered zone R [1, N-2] and ordered zone are obtained.

R [1, N-2]. keys <= R [n, n-1]. keys, also adjust R [1 .. N-2] to heap

There is only one element in the unordered area.

(2) Basic operations on sorting of large root heaps

1. Initialization operation. The R [1.n] is constructed as the initial heap.

2. For each basic sorting operation, the heap top record R [1] In the unordered area is exchanged with the last record in the interval. Then adjust the unordered area to the heap.

Note:

1. You only need to perform n-1 sort and select a large n-1 keyword to make the file ascending and orderly.

2. the sorting of the small root heap is similar to that of the large root heap. However, the sorting result is in descending order. The heap sorting is opposite to the direct selection sorting. In heap sorting at any time, the unordered area is always before the ordered area. In addition, the ordered area gradually expands to the whole vector until the right and back of the original vector.

**Code Implementation**

Package com. spring. test; import java.util.zip. dataFormatException;/*** heap sorting */public class HeapSortTest {public static void main (String [] args) {int [] data5 = new int [] {5, 3, 6, 2, 1, 9, 4, 8, 7}; print (data5); heapSort (data5); System. out. println ("sorted array"); print (data5);} public static void heapSort (int [] data) {for (int I = 0; I <data. length; I ++) {createMaxdHeap (data, data. length-1-i); swap (data, 0, data. length-1-i); print (data);}/*** exchange data */public static void swap (int [] data, int I, int j) {if (I = j) {return;} data [I] = data [I] + data [j]; data [j] = data [I]-data [j]; data [I] = data [I]-data [j];} /*** print Output */public static void print (int [] data) {for (int I = 0; I <data. length; I ++) {System. out. print (data [I] + "\ t");} System. out. println ();}/*** large root heap judgment */public static void createMaxdHeap (int [] data, int lastIndex) {for (int I = (lastIndex-1)/2; i> = 0; I --) {// Save the node int k = I currently being judged; // if the child node of the current node has a while (2 * k + 1 <= lastIndex) {// biggerIndex always records the value of a large node, first assign a value to the left subnode of the current judgment node int biggerIndex = 2 * k + 1; if (biggerIndex <lastIndex) {// if the right subnode exists, otherwise, biggerIndex should be equal to lastIndex if (data [biggerIndex] <data [biggerIndex + 1]) {// if the value of the right subnode is greater than that of the left subnode, biggerIndex records the value of the right subnode biggerIndex ++;} if (data [k] <data [biggerIndex]) {/*** if the value of the current node is smaller than the maximum value of the subnode, the values of the two are exchanged. After the exchange, biggerIndex is assigned to K **/swap (data, k, biggerIndex); k = biggerIndex;} else {break ;}}}}}

**Sorting result**