Introduction to Algorithms sorting algorithm

Heapsort

Array is a binary heap can be seen as a complete binary tree approximation. Binary heap can be divided into two forms: the maximum and minimum heap heap.
The maximum stack, the maximum heap property refers to all except the root node i is satisfied: $ A [PARENT [i] ]> = A [i] $
That is, the value of as much as a node to its parent node as large, therefore, the maximum element of the stack stored in the root node, and, in any subtree, the values of all the sub-tree nodes is not greater than the value contained in the root node of the subtree.

The minimum heap property refers to all except the root node i is satisfied: $ A [PARENT [i] ] <= A [i] $
heap smallest element is stored in the root node.

The height of the stack is $ lg n $, n is the number of nodes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"" "Maintaining the nature of the heap: 
input A and an array subscript i, when calling MaxHeapify, assume root left subtree LEFT (i) and right subtree RIGHT (i) of the binary tree is the biggest heap,
but this when a [i] may be smaller than its children, which violates the biggest heap of nature.
invocation by value MaxHeapify let a [i] in the largest heap "stepping down" so that the subscript i is the root node subtree again follow the maximum heap properties.
time complexity: $ T (n-) Leq T (2N /. 3) + O (. 1), T (n-) = O (LG n-), H = LG n-$
"" "
DEF (A, I, size) :
left = I << . 1
right = (I << . 1 ) + . 1
Largest = I

if left<size and A[left]>A[i]:
largest=left
if right<size and A[right]>A[largest]:
大专栏  算法导论 排序算法 largest=right
if largest!=i:
A[i],A[largest]=A[largest],A[i]
MaxHeapify(A, largest, size)

"" "Stack construction:
since the bottom-up method using MaxHeapify, to a size of n = A.length array A [1 ... n] is converted into the maximum heap.
Subarray A [lfloor n / 2 rfloor + 1. . leaf nodes ..n] elements are in each leaf node of the tree are considered as only one element of the maximum heap.
time complexity: O (n), the linear time to a maximum unordered array configured heap
"" "
DEF (A, size) : for I in Range ( 0 , len (A) // 2 ) [:: -1 ]: MaxHeapify (A, I, size) return A




"" "Heap sort algorithm:
. BuildMaxHeap with the input array A [0 ... n-1] to construct the max heap, where n = A.length
because the largest element in the array in the root total A [0] in , it is performed by the a [n-1] are interchangeable, so that the element in the right place.
If the node is removed from the stack n (A.heapsize-1), the remaining nodes, the root node of the original when the child node is still the largest heap, and
the latest root may violate the maximum heap nature, to maintain maximum heap property, to call MaxHeapify (a, 0, size) ,
so that the a [0 ... n-2 configuration] a new maximum heap until the heap size to 2 n-1 from the
time complexity: $ O (n lg n) $, BuildMaxHeap is O (n), n-1 times call MaxHeapify $ O (lg n-) $
"" "
DEF HeapSort (A) :
size = len (A)
BuildMaxHeap (A, size)

for i in range(1, len(A))[::-1]:
A[0],A[i]=A[i],A[0]
size-=1
MaxHeapify(A, 0, size)
return A

if __name__=='__main__':
import random
print( "Now displaying HeapSort")
s = random.randint(5, 10)
A = []
for i in range(0, s):
A.append(random.randint(0, 1000))
print (A)
print (HeapSort(A))



Guess you like

Origin www.cnblogs.com/liuzhongrong/p/11986087.html