Skip to content
Snippets Groups Projects
Commit 1caa1a69 authored by nilsl99's avatar nilsl99
Browse files

Implemented HeapSort

parent fa27a910
Branches
No related tags found
No related merge requests found
......@@ -13,7 +13,42 @@ def HeapSort(array):
sorted_array: a sorted copy of the array
'''
return
p = lambda i: (i-1)//2
l = lambda i: 2*i + 1
r = lambda i: 2*i + 2
sorted_array = list(array)
# Erzeuge einen max-Heap
for i in range(1, len(sorted_array)):
j = i
while sorted_array[j] > sorted_array[p(j)] and j != 0:
sorted_array[j], sorted_array[p(j)] = sorted_array[p(j)], sorted_array[j]
j = p(j)
# Entferne max aus dem max-Heap
for i in range(len(sorted_array)-1, 1, -1):
sorted_array[0], sorted_array[i] = sorted_array[i], sorted_array[0]
j = 0
while True:
if l(j) >= i or r(j) >= i:
break
if r(j) >= i and sorted_array[l(j)] > sorted_array[j]:
sorted_array[j], sorted_array[l(j)] = sorted_array[l(j)], sorted_array[j]
j = l(j)
if sorted_array[j] >= sorted_array[l(j)] and sorted_array[j] >= sorted_array[r(j)]:
break
if sorted_array[l(j)] > sorted_array[r(j)]:
sorted_array[j], sorted_array[l(j)] = sorted_array[l(j)], sorted_array[j]
j = l(j)
else:
sorted_array[j], sorted_array[r(j)] = sorted_array[r(j)], sorted_array[j]
j = r(j)
return sorted_array
def InsertionSort(array):
'''Sort the array by inserting each element into an already sorted part of the array.
......@@ -40,7 +75,7 @@ def InsertionSort(array):
return sorted_array
def CountingSort(array):
'''Sort the array by counting the occurrence of each element in the array.
'''Sort the array by counting the ocjence of each element in the array.
Parameters
----------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment