Heap Sort algorithm of analysis

Heapsort

Heap sort it is a kind of sorting algorithm uses the heap of features designed. (Complete binary tree, the value of each node are child nodes greater than about (or less than))

 

Heap sort process can be roughly broken down into two steps: build and heap sorting.

 

Built heap

We can build a heap array place. That is, without the aid of in situ with the other array, operating in the original array of built heap

There are two methods: if the original array has n data, it can be assumed initially contains only one data array, index data is 1.

Then the index n is from 2 to data sequentially inserted into the stack. Such an array will contain n data, and organized into stacks.

This process is built stack processing data from front to back, which is a stack from the bottom of the stack building process

 

The second method and a process in contrast, is a process data from the back. That is, when each data is inserted down the stack from the stack of.

Because the leaf node can not heap of down from above, so starting with the first non-leaf node, in turn heap of.

 

 

 

Sequence

After the construction of the heap, the first element of the array is the top of the heap, which is the largest element.

It will be with the last element exchange, the largest element that would put the subscript n positions.

Sort similar to the "Remove top of the stack elements", the subscript n of the element into the top of the stack, and then through the stack of the way, the remaining elements of the n-1 reconstructed piles.

After the stack is complete, we then take the top of the stack of elements into a position index is n-1,.

This process is repeated until the last heap only labeled element 1, the sort of work is complete.

 

 

 

 

Heap sort process

Efficiency Heap Sort

Built heap

Stack of leaf nodes do not need, it is necessary to start from the stack of the penultimate layer node.

Each node of the process stack, the number of nodes need to compare and exchange, with a height proportional to this node k.

 

Each non-leaf node height sum, is the following formula:

Subtraction dislocation

The left and right formula are multiplied by 2, we get another formula S2. We will align misaligned S2, with S1 and S2 is subtracted, S. obtained

 

And S is part of a geometric sequence, summing geometric sequence

Since h = log2 n, into the formula S, can be obtained S = O (n), therefore, the time complexity of the stack construction is O (n). 

 

Sequence

Sorting process is similar to "delete top of the heap element", so the time complexity of the sorting process is O (nlogn)

 

Sort and stack construction comprising two sorting operation, the stack building process time is the time complexity of O (n), the sorting process complexity is O (nlogn).

Therefore, the entire heap sort time complexity is O (nlogn).

 

Heap Sort space consumption

Heap sort, whether built or heap sort, it is carried out in the original array, only need very few temporary storage space, so the place is sort heap sorting algorithm.

 

Heap sort of stability

Heap sort is not a stable sorting algorithm.

Because the sorting process, the presence of the last node of the stack top of the stack with the operating node interchangeable, it becomes possible to change the value of the original data of the same relative order.

 

Heap Sort code implementation


public class Heap {
  private int[] a; // 数组,从下标1开始存储数据
  private int n;  // 堆可以存储的最大数据个数
  private int count; // 堆中已经存储的数据个数
 
  public Heap(int capacity) {
    a = new int[capacity + 1];
    n = capacity;
    count = 0;
  }
  //对于完全二叉树来说,下标从 2n​+1 到 n 的节点都是叶子节点。叶子结点不需要堆化
  private static void buildHeap(int[] a, int n) {
    for (int i = n/2; i >= 1; --i) {
      heapify(a, n, i);
    }
  }

  private static void heapify(int[] a, int n, int i) {
    while (true) {
      int maxPos = i;
      if (i*2 <= n && a[i] < a[i*2]) maxPos = i*2;
      if (i*2+1 <= n && a[maxPos] < a[i*2+1]) maxPos = i*2+1;
      if (maxPos == i) break;
      swap(a, i, maxPos);
      i = maxPos;
    }
  }  
  // n表示数据的个数,数组a中的数据从下标1到n的位置。
  public static void sort(int[] a, int n) {
    buildHeap(a, n);
    int k = n;
    while (k > 1) {
      swap(a, 1, k);
      --k;
      heapify(a, k, 1);
    }
  }
}

 

The above code is assumed from the data stack array subscript 1 is stored as the start position.

If that starts at 0 storage, processing idea is actually no change.

The only change when the code is implemented, under the formula for calculating the target child and parent nodes are changed.

If the node is the subscript i, the left child node index that is 2 * i + 1, the subscript of the right child is 2 * i + 2, the subscript is the parent node (i-1) / 2.

Published 113 original articles · won praise 25 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_42006733/article/details/104694805