[Basic Python sorting algorithms] heap sorting

Source: Internet
Author: User

Heap is a Complete Binary Tree. Heap sorting is a tree-based sorting method that uses the biggest features of the elements on the top of the heap to continuously retrieve the largest elements, then, adjust the remaining elements to the top heap, and sort out the largest elements in order to sort the list. For example, the sequence [,] is sorted as follows:


The Code is as follows:
Def fixUp (a): # add new elements to the end of the heap, and set the condition k = len (a) For fixUp to restore the heap) -1 while k> 1 and a [k/2] <a [k]: a [k/2], a [k] = a [k], a [k/2] k = k // 2def fixDown (a): # Take the value returned by a [1] and move a [N] to a [1]. fixDown to restore the heap condition k = 1N = len (a)-1 while 2 * k <= N: j = 2 * kif j <N and a [j] <a [j + 1]: j + = 1if a [k] <a [j]: a [k], a [j] = a [j], a [k] k = jelse: breakdef insert (a, elem):. append (elem) fixUp (a) def delMax (a): maxElem = a [1] N = len (a) if N <= 1: print ('There \'s none element in the list') return-1if N = 2: return a [1] else: a [1] =. pop () fixDown (a) return maxElemdata = [-1,] # No first element, placeholder insert (data, 26) insert (data, 5) insert (data, 77) insert (data, 1) insert (data, 61) insert (data, 11) insert (data, 59) insert (data, 15) insert (data, 48) insert (data, 19) result = [] N = len (data)-1for I in range (N): print (data) result. append (delMax (data) print (result)

The fixUp function is used to add a new element to the end of the list and then adjust it to a large top heap. The fixDown function is used to retrieve the largest element of the Large top heap and place the element at the end of the list to the top of the heap, then it is adjusted to a large top heap. The insert function inserts a new element and changes it to a large top heap each time. The delMax function returns the largest element and changes the remaining element to a large top heap. The output is as follows:
[-1, 77, 61, 59, 48, 19, 11, 26, 1, 15, 5][-1, 61, 48, 59, 15, 19, 11, 26, 1, 5][-1, 59, 48, 26, 15, 19, 11, 5, 1][-1, 48, 19, 26, 15, 1, 11, 5][-1, 26, 19, 11, 15, 1, 5][-1, 19, 15, 11, 5, 1][-1, 15, 5, 11, 1][-1, 11, 5, 1][-1, 5, 1][-1, 1][77, 61, 59, 48, 26, 19, 15, 11, 5, 1]

The previous output is the big top heap after the largest element is retrieved constantly. Because it is a complete binary tree, you can write the tree structure of the Big Top heap by yourself based on the list, so I will not repeat it here, the last row is the sorted list.
Reprinted Please note:

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.