Algorithms and data structures (16) heapsort

1 priority queue

Have mentioned before heap priority queues, link: https: //blog.csdn.net/qq_32193775/article/details/104115268

Foreword

Many applications need to be addressed and orderly elements, but does not necessarily require all of them ordered, for example, the vast majority of phone calls allocated to priority will be higher than the game program;

In this case an appropriate data structure should support both operations, remove the largest element and insert elements, this data type called a priority queue, use the priority queue and queue (delete the oldest elements) and stack (deleting latest elements) is similar, but it needs to have a weight, it is more efficient implementation challenges;

Important actions:

  1. delMax () to delete the largest element
  2. insert () insert elements
  3. less () helper, sizes

api:

MaxPQ () Create a priority queue
MaxPQ(int max) Create an initial capacity of max priority queue
MaxPQ(Key[] a) With a [] elements to create a priority queue
void insert(Key k) Priority queue insert elements
Key max() Returns the largest element
Key delMax() Delete the largest element
Boolean isEmpty() Returns whether the queue is empty
int size() Returns the number of elements in the priority queue

There is also delMin (), MinPQ () method and the like;

Primary realization

  1. Array achieve (disorder): The easiest way to stack-based, to achieve the maximum delete elements can be added within a loop code for a similar sort of choice, will exchange the largest element boundary element, then delete it, and the stack pop ( )similar;
  2. Array implementation (ordered): insert () to add the code, all the elements to the right a larger grid, so to maintain an orderly array (similar to insertion sort), so that one end of the largest element in the array, the maximum deleted elements on and pop () is similar to the;

Heap achieve

Binary heap data structure can well achieve the basic operation of a priority queue;

Review: Ordered stacks: a binary tree, each node is greater than two is equal to its child node ;

Complete binary tree arrays may be expressed as specific methods: in the root index = 1, its child nodes 2 and 3 at the sub child nodes at the node positions 4,5,6,7;

Therefore, the parent node of the node position k is k / 2 at which the child nodes respectively 2k, 2k + 1 at;

Heap algorithm

We use an array of length pq [] N + 1 to represent a heap of size N, we do not use pq [0], the operation is important is how to achieve this orderly stack; following auxiliary function codes

private boolean less(int i, int j){
  return pq[i].compareTo(pq[j])<0;
}
private void exch(int i,int j){
  Key t = qp[i];
  qp[i] = qp[j];
  qp[j] = t;
}

A bottom-ordered stack (floating)

If the heap ordered state, because a junction point becomes larger than its parent node is broken, then you need to exchange it and its parent node to repair the heap;

I.e., it is determined size and k / 2, if large, then the switch; however this node may still larger than the new parent node, loop required;

code show as below:

private void swim(int k){
  while(k>1&&less(k/2,k)){
    exch(k/2,k);
    k=k/2;
  }
}

From top to bottom of the heap ordered (sinking)

With the float, because a node becomes greater than its two sub-nodes or less one is broken; we need to go up with ways to repair the heap; code is as follows:

private void sink(int k){
  while(2*k<=N){//N为当前优先队列的大小
    int j = 2*k;
    if(j<N&&less(j,j+1)) j++;//找到两个子结点中的最大值;
    if(!less(k,j)) break;
    exch(k,j);
    k=j;
  }
}

Heap-based (array representation, complete binary tree) priority queue

public class MaxPQ<Key extends Comparable<Key>>{
  private Key[] pq;
  private int N=0;//size
  
  public MaxPQ(int max){
    pq=(Key[]) new Comparable[max+1];
  }
  public int size(){
    return N;
  }
  public void insert(Key v){
    pq[++N]=v;
    swim(N);
  }
  public Key delMax(){
    Key max = pq[1];
    excha(1,N--);
    pq[N+1] = null;
    sink(1);
    return max;
  }
  //注意其他方法见前述;
}

Heapsort

we

Can be any priority queue into a sorting method all the elements into a priority queue to find the smallest element; then repeated call to delete the smallest element operable to delete them (the same can also delete the largest element) sequence, with a stack-based the priority queue to do so is a new sort of way to do ---- heap sort;

In order to ensure consistent and said codes, all heap sort employed to remove the maximum element;

code show as below:

public static void sort(Comparable[] a){
  int N = a.length;
  for(int k = N/2 ; k>=1;k--){
    sink(a,k,N);
  }
  while(N>1){
    exch(a,1,N--);
    sink(a,1,N);
  }
}

Code explanation:

exch and sink method slightly modified,

The for loop pile achieve orderly array;

Then be known, the root node, so the need to first exchange the root node and ending positions for all elements of the maximum value, the root node is fixed, the length of the array of n-1 is not ordered, all to sink (because of root knot point lead to a small value); therefore calls sink method;

So as to achieve the effect of the ordering;

Time complexity: 2logN;

Published 17 original articles · won praise 0 · Views 352

Guess you like

Origin blog.csdn.net/qq_32193775/article/details/104226724