Objective: Create a heap sort that returns a unsorted list to sorted from greatest to least.
Does this implementation look correct? Do you have any tips for optimizing this?
'''HeapSort. Best: n log(n) Average: n log(n) Worst: n log(n) '''
#Calls heapify and returns sorted list from greatest to least.
def heapSort(list):
result = []
while list:
result.append(heapify(list))
return result
def heapify(struct):
for x in range(len(struct),0,-1):
#Base Case: Returns last integer when sequence reaches below 2
if len(struct) < 2:
return struct.pop(0)
#Compares Tree Root Node vs Children.
if struct[0] < struct[1]:
temp = struct[0]
struct[0] = struct[1]
struct[1] = temp
else:
#Parent: x/2. Child: x-1. Compares child nodes vs parent nodes in Tree.
if struct[x-1] > struct[x/2]:
temp = struct[x-1]
struct[x-1] = struct[x/2]
struct[x/2] = temp
return struct.pop(0)