Heap sort detailed analysis of source code

        Introduced before several sorting algorithms, today to talk about the heap sort algorithm. Although heap sort not used in practice, often defeated quicksort efficiency, but the advantages of heap sort is independent of the data input, the time complexity stable O (N * lgN), unlike the fast row, the worst case the time complexity is O (N 2 ).

  Notes for heap sort of the premise is to grasp the concept of a binary tree, self Baidu, this article does not describe.

        Speaking heap sort, we first need to understand a data structure - heap. Stack is a complete binary tree, which can be generally represented by an array structure. In practice, the stack can be divided into minimum and maximum heap stack, the difference is as follows:

  - max-heap Property: For all except the root node (root) node I, A [ P A R & lt E n- T (I) ] A [ I ]

  -min -heap Property: For all except the root node (root) node I, A [ P A R & lt E n- T (I) ] A [ I ]

          For a heap represented by an array, the parent-child relationship between the index of each node is as follows:

Parent of the node index i is: the Parent (i) P A R & lt E n- T ( i ) A [ F L O O R & lt ( ( i - . 1 ) / 2 ) ]

左孩子:Left(i)left(i)A[2i+1]

Right child: Right (I) R & lt I G H T ( I ) A [ 2 * I + 2 ]

     

    Complete understanding of the basic concepts of the heap after heap can begin to sort of. Heap sort can be divided into two steps:

1, the array to be sorted, according to the definition of the stack, constructed as a stack.

2, sort: Before removing the top of the stack elements (maximum or minimum), the switching element of the last stack position, and reconstituted heap; In this step, a top element is removed after each heap size will be minus 1.

Repeat step 2 until the heap size of up to 1, then only one stack of elements, all the elements of a large (or small) than it had been in the array are arranged in order, the sort is complete.

The following analysis at the maximum heap example.

In the above process, a key function uses: Construction --_ maxHeapify stack to an element of the parent element, the source and the method detailed as follows. specific:

. 1  void _maxHeapify <E the extends the Comparable <E >> (List <E> A, int i, int size) {
 2    // parent node i, the largest element is assumed that the parent node; left child of l, right child of R & lt; 
. 3    var mi the = I, L = (I <<. 1) +. 1, R & lt = (I <<. 1) + 2 ;
 . 4  
. 5    // find the parent, left, and right in the point most 
. 6    IF (L <size && A [L] .compareTo (A [mi The])> 0) = mi The L;
 . 7    IF (R & lt <size && A [R & lt] .compareTo (A [mi The])> 0) = mi The R & lt;
 . 8  
. 9    // if parent node is not the maximum, then the maximum switching node to the parent node, node i to complete the maximum heap constructed; 
10    IF (! = mi the i) {
 . 11      _swap (a, i, mi the);
12 is  
13 is    // Note that, after the exchange, the maximum heap nature mi its corresponding child nodes may be damaged, and therefore need recursive call
 14    // to ensure orderly stack; 
15      _maxHeapify (A, mi, size);
 16    }
 17 }

    For an array, the maximum stack build process, starting from the parent element is the last element, a recursive build up until the root element A [0]. Specific code as follows:

. 1  void _buildMaxHeap <E the extends the Comparable <E >> (List <E> A) {
 2    // starting from the last parent node, one by one upward 
. 3    for (I = var (a.length >>. 1) -. 1; I> = 0; i-- ) _maxHeapify (A, I, a.length);
 . 4 }

     Finally, start using heap sort, detailed code analysis is as follows:

. 1  void heapSort <E the extends the Comparable <E >> (List <E> A) {
 2    // Step: Construction of the stack 
. 3    _buildMaxHeap (A);
 . 4  
. 5    // I +. 1 represents the size of each loop pile stack. size minus 1;
 6    // when only one stack element, sorting ends. In this case the array is in ascending order 
. 7    for (var = a.length I -. 1; I> 0; i-- ) {
 . 8      // each removal element stack top (largest element) in the tail reactor. 
. 9      _swap (A, 0 , I);
 10      
. 11      // element exchange properties may break up the stack, the stack thus reconstructed; 
12 is      _maxHeapify (A, 0 , I);
 13 is    }
 14 }

    Finally, attach the swap code:

1 void _swap<E>(List<E> a, int i, int j) {
2   var t = a[i];
3   a[i] = a[j];
4   a[j] = t;
5 }

    从以上代码也可以看出,堆排序也不需要额外的存储空间,因此,堆排序的空间复杂度为O(1).

    测试代码如下:

 1 import 'dart:math';
 2 import 'package:data_struct/sort/heap_sort.dart';
 3 
 4 void main() {
 5   var rd = Random();
 6   // 随机生成一个数组
 7   List<num> a = List.generate(12, (_) => rd.nextInt(200));
 8 
 9   // 原始数组
10   print(a);
11   print('---------------------------------');
12 
13   // 排序
14   heapSort(a);
15   // 排序后数组
16   print(a);
17 }

    输出结果:

1 [182, 9, 105, 86, 181, 87, 166, 98, 90, 142, 192, 176]
2 ---------------------------------
3 [9, 86, 87, 90, 98, 105, 142, 166, 176, 181, 182, 192]
4 Exited

 

Guess you like

Origin www.cnblogs.com/outerspace/p/11098461.html