def heap_sort(heap): build_max_heap(heap) heap_size = len(heap) - 1 for i in range(heap_size, 1, -1): heap[1], heap[i] = heap[i], heap[1] heap_size -= 1 max_heapify(heap, heap_size, 1)
def delete_item(self,k): for i in self.list: if i==k: index=self.list.index(i) break self.list[len(self.list)-1],self.list[index]=self.list[index],self.list[len(self.list)-1] self.list.pop() build_max_heap(self.list)
def delete_item(self, k): for i in self.list: if i == k: index = self.list.index(i) break self.list[len(self.list) - 1], self.list[index] = self.list[index], self.list[ len(self.list) - 1] self.list.pop() build_max_heap(self.list)
def heap_sort(array): array = build_max_heap(array) sorted_array = [] # Loop till array has 1 element for i in range(len(array) - 1, 0, -1): array = swap(i, 0, array) maxnum = array[len(array) - 1] array.pop(len(array) - 1) sorted_array.insert(0, maxnum) max_heapify(array, 0) # Insert last element of heap sorted_array.insert(0, array[0]) return sorted_array
def insert(self,x): self.list.append(x) build_max_heap(self.list)
def change_key(self,i,key): if key<self.list[i]: print "no insertion" else : self.list[i]=key build_max_heap(self.list)
def extract_max(self): self.list[len(self.list)-1],self.list[0]=self.list[0],self.list[len(self.list)-1] print self.list.pop() build_max_heap(self.list)
def insert(self, x): self.list.append(x) build_max_heap(self.list)
def change_key(self, i, key): if key < self.list[i]: print "no insertion" else: self.list[i] = key build_max_heap(self.list)
def extract_max(self): self.list[len(self.list) - 1], self.list[0] = self.list[0], self.list[len(self.list) - 1] print self.list.pop() build_max_heap(self.list)