Implement binary heap and heap sorting in python, and python heap sorting

Source: Internet
Author: User

Implement binary heap and heap sorting in python, and python heap sorting

Implement binary heap and heap sorting in python

Heap is a special tree structure. The data storage in the heap meets certain heap sequence. Heap sorting is a kind of sorting. Its algorithm complexity and time complexity have great advantages over other sorting algorithms.

The heap is divided into the big head heap and the small head heap. Just like its name, the first element of the big head heap is the largest. Each parent node with a child node has a larger data value than its child node. The opposite is true for a small heap.

I will explain the algorithm process for creating a tree heap:
Find the array data at the N/2 position, start from this position, find the index of the left sub-node of the node, first compare the sub-node under this node, find the largest, assign the index of the largest child node to the left child node, and then compare the largest child node with the parent node. If the index is larger than the parent node, exchange data with the parent node. Of course, I just mentioned the implementation below. In this process, we also need to consider the absence of nodes. Check the Code:

# Build a binary heap def binaryHeap (arr, lenth, m): temp = arr [m] # the value of the current node while (2 * m + 1 <lenth ): lchild = 2 * m + 1 if lchild! = Lenth-1 and arr [lchild] <arr [lchild + 1]: lchild = lchild + 1 if temp <arr [lchild]: arr [m] = arr [lchild] else: break m = lchild arr [m] = temp def heapsort (arr, length): I = int (len (arr)/2) while (I> = 0): binaryHeap (arr, len (arr), I) I = I-1 print ("the physical sequence of the binary heap is:") print (arr) # output physical sequence of the binary heap if _ name _ = '_ main _': arr = [2, 87, 39, 49, 34, 62, 53, 6, 44, 98] heapsort (arr, len (arr ))

The heap sorting process is to compare and exchange the last node and the first node in sequence:

# Build a binary heap def binaryHeap (arr, lenth, m): temp = arr [m] # the value of the current node while (2 * m + 1 <lenth ): lchild = 2 * m + 1 if lchild! = Lenth-1 and arr [lchild] <arr [lchild + 1]: lchild = lchild + 1 if temp <arr [lchild]: arr [m] = arr [lchild] else: break m = lchild arr [m] = tempdef heapsort (arr, length): I = int (len (arr)/2) while (I> = 0): binaryHeap (arr, len (arr), I) I = I-1 print ("the physical sequence of the binary heap is:") print (arr) # output binary heap physical sequence I = length-1 while (I> 0): arr [I], arr [0] = arr [0], arr [I] # variable exchange binaryHeap (arr, I, 0) I = I-1560def pop (arr): first = arr. pop (0) return firstif _ name _ = '_ main _': arr = [2, 87, 39, 49, 34, 62, 53, 6, 44, 98] heapsort (arr, len (arr) print ("physical order after heap sorting") print (arr) # output physical sequence data after heap sorting = pop (arr) print (data) print (arr)

Python encapsulates a heap module. We can use this module to efficiently implement a priority queue:

Import heapqclass Item: def _ init _ (self, name): self. name = name def _ repr _ (self): return 'item ({! R })'. format (self. name) class PriorityQueue: def _ init _ (self): self. _ queue = [] self. _ index = 0 def push (self, item, priority): heapq. heappush (self. _ queue, (-priority, self. _ index, item) # store a triple self. _ index + = 1 def pop (self): return heapq. heappop (self. _ queue) [-1] # if _ name _ = '_ main _': p = PriorityQueue () p. push (Item ('foo'), 1) p. push (Item ('bar'), 5) p. push (Item ('spam'), 4) p. push (Item ('grok'), 1) print (p. pop () print (p. pop ())

For more information, see the official heapq website.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.