The example in this paper describes how Python implements heap ordering. Share to everyone for your reference, as follows:
Heap sorting is one of the basic sorting methods, similar to a merge sort rather than an insert sort, which runs at O (Nlogn), like an insert sort rather than a merge sort, which is an in-place sorting algorithm that occupies only a constant number of element spaces in addition to the input array.
heap (definition):(binary) The heap data structure is an array object that can be treated as a complete binary tree. If the value of the root node is greater than (less than) all other nodes, and its left and right subtree satisfies this nature, then the heap is a large (small) Gan.
We assume that a heap is represented by array A, a[1] is the root of a tree, and the subscript I of a given node can be computed for the parent node, the left child, and the right child's subscripts:
PARENT (i):
Return I/2
Left (i):
Return 2i
Right (i):
Return 2i+1
Heap sort Python implementation
The so-called heap sequencing process, is to put some unordered objects, gradually build up a heap of process.
The following is the code for the heap sort implemented in Python:
def build_max_heap (to_build_list): "" "Build A Heap" "# Bottom up heap for I in range (len (to_build_list)/2-1,-1,-1): Max_heap (t O_build_list, Len (to_build_list), i) def max_heap (To_adjust_list, Heap_size, index): "" Adjusts the elements in the list to ensure that the heap with index root is a maximum heap "" # Compare the current node with its left and right child nodes, swap the larger nodes with the current node, and then recursively adjust the subtree Left_child = 2 * index + 1 right_child = left_child + 1 if Left_child < Heap_size and To_adjust_list[left_child] > To_adjust_list[index]: largest = left_child else:largest = Index If Right_child < heap_size and To_adjust_list[right_child] > To_adjust_list[largest]: largest = Right_child if Lar Gest = Index:to_adjust_list[index], to_adjust_list[largest] = \ To_adjust_list[largest], To_adjust_list[index] Max_heap (to_adjust_list, Heap_size, largest) def heap_sort (to_sort_list): "" "Heap Sort" "# First adjust the list to heap build_max_heap (To_sort_ List) Heap_size = Len (to_sort_list) # The first element of the adjusted list is the largest element in the list, swaps it with the last element, and then adjusts the remaining list to the maximum heap for I in range (Len (to_sort_li ST)-1, 0,-1): To_sort_lisT[i], to_sort_list[0] = to_sort_list[0], to_sort_list[i] heap_size-= 1 max_heap (to_sort_list, heap_size, 0) if __nam e__ = = ' __main__ ': to_sort_list = [4, 1, 3, 2, +, 9, ten,, 8, 7] Heap_sort (to_sort_list) Print to_sort_list
Read more about Python topics: python Regular expression Usage summary, Python data structure and algorithm tutorials, Python socket Programming Tips Summary, Python function Usage tips summary, "How-to" Python string manipulation Tips Summary, Python Introductory and Advanced classic tutorials, and Python file and directory operations tips
I hope this article is helpful for Python program design.