Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

[ADD] heap sort with pure function #326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Python/heap_sort_pure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Heap Sort


def heapify(arr, n, i):
largest = i
left = 2 * i + 1 # left = 2*i + 1
right = 2 * i + 2 # right = 2*i + 2

# See if left child of root exists and is
# greater than root
if left < n and arr[i] < arr[left]:
largest = left

# See if right child of root exists and is
# greater than root
if right < n and arr[largest] < arr[right]:
largest = right

# Change root, if needed
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)


def heap_sort(input_list):
size = len(input_list)
result_array = input_list[:]

# Build a max heap.
for i in range(size, -1, -1):
heapify(result_array, size, i)

# One by one extract elements
for i in range(size-1, 0, -1):
result_array[i], result_array[0] = result_array[0], result_array[i]
heapify(result_array, i, 0)

return result_array


if __name__ == '__main__':
sample = [12, 11, 13, 5, 6, 7]

print("Sorted array:")
print(sample)

print("Sorted array:")
print(heap_sort(sample))