Eight of the Java heap sort sort

HEAPSORT (English: Heapsort) refers to a sorting algorithm such a data structure designed for use heap. A stack is nearly complete binary tree structure, properties and satisfy deposited: key or index that is a child node is always less than (or greater than) its parent node.

Depending on whether the root key is the maximum or minimum value, and whether the child nodes is smaller or larger than its parent node can be divided into two stacks, as follows:

  1. Large top stack: each key node are smaller than its parent node;

  2. Small top stack: each node key is greater than its parent;

HEAPSORT basic idea:

  1. The top of stack array arranged in a large (or small stacks top);

  2. The tail and root node exchange, then the n-1 element reconstruct a large stack top (or small stacks top);

  3. Repeat step 2 until the last element.

Flowchart (to less cumbersome, so that such data is reduced):

First, sort the array larger than the first stack top

Second, the root node and the end switching

Code:

 1 /**
 2  * 堆排序
 3  */
 4 public class TestHeap {
 5     //创建大顶堆
 6     public static void maximumHeap(int k, int[] arr,int m) {
 7         int i, j, temp;
 8         boolean finished;
 9         i = k;
10         j = 2*i+1;
11         temp = arr[k];
12         finished = false;
13         while((j <= m) && !finished){
14             //孩子结点位置小于数组长度 且 右孩子大于左孩子
15             if ((j < m) && (arr[j+1] > arr[j])){
16                 j++;    //把右孩子结点当作最大孩子结点
17             }
18             //如果最大的孩子结点小于其根节点
19             if (temp >= arr[j]){
20                 finished = true;    //结束循环
21             }else{
22                 //把最大子节点赋给根结点
23                 arr[i] = arr[j];
24                 i = j;
25                 //循环下一个根结点
26                 j = 2*j+1;
27             }
28         }
29         arr[i] = temp;
30     }
31 
32     //交换
33     public static void heapsort(int[] arrays) {
34         int i;
35         //从最后一个非叶节点开始构建大顶堆
36         for (i = arrays.length / 2 - 1; i >= 0; i--) {
37             maximumHeap(i, arrays, arrays.length);
38         }
39         System.out.println("建堆后:" + Arrays.toString(arrays));
40         //从最小的叶子节点开始与根节点进行交换并重新构建大顶堆
41         for (i = arrays.length - 1; i >= 1; i--) {
42             int temp = arrays[i];
43             arrays[i] = arrays[0];
44             arrays[0] = temp;
45             maximumHeap(0, arrays,i-1);
46         }
47         System.out.println(Arrays.toString(arrays));
48     }
49 
50     public static void main(String[] args) {
51         int[] arrays = {49, 38, 65, 97, 176, 213, 227, 49, 78, 34, 12, 164, 11, 18, 1};
52         heapsort(arrays);
53     }
54 }

测试结果:

建堆后:[227, 176, 213, 97, 38, 164, 65, 49, 78, 34, 12, 49, 11, 18, 1]
[1, 11, 12, 18, 34, 38, 49, 49, 65, 78, 97, 164, 176, 213, 227]

时间复杂度:堆排序方法对记录数n较大的文件排序是很好的,但当n较小时,不提倡使用,因为初始建堆和调整建新堆时要进行反复的筛选。堆排序时间复杂度为O(nlog2n)。

堆排序只需存放一个记录的附加空间。

堆排序是一种不稳定的排序算法。

结语:堆排序是为了即要保存中间比较结果,减少后面的比较次数,又不占用大量的附加存储空间,使排序算法具有较好的性能才被Willioms和Floyd在1964年提出的。

 

Guess you like

Origin www.cnblogs.com/mtgh/p/11374434.html