Sorting algorithms: HEAPSORT (rpm)

Preliminaries

Heapsort

  Heap sort using heap An Algorithm, heap sort such data structure is a design choice sort its worst, preferably, the average time complexity are O (nlogn), which is also the sort of instability. First, a simple understanding of the structure under the heap.

stack

  Heap is a complete binary tree with the following properties: the value of each node is equal to or greater than the left and right child node values, called the large top stack; each node or a value less than or equal to its left child node value, known as the 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]  

ok, we understand these definitions. Next, let's 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.

步骤二 将堆顶元素与末尾元素进行交换,使末尾元素最大。然后继续调整堆,再将堆顶元素与末尾元素交换,得到第二大元素。如此反复进行交换、重建、交换。

a.将堆顶元素9和末尾元素4进行交换

b.重新调整结构,使其继续满足堆定义

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

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

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

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

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

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

代码实现

public class HeapSort {
    public static void main(String []args){
        int []arr = {9,8,7,6,5,4,3,2,1};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void sort(int []arr){
        //1.构建大顶堆
        for(int i=arr.length/2-1;i>=0;i--){
            //从第一个非叶子结点从下至上,从右至左调整结构
            adjustHeap(arr,i,arr.length);
        }
        //2.调整堆结构+交换堆顶元素与末尾元素
        for(int j=arr.length-1;j>0;j--){
            swap(arr,0,j);//将堆顶元素与末尾元素进行交换
            adjustHeap(arr,0,j);//重新对堆进行调整
        }

    }

    /**
     * 调整大顶堆(仅是调整过程,建立在大顶堆已构建的基础上)
     */
    public static void adjustHeap(int []arr,int i,int length){
        int temp = arr[i];//先取出当前元素i
        for(int k=i*2+1;k<length;k=k*2+1){//从i结点的左子结点开始,也就是2i+1处开始
            if(k+1<length && arr[k]<arr[k+1]){//如果左子结点小于右子结点,k指向右子结点
                k++;
            }
            if(arr[k] >temp){//如果子节点大于父节点,将子节点值赋给父节点(不用进行交换)
                arr[i] = arr[k];
                i = k;
            }else{
                break;
            }
        }
        arr[i] = temp;//将temp值放到最终的位置
    }

    /**
     * 交换元素
     */
    public static void swap(int []arr,int a ,int b){
        int temp=arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
}

最后

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

 

转自:https://www.cnblogs.com/chengxiao/p/6129630.html

Guess you like

Origin www.cnblogs.com/helios-fz/p/11008685.html