Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
 def test_array_of_one(self):
     self.assertListEqual([5], heapify([5]))
Exemplo n.º 5
0
 def test_empty_array(self):
     self.assertListEqual([], heapify([]))
Exemplo n.º 6
0
 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]))