def heapSort(a): x=[] for i in range(len(a)/2,-1,-1): hpfy.heapify(a,i) final_arr.append(a[0]) a[0],a[len(a)-1]=a[len(a)-1],a[0] a.pop() while(len(a)!=0): for i in range(len(a)/2,-1,-1): hpfy.heapify(a,i) final_arr.append(a[0]) a[0],a[len(a)-1]=a[len(a)-1],a[0] a.pop()
def heapSort(a): x = [] for i in range(len(a) / 2, -1, -1): hpfy.heapify(a, i) final_arr.append(a[0]) a[0], a[len(a) - 1] = a[len(a) - 1], a[0] a.pop() while (len(a) != 0): for i in range(len(a) / 2, -1, -1): hpfy.heapify(a, i) final_arr.append(a[0]) a[0], a[len(a) - 1] = a[len(a) - 1], a[0] a.pop()
from heapify import heapify # homemade module origArr = [5, 3, 17, 10, 84, 19, 6, 22, 9] heapify(origArr) print("heap:",origArr) sorted_array = [] while len(origArr) > 0: if len(origArr) == 1: sorted_array.append(origArr[0]) break else: sorted_array.append(origArr.pop(0)) origArr.insert(0, origArr.pop(-1)) heapify(origArr) print(origArr) print("done", sorted_array)
def test_array_of_one(self): self.assertListEqual([5], heapify([5]))
def test_empty_array(self): self.assertListEqual([], heapify([]))
def test_example1(self): self.assertListEqual([84, 22, 19, 10, 3, 17, 6, 5, 9], heapify([5, 3, 17, 10, 84, 19, 6, 22, 9]))