Java eight of the sort heap sorting algorithm

First, the moving map presentation

 

 

Second, the idea of ​​analysis

 

  The first stack to understand the concepts: heap is a complete binary tree with the following properties: the value of each node is equal to or greater than the value of its left and right child nodes, referred to as large a top stack; or value of each node are value less than or equal to its left child node, called small stack top. As shown below:

Meanwhile, we stack node are numbered layer, which maps the logical structure into an array will look like

The array is logically a heap structure, we use a simple formula to describe the stack definition is:

大顶堆:arr[i] >= arr[2i+1] && arr[i] >= arr[2i+2]  

小顶堆:arr[i] <= arr[2i+1] && arr[i] <= arr[2i+2]  

Understand these definitions. Then look at the basic idea and the basic steps Heap Sort:

The basic idea of ​​the sort heap and steps

  The basic idea is HEAPSORT: collating sequence to be configured into a large pile top, at this time, the maximum value of the root node is the top of the stack of the entire sequence. To be exchanged with the last element on the end of this time the maximum value. The remaining n-1 th element of a stack is reconfigured, it would receive the next smallest value of n elements. So again executed, it will be able to get an ordered sequence of

A step configuration initial heap. Given sequence disorder configured to stack a large top (top stack with large ships ascending, descending a small stack top).

  a. Suppose the given sequence disorder following structure

2. At this point we have from the last non-leaf nodes start (natural leaf nodes without adjustment, the first non-leaf node arr.length / 2-1 = 5 / 2-1 = 1, which is below 6 knot point), from left to right, from the bottom is adjusted.

4. find the second non-leaf nodes 4, since [4,9,8] is the largest element 9, 9 and 4 exchange.

In this case, leading to the exchange sub-root [4,5,6] chaotic structure, continue to adjust the [4,5,6] Maximum 6, 4 and 6 exchange.

At this point, we will construct a sequence without having to become a top big heap.

Step two end elements and the top of the stack element exchange, so that the maximum end of the element. Then continue to adjust the stack, then the top of the stack elements and switching elements at the end, to give a second large element. So again exchanged, rebuild, exchange.

a. The end 9 and the top of the stack element exchange elements 4

b. re-adjust the structure, so that it continues to meet the definition of heap

c.再将堆顶元素8与末尾元素5进行交换,得到第二大元素8.

后续过程,继续进行调整,交换,如此反复进行,最终使得整个序列有序

再简单总结下堆排序的基本思路:

  a.将无序序列构建成一个堆,根据升序降序需求选择大顶堆或小顶堆;

  b.将堆顶元素与末尾元素交换,将最大元素"沉"到数组末端;

  c.重新调整结构,使其满足堆定义,然后继续交换堆顶元素与当前末尾元素,反复执行调整+交换步骤,直到整个序列有序。

 

三、复杂度分析

1.  时间复杂度:堆排序是一种选择排序,整体主要由构建初始堆+交换堆顶元素和末尾元素并重建堆两部分组成。其中构建初始堆经推导复杂度为O(n),在交换并重建堆的过程中,需交换n-1次,而重建堆的过程中,根据完全二叉树的性质,[log2(n-1),log2(n-2)...1]逐步递减,近似为nlogn。所以堆排序时间复杂度最好和最坏情况下都是O(nlogn)级。

2.  空间复杂度:堆排序不要任何辅助数组,只需要一个辅助变量,所占空间是常数与n无关,所以空间复杂度为O(1)

 

import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[]{4,6,8,5,9};
        //从最后一个非叶节点开始构建大顶堆
        for (int i = arr.length/2-1; i >=0; i--) {
            maximumHeap(i,arr);
        }
        //从最小的叶子节点开始与根节点进行交换并重新构建大顶堆
        for (int i = arr.length-1; i >=0; i--) {
            swap(arr,0,i);
            maximumHeap(0,arr);
        }
        System.out.println(Arrays.toString(arr));
    }
    //构建大顶堆
    public static void maximumHeap(int i,int[] arr){
        int temp = arr[i];
        for (int j = i*2+1; j < arr.length; j=j*2+1) {
            //如果右孩子大于做孩子,则指向右孩子
            if(j+1<arr.length && arr[j+1]>arr[j]){
                j++;
            }
            //如果最大的孩子大于当前节点,则将大孩子赋给当前节点,修改当前节点为其大孩子节点,再向下走。
            if(arr[j]>temp){
                arr[i] = arr[j];
                i = j;
            }else{
                break;
            }
        }
        //将temp放到最终位置
        arr[i] = temp;
    }
    //交换
    public static void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159803.htm