heapsort python

Solutions on MaxInterview for heapsort python by the best coders in the world

showing results for - "heapsort python"
Luigi
24 Nov 2016
1def buildHeap(lista, n):
2    for i in range(n//2 - 1, -1, -1):
3        heapify(lista, n, i)
4
5def heapify(lista, n, i):
6    largest = i  
7    left = (2 * i) + 1    
8    right = (2 * i) + 2 
9
10    if left < n and lista[largest] < lista[left]:
11        largest = left
12
13    if right < n and lista[largest] < lista[right]:
14        largest = right
15
16    if largest != i:
17        lista[i], lista[largest] = lista[largest], lista[i] 
18        heapify(lista, n, largest) 
19
20def heapSort(lista):
21    n = len(lista)
22    buildHeap(lista, n)
23    
24    for i in range(n-1, 0, -1):
25        lista[i], lista[0] = lista[0], lista[i]
26        heapify(lista, i, 0)
Oasis
15 Mar 2017
1#Implementing Heap Using Heapify Method in Python 3
2#MaxHeapify,MinHeapify,Ascending_Heapsort,Descending_Heapsort
3class heap:
4    
5    def maxheapify(self,array):
6        n=len(array)
7        for i in range(n//2-1,-1,-1):
8            self._maxheapify(array,n,i)
9            
10            
11    def _maxheapify(self,array,n,i):
12        l=2*i+1
13        r=2*i+2
14        if l<n and array[l]>array[i]:
15            largest=l
16        else:
17            largest=i
18        if r<n and array[r]>array[largest]:
19            largest=r
20        if (largest!=i):
21            array[largest],array[i]=array[i],array[largest]
22            self._maxheapify(array,n,largest)
23            
24            
25    def minheapify(self,array):
26        n = len(array)
27        for i in range(n//2-1,-1,-1):
28            self._minheapify(array,n,i)
29            
30            
31    def _minheapify(self,array,n,i):
32        l=2*i+1
33        r=2*i+2
34        if l<n and array[l]<array[i]:
35            smallest = l
36        else:
37            smallest = i
38        if r < n and array[r]<array[smallest]:
39            smallest = r
40        if (smallest != i):
41            array[smallest], array[i] = array[i], array[smallest]
42            self._minheapify(array, n, smallest)
43            
44            
45    def descending_heapsort(self,array):
46        n = len(array)
47        for i in range(n // 2 - 1, -1, -1):
48            self._minheapify(array, n, i)
49        for i in range(n - 1, 0, -1):
50            array[0], array[i] = array[i], array[0]
51            self._minheapify(array, i, 0)
52
53
54    def ascending_heapsort(self,array):
55        n=len(array)
56        for i in range(n//2-1,-1,-1):
57            self._maxheapify(array,n,i)
58        for i in range(n-1,0,-1):
59            array[0],array[i]=array[i],array[0]
60            self._maxheapify(array,i,0)
61
62b=[550,4520,3,2340,12]
63a=heap()
64
65a.maxheapify(b)
66print('Max Heapify -->',b)
67
68a.minheapify(b)
69print('Min Heapify -->',b)
70
71a.ascending_heapsort(b)
72print('Ascending Heap Sort -->',b)
73
74a.descending_heapsort(b)
75print('Descending Heap Sort -->',b)
Diego
29 Jul 2016
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Created on Sun Mar 10 18:18:25 2019
5
6@source: https://www.geeksforgeeks.org/heap-sort/
7
8"""
9# Python program for implementation of heap Sort 
10
11# To heapify subtree rooted at index i. 
12# n is size of heap 
13def heapify(arr, n, i): 
14	largest = i # Initialize largest as root 
15	l = 2 * i + 1	 # left = 2*i + 1 
16	r = 2 * i + 2	 # right = 2*i + 2 
17
18	# See if left child of root exists and is 
19	# greater than root 
20	if l < n and arr[i] < arr[l]: 
21		largest = l 
22
23	# See if right child of root exists and is 
24	# greater than root 
25	if r < n and arr[largest] < arr[r]: 
26		largest = r 
27
28	# Change root, if needed 
29	if largest != i: 
30		arr[i],arr[largest] = arr[largest],arr[i] # swap 
31
32		# Heapify the root. 
33		heapify(arr, n, largest) 
34
35# The main function to sort an array of given size 
36def heapSort(arr): 
37	n = len(arr) 
38
39	# Build a maxheap. 
40	for i in range(n, -1, -1): 
41		heapify(arr, n, i) 
42
43	# One by one extract elements 
44	for i in range(n-1, 0, -1): 
45		arr[i], arr[0] = arr[0], arr[i] # swap 
46		heapify(arr, i, 0) 
47
48heapSort(arr) 
49 
50
Miguel Ángel
05 Aug 2020
1// @see https://www.youtube.com/watch?v=H5kAcmGOn4Q
2
3function heapify(list, size, index) {
4    let largest = index;
5    let left = index * 2 + 1;
6    let right = left + 1;
7    if (left < size && list[left] > list[largest]) {
8        largest = left;
9    }
10    if (right < size && list[right] > list[largest]) {
11        largest = right;
12    }
13    if (largest !== index) {
14        [list[index], list[largest]] = [list[largest], list[index]];
15        heapify(list, size, largest);
16    }
17    return list;
18}
19
20function heapsort(list) {
21    const size = list.length;
22    let index = ~~(size / 2 - 1);
23    let last = size - 1;
24    while (index >= 0) {
25        heapify(list, size, --index);
26    }
27    while (last >= 0) {
28        [list[0], list[last]] = [list[last], list[0]];
29        heapify(list, --last, 0);
30    }
31    return list;
32}
33
34heapsort([4, 7, 2, 6, 4, 1, 8, 3]);
Joaquín
22 Jan 2020
1Implementation of heap sort in C:
2
3#include <stdio.h>
4int main()
5{
6   int heap[10], array_size, i, j, c, root, temporary;
7   printf("\n Enter size of array to be sorted :");
8   scanf("%d", &array_size);
9   printf("\n Enter the elements of array : ");
10   for (i = 0; i < array_size; i++)
11      scanf("%d", &heap[i]);
12   for (i = 1; i < array_size; i++)
13   {
14       c = i;
15       do
16       {
17           root = (c - 1) / 2;            
18           if (heap[root] < heap[c])   /* to create MAX heap array */
19           {                                  // if child is greater than parent swap them
20               temporary = heap[root];      // as structure is of complete binary tree
21               heap[root] = heap[c];     // it took logn steps to reach from root to leaf
22               heap[c] = temporary;
23           }
24           c = root;
25       } while (c != 0);
26   }
27   printf("Heap array : ");
28   for (i = 0; i < array_size; i++)
29       printf("%d\t ", heap[i]);         //printing the heap array
30   for (j = array_size - 1; j >= 0; j--)
31   {
32       temporary = heap[0];
33       heap[0] = heap[j] ;   /* swap max element with rightmost leaf element */
34       heap[j] = temporary;
35       root = 0;
36       do
37       {
38           c = 2 * root + 1;    /* left node of root element */
39           if ((heap[c] < heap[c + 1]) && c < j-1)
40               c++;
41           if (heap[root]<heap[c] && c<j)    /* again rearrange to max heap array */
42           {
43               temporary = heap[root];
44               heap[root] = heap[c];
45               heap[c] = temporary;
46           }
47           root = c;
48       } while (c < j);
49   }
50   printf("\n The sorted array is : ");
51   for (i = 0; i < array_size; i++)
52      printf("\t %d", heap[i]);
53}
Victoria
03 Feb 2020
1Heap Implementation at this link:
2
3https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/Hashing
queries leading to this page
min heap python codeheapsort with heapify downheap sort of max heapheap sorting algorithmminmax heap heapifyhow to use heap in pythonalgorithm for heap sort in calgorithm to sort two min heap sort arraysin place heap sortheap claass pythonthe time to heapify the tree as max heapthere is a binary min heap with array representation a 3d 5b5 2c 22 2c 8 2c 31 2c 42 2c 11 2c 9 2c 38 2c 33 2c 55 2c 49 5d write the array representation of the binary heap after we perform one extract min operation on the heap min heap binary treeheap sort pyhonheap sort in python built in functionswhen was heap sort createdheap sort in c 2b 2bin place heap sotyheap sort in place sorting algorithmheapq min heapjava heapsortheap pythoknheapify a binary treeheapsort arrayheapify heapheap gfgfdoes heap sort work on max heapjava sorting heap sortheap datastructure in pyton iscreate min heap in pythonfind the min of heap using heapify method in data structureheapsort explainedshould i implement heap using binary treeheap space in pythonstack and heap memory in pythonheapq heappop pythonwhen using heap sortheap in pyhtonheapify heapq in pythoncomplexity of heap sort2 constructing the heap for the list 11 2c 51 2c 42 2c 35 2c 23 2c 45 2c 32 a bottom up b top down c array representation and sort it 28write out the steps in both of the two stages 29heap implementation in pythonheap sort dasheap sort in place explainedheap in data structure pythonpython 2 7 heapq max heapheap sort approach but without heap tree or heapifymax heapify tree in data structure codewhy need of heap sortconvert a min heap to a max heapmin heap implementation pythonmin heap tree in data structureheapq heap maxheap max heapdown heap algorithmsort the array 5b10 2c3 2c5 2c1 2c4 2c2 2c7 2c8 5d using heap sorting algorithm show all the steps in figures in design and analysis of algorithmheap sort chow algorithm in max heap heapify works pythonheap sort max heapifyevery heap is a complete binary treeheap sort analysiswhat is heapifymin heap and max heap in python mini heap pythonhow to implement heap for heapsortheap sort using max heap algorithmpython implement heap sortheap python import everythingheap sort code explainedpyramid sortis python heapq max heapheap sort is tecniquehow to heapify min heapheap sort invariantheap sort runtimedifferent methods to sort binary heapheapq python exampleheap sort programizbinary heap sortheap allocation in pythonheap class pythonheapsort data structurehow does heap sort workheap structure using lists pythonmax heap in python3heapify code robert sidheap operations heapq python move downpython heap sortmin heap function in pythonmax heapify algorithmheappfy pythonheap in oythonheap memory24 if you were given the array below 2c and used the procedure we learned in class to build a heap from the array in place 28i e step 1 of heapsort 29 2c what would the resulting heapified array look like 3fwrite an algorithm to explain the concept of heap sortheapq python source codeis binary tree heapheap sort explanationheap using binary treeheap sort algorithm in javaheap sort with binaryhow to create a heap using pythonsort struct using heapsortdo python use heap or stackheap sort with out heapifyheapify algorithm pythonpython heapq max heapheapq pythnheap inbuilt in pythonis heapify and heap sort sameimplementation of heap sortheap 27s algorithmheap sort approach but without heap treeheapq min heap pythonheap ascending sort using min heapmin heap vs binary heapheap sort algorithm javawhy use heap sortheap sort from do wayheapsort time complexityrecursive heapsortheap sort sorted arraywrite the heap sort algorithm python min heapmin heap using heapqmin heap max heapheapify heapqmin heap and max heap as stackdefine heap write down the properties of max heap and min heapmax heap to min heapmin heap tree exampleusing a binary tree to implement a heap and heap sortheap sort program in cheapsort max heapheap sort using max heap pythonheapify in cheap sorheap in python heap sort complexitymax heapify codeheap heapifypython heapify with returnpython heapify defaultpython heapppopheap sort complexityheap in python 2python buildin function for heappython heapifyexplain heapsortpython min heap clasheap meaningwhat are heaps 3f min heap and max heaphow to heap sort pythondiffrence between heap and heap sortcreate a heap with the follwoing array solved exampleswhat is the datastructure for heap sortheapsort java solutionheap pop min pythonheap data structure heapifypython implement min heaphepa sort pythonheap memory in pythonbuild heap and heap sortstl for heap in pythonmin heap and max heap topstack and heap in pythonheap sort codeheap sort treeheap sort using arraywrite a c program to construct a min heap with n elements 2c then display the elements in the sorted order heap sort min heap step bt stephow to do a min heap in pythonheapq heapify python 3construct a heap of size 8 from the following filefirst step of heap sort is to remove the largest element from the heapheap python3heap sort the first step of heap sort is to 3apython heapify functionheap sort function pythonwhy heap sortheap sortingheap sort clrsconvert min heap to max heapmax heap min heap data structureheap datastructure in pythonheapsort in strings arrayheap sort is implementation ofsort an array using heapsort heapsort python code mediumpython how to turn heapimplement heap in pythonpython heapsbuild heap and heapifypython heapq heapsizeheapsort is used to sort a list of 8 integers in an array and few heapify method has been used after that the array is now as follows 3a16 14 15 10 12 27 28heap sort 3fheapify python codeheap sort c 2b 2bpytho heapheapsort using min heap in cexplain heap sort with its algorithmquestion regarding heapifyhow to make a heap in pythonarray sort using max heapheapsort using min heappython heap plain codeheap sort iterativeheapq pythonmin binary heap dijkstra 27s algorithm pseudocodeheapsort python codemin heap in pythonheapq python 3 nlargestheapy binary treeheap sort array pythonheap and heap sortheap sort in pythonmin heap is a complete binary treepython heap implementationimplementation of heap in pythonheap sort example and analysispytohn heapheap sort algorithm in python full detailsheap sort dry run exampleheap sort with priority algorithm c 2b 2b implementationheapify cheap sort code in c for min heapheap sort using min heapbuild heap pythonimplementation of heap sort inbuilt python heap libraryheapsort with min heap javaheap sort inplaceheapify algorithm javaheap in python using listimplement min heap in python syntaxheappushpop in pythonheap sort pythonheapq heapifywhat is heap sortheapq in python 3heap sort use array build heapheap sort javaheap sort code in c for max heapheapify 28 29 pythonheapsort algorithm python geeks for geekscreate min heap pythonheap vs binary heapdifference heapsort and max heapifypython3 heapperform heap sort given data 12 2c 34 2c 3 2c 4 2c 8 2c 1 2c 2 2c 9 2c 11 2c 20 2c 7in a min heap 2c which of the following methods is used to maintain the heap order property when building a heap from a list of given numbers 3fbinary search tree vs heapmax heap and min heap in pythonheap order in max heapheapq in pythondoes heap sort work on non omcplete treeheap sort cpppython heap algorithmheapifymax heap and min heap in data structureheap construction in pythonmin heap implementation using pythonheapify 28heap 29heap map pythonsort heap vs make heapheap vs heapify approachheapsort with min heapheap data structure python implementationsort the set of numbers 22 2c 15 2c 88 2c 46 2c 35 2c 44 in increasing order with heap sort using a different array to store the heap tree 29 heap sortheapq 5bpythomnpython heap codeheaps in pythonheapq 5bythonheap data structure in pythonheap sort using heapifyheap sort itsclass app heap 3a pythonheap sort exampleshow heap sort worksimple heap with pythonwhat are heaps 3f min heap and max heap 3falgorithm of heap sortheap sort using min heapifymin heap sort examplemin heap sortheapify min heapheap extract pythonhow to use heapify pythonheap sort vs quick sortheapsort is used to sort a list of 8 integers in an array and few heapify method has been used after that the array is now as follows 3a 16 14 15 10 12 27 28 how many heapify operations has been performed on root of heap 3fbinary heap orderingjasva heap algorithmbasic heap implementation in pythonheap pythonheap sort algorithm explainedpython min heap stringheapq module uses min heap or max heap 3fmin heap treeheapsort pythonhow to implement heap in pythonwrite a program to heapsort algorithm to sort an integer array in ascendingheapsort max heap pytjonheap sort binary treedoes python put objects in the heapdoes heapify convert it into max heap or min heapheapq python how to implementwhen is heap sort usedheap sort stepsheap1 pythonis heap sort the same as min heapheap sort with min heapusing heap sort which data structure we can builtwhy the heapify algorithm is the most efficient way to create a binary heapascending order heap sort is done by using min heapheap sort in depthheap and stack memory pythonpython heapq min heap pop pushbuild a heap sortimplement heap sorthow does heap sort c 2b 2b sort data heapsort algorithm in pythonwhen we use heap sortbinary heap maxsize of heap in pythonheapify heapqmax heap in python heapqheap import class pythonpython memory heapedheappush pythonheapsort programwhat is the time complexity of heap sort 3fheapify operationheap sort tree exampleheapq heapifyheapify heap max vx heapify heap minheap pop pythondefine 2c discuss 2c explain the heapsort algorithmpython heap with updateheap sort code in python for min heapheap heapify and min maxheapsort algorithm input arraywhy is heap sort in place 3fmin heap to max heapheap stl in pythonmax heap binary treewhat is heapsortconitions for heapify in heapsortheap in python stlheap sorrtheap sort c codepython heapq min heap exampleheapsortheap sort on a object in pythonheap sort for increasing order using max heapin a minimum heap tree 3apython heap sort algorithmhow to perform sorting using heap sortsort heap using heapifymin heap using heapify method in data structuretime complexity of heap sortpython heap sizeheapify in pythonwhen to use heap sortdoes heap sort sort least to greatestheap remove pythonmin heap heapifydoes heap sort sort smallest to largest 3fheap queue pythonminimum heap treeconvert max heap to min heappython heap library max heap heapq pythonhow to make a min heap in pythonheap library in pythondifferences between max heap and min heap used in the heap sort algorithm how heap sort examplemax heap pythonmin heap pythonwhat is heap sorting heap sortheapq module in pythonmax heap and min heaps are binary trees 2aheap sort using techniqueheapify javaheap sort implementation in cbinary min heap treemin heap array implementationheap sort explainedheapy pythonpython is heapq min heap by default construct a binary tree using heapsort algorithm max heappython min heap classsolve heap sort step by stepsorts an input array using heap sort algorithm in javascriptwhat will an empty heap in python will returnpython heapreplaceheap sort max heapmin heap sort algorithm with examplepython how to heap objectpython code for heap using heapifywhat is heaps pythonfirst step of heap sortis heap sort in place 3fheapq python max min heapheap sort minheaps pythonhow to display heap in orderheap sort greedy algorithm complexityheap sort heapify and max heaprecursive heap sortcan you use a min heap for heapsortfor the following sequence 3c16 14 15 10 12 27 28 3e 2c apply the heapify 28max heap or min heap 29 find the total number of heapify procedure at the root python heap data structureimplementation min heap and max heapheap sort complexityis heap sort stab 3beheap sort algorithm tutorialis heapifying an array a heapsortbinary tree max heapheap push inpythonwhat is heap or heap treeheapsort in cheap define pythonheap sort example pyramid sort javabinary tree min heappython max heapifyheapify arrayheap sort is based onheapsort ascending ordersorting using min heapwhat is the state of the following array data when the heap has first been built 3f 33 2c 2 2c 1 2c 8 2c 99 2c 7 2c 5 2c 77is binary tree a max heapheapsort max arraypython heap tutorialc 2b 2b heap sort non recursiveheap using heapifyheap sort practice problems geeksforgeeksheap sort is an implementation ofmin and max heap in pythonheap functions in pythonwhat is a heap in python heap 27s algorithm pythonheapq source code pythonheapify max heap pythonheap inbuild in pythonheap sort using max heapheap sort applicationheap sort based onhaeap sort using mean and max heapheapify process pythonwhat is a max heap in sort heapmin heap and max heap 5dbinary heap heapify upcustom heap pythonwhat is heap space in pythonpython print a heapcases to check heapsortmin heapify algorithmheap sort is implementation isheapify from max to min heapheapq max heapbinary heap sortingheapq sort arraycreate a heap in pythonimplement heapsort algorithm to sort an integer array in an ascending heapsort algorithmdoes heap sort use min heap or max heap min heap pythonheapsort in pythonis heap sort goodheap reviewsfor the implementation of heap sort discussed in class 2c the worst case complexity for sorting an array of size n isopci c3 b3n c3 banica heap in python3an array of elements can be sorted using the heap sort algorithm the first step of the heap sort algorithm is to convert the array into a maximum heap if the following array is to be sorted 3aheap in python 3minimum heap in pythoncreate heap using heapify jsheap sort min heapheap algorithmheap sort algorithmpython implementation of a min heappython heapimplement heap using binary treehow to implement a heap in pythonwhat is heapify in heap sortwhat is heap memory in pythonheap sort without heapifyheap sort python codeheap sort in dsmin heap is a binary treepython program for heap sortcreate a js program that sorts an input array using heap sort algorithm max heap python heapqheap data structure pythonheapifi codeheap and stack memory in pythonwhat is heapify in pythonusing builtin heap in pythonpython heapsort explainedheappop python codebinary search tree heapheap programminheap sort heapify meaningmin heap and max heap pythonhow to heap a max heap in pythonheap sort in max heap min of heap using heapify method in data structureheapsort algorithm examplehow algorithm in max heap heapify worksmax heao algorithmpython heap how it worksheap data structure heapify pythonsimple python heapin place heap sort pythonheapq example pythoncreate heap in pythonhow to initialize a heap in pythonsort array using min heapheap sort out place sorting algorithmheap ordered binary treecomplexity of an sorting an array using the heapheapsort algorithm to sort an integer array in ascendingheap sort code in cpython heapqexample of a max heap binary treehow can we write the heap in pythonhow to heapify a binary treeheap sort to sort a list pythonmin heapafter performing heapsort will the array also be called as heapheapsort on min heapis heap binary treeheap sort time complexityheap sort algorhtimheapify python docsmax heap in pythonheap sort implementationheap sort dryrunheap property in pythonheap sort 12 2c 34 2c 3 2c 4 2c 8 2c 1 2c 2 2c 9 2c 11 2c 20 2c 7on which algorithm is heap sort based on 3fheapq python 3heap sort and heap structureheap sort in place geeks for geeksheapsort max heap binary treepython max heap heaplifyheap module in pythonin a min heap 2c which of the following methods is used to maintain the heap order property when building a heap from a list of given numbersheapsort complexitypython heapq create max heappython heappushheap module pythonmaking a heap in python2 6 3 heap heap sort heapifyanalysis of heap sortheapsort array pythonhow to heap sortcreating heap pythonsort using heap sortheap sotheap size pythonheapify array to heapheapify down pythonheapify python linearheap sort in cmax heapify algorithmprint heap pythonheap binary treeheapify algorithmheapq import pythonpython heapq min heapthe heap sort method has a worst case complexity function that is inheap sort in data structuresort heapheap mechanism in pythonheap in python without heapqheapsort c 2b 2bheapq max min heapgenerate heap in pythonheapify and build heapmax heap sort algorithmcode for heapifypythong heap data structureheap sorting max heap outputbinary max heap tree 3fhow does heap sort work 3f implement heap sort for the given array heap queuepython inbuilt heapis binary tree heap solutionmax heap using heapq in pythonheap sort in placeheap sort pseudocodeheap sort in javabinary heap implementationderivation of running times for heapsortpython max heap sortminimum binary heappseudo code for min heap sorthow to use heapq pythonwhat is a heap pythoncreate max heap for following sequence 3c12 2c 18 2c 15 2c 20 2c 7 2c 9 2c 2 2c 6 2c 5 2c 3 3e then perform heap sort for the following sequence show all the steps of insertion 2c deletion and sorting 2c and analyse the running time complexity for heap sort is heap python built in pythonpython in built heapify functionimplement heap pythondoes heapsort require max heapmax heap chow to create max heap and min heap using heapqwhat binary tree uses heaphow to implement min heap in pythonbuild min heap sort arraymin heap and max heapmax heap binary tree exampleheap python librarypython heapify examplesort a heap in pythonfar right node in min heapwap to sort integers in an ascending order using heap sort min heaps and max heapsheappush with function in pythonbinary heapheapsort in c 2b 2bheap sort algorithm in pythonheap max pythonmin heap algorithmexplain heap sort on the array max heapifyhow to use min heap in pythonmax heap and min heapheap bottom uppython heap on stringsheap and binary heapin heap sort 2c we first build a heap 2c then we do following operations till the heap size becomes 1 in place heap sort using a max heaphow much time heap sort will take to sort n 2f10 elements heapq function in pythonsorting min heap complexitytypical running time of a heap sort algorithmheap sort factsheapify function in pythonclement heap sortheapify max heappython heap stackheapify pythonpython heapq to sortmin heap insert algorithmdoes python 2 have heapmax heapify tree codein a minimum heap treeheap sorting on max heaphow to sort a heapbinary heap minifyheap sort complexicyheap methodsheapsort javaheapsort 22python 22heap sort code c 2b 2bheappush and heappop pythonheapq max heap pythonthe first step of heap sort is todefault heap type in pythonsorting a binary heapheappush in pythonheap sortheapq python max heappython heap sort functionpython heap memoryheap sotshow to create a heap in pythonheap sort binary min heappython build heapmax heap treemax heap heapifyheap is complete binary treepython heap and stack memoryheap sort complexity analysisaccess heap in pythonprogramiz heap sortpython heapq heapify on valueheap isort complexityheap property of a binary treeheapsort python