Sorting Algorithm (4)---Heap Sort (Selection Sort)

Heap sort: heap sort belongs to selection sort Definition of

heap : A sequence with n elements (k1, k2...kn) satisfies:

1: Ki =< K2i and Ki =< K2i+1 (small top heap)

or

2: Ki >= K2i and Ki >= K2i+1 (large top heap)

i = (1,2,...n/2) The

basic idea
is to form a sequence of n numbers to be sorted into a heap, and put the top of the heap into a heap. Element output (at this time, the top element of the heap, that is, the root element is the largest or smallest), then the remaining n-1 elements are formed into a heap again, and the top element of the heap is output again...
until the heap with only two nodes, Swap them to get an ordered sequence of n nodes

Operation : Two problems need to be solved:
1: How to build a heap
2: After outputting the top element of the heap, how to form a new heap for the remaining n-1 elements

first Look at the solution of the second problem:
1) Send the bottom element of the heap to the top of the heap (the last element is swapped with the top of the heap), the heap is destroyed
2) The root node is connected with the smallest (or largest) in the left and right subtrees Elements are exchanged
3) If exchanged with the left subtree, if the root node of the left subtree does not satisfy the heap definition, repeat 2)
4) If it is exchanged with the right subtree, if the root node of the right subtree does not satisfy the heap definition, Repeat 2)
5) Continue to adjust the nodes that do not meet the definition of the heap until the leaf node is completed.

Look at
the first question, the heap is completed 1) A complete binary tree with n nodes, the last node is the nth / 2-node subtree
2) Screening starts with the subtree rooted at the n/2th node and builds the subtree into a heap
3) Continue to n/2-1 nodes, repeat 2) until n/2-1=1

Algorithm complexity:
worst case close to worst case: O(n*logn)

Stability:
unstable

python code : heap_sort.py
#coding:utf-8
def build_heap(l, length):
    for i in range((length-1)/2, -1, -1):
        adjust_heap(l, i, length)

#l is the sequence to be sorted
#s is the position of the array element to be adjusted
#length is the length of the array
#This is the small top heap sort
def adjust_heap(l, s, length):
    tmp = l[s]
    child = 2*s + 1 #The position of the left child node
    while(child < length):
        if(child+1 < length and l[child] < l[child+1]):
            child += 1 #Find the smallest element in the left and right nodes
        if l[s] < l[child]:
            l[s] = l[child]
            s = child #Reset s, that is, the position of the next node to be adjusted
            child = 2*s + 1
        else:
            break
        l[s] = tmp

def heap_sort(l, length):
    #initialize heap
    build_heap(l, length)

    # Adjust the sequence starting from the last element
    for i in range(length - 1, -1, -1):
        tmp = l[i] #heap top element and heap bottom element exchange
        l[i] = l[0]
        l[0] = tmp
        adjust_heap(l, 0, i) #Note the size i of the heap at this time

if __name__ == '__main__':
    l = [3,1,5,7,2,4,9,6,10,8]
    heap_sort(l, 10)
    print('result:' + str(l))

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326592962&siteId=291194637