Stack and heap sort: Why did not the sort heap row fast fast?

Stack and heap sort: Why did not the sort heap row fast fast?

Stack (HEAP) is an in situ, the time complexity is O (nlogn) sorting algorithm

In the actual software development, fast performance ranked than the heap sort Why good?

How to understand the heap?

Meet these two points is a heap

  • Heap is a complete binary tree
  • Value for each node of the heap must be greater than equal to (or less) value for each node in its subtree

Value for each node is greater than or equal sub-stacks in each node of the value tree, called the "big top stack", otherwise "small pile top"

How to achieve a heap?

Which operating stack support? If you store a heap?

Complete binary tree for an array to store, only the left and right child node pointers stored, simply by the array index, can be found left child node and the parent node of a node

Array subscript i of the left child node, that is, the node labeled 2i, is the right child node labeled 2i + 1 of the parent node is labeled node i / 2 of

The core stack operation is to insert a stack top of the stack element and delete elements (typically large stack top)

1. To insert a stack element

After inserting an element, it is necessary to continue to meet the two characteristics of the heap, but if new elements are inserted into the heap Finally, do not meet the characteristics of the heap (such as the newly inserted element is relatively large, do not comply with Article characteristics of), so we need to be adjusted to meet again let properties, a process called heap of

There are two types of reactor, up from the bottom and from the top down, talk about the stack up from the method: the node is located along the path, up or down, contrast, and then exchange

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;
  }
  
  public void insert(int data){
    if(count >= n )  return;//堆满了
    ++count;
    a[count] = data;
    int i = count;
    while(i/2 > 0 && a[i] > a[i/2]){    //自下往上堆化
      swap(a,i,i/2);   //swap()函数作用:交换小标为i 和i/2的两个元素
      i=i/2;
    }
  }
}
2. Remove the top of the heap element

The last node into the top of the stack, and then by the same method of Comparative parent and child nodes, parent-child node does not meet the size relationship, two nodes exchange, between the parent and child nodes until satisfied until the magnitude relation, which is from top to bottom heap method.

We removed the last element in the array, and in the heap of the process, all swap operation, no "hole"

public void removeMax(){
  if(count == 0) return -1;   //堆中没有数据
  a[1] = a[count];   //把最后一个元素放到堆顶
  --count;   //堆的规模-1
  heapify(a,count,1);   //堆化完成,count是长度
}

public 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+1;
    if(maxPos == i) break;
    swap(a ,i ,maxPos);
    i = maxPos;
  }
}

Contains a complete binary tree of n nodes, not exceeding log2n height of the tree, the stack of the node is located along the process path to comparator exchanged, so that the stack of the complex with time proportional to the height of the tree, i.e. O (log2n) , inserting a fill top of the stack element and delete elements of the time complexity is O (logn)

How to implement heap sort?

Sorted by time complexity is O (nlogn), heap sort of process down into two major steps: construction of the stack and sort

1. Built heap

The place to build a stack array is operating in the original array

A: Insert an element in the stack idea, assume that initially only one stack data, index data is 1, the insertion call, the subscript 2 to n data sequentially inserted into the stack, this will the data organization n data pile up

II: leaf node down dialogue can only compare themselves with their own, so we started directly from a non-leaf node, it can turn the heap of

public 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;
	}
}

Starting from n / 2 to 1 data of the stack, showing the lower n / 2 + 1 to n nodes are leaf nodes of the stack do not need

In fact, the time complexity of the stack building process heap sort is O (n)

Not a leaf node of the heap, it is necessary to stack a second layer of nodes from the countdown has begun, each node in the heap of the process, the number of nodes required to compare and exchange with the node is proportional to the height k

2. Sort

After the construction of the heap, the data array is in accordance with the characteristics of the big top of the heap organized

The first element in the array is the top of the heap, which is the largest element, with the last element of the exchange, that would put the largest element at index position n, when the top of the heap element removal, the subscript n is an element of into the top of the stack, and then through the stack of the remaining n-1 elements to rebuild, after stack, re-take the top of the stack element, n-1, into the position, repeating, until there is a marked 1 element

//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);
	}
}

Heap sort algorithm to sort are not stable, 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

In the actual development, why so fast parallelism heapsort performance?

A: heap sort data access mode is not as fast friendship row

Fast row is sequentially accessed data, heap sort data is accessed jumping

II: for the same data, the sorting process, the data exchange heap sort algorithm is faster than the number of rows

Published 76 original articles · won praise 9 · views 9192

Guess you like

Origin blog.csdn.net/ywangjiyl/article/details/104400691