-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinHeapSort.py
More file actions
55 lines (45 loc) · 1.71 KB
/
MinHeapSort.py
File metadata and controls
55 lines (45 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# **********************************************************************************************************************
# NAME: Timothy P. McCrary
# CLASS: CS 2302
# LAB 5 OPTION A
# INSTRUCTOR: Diego Aguirre
# TA: Manoj Pravaka Saha
# DATE: 11/25/2018
# PURPOSE: To understand and use a min heap.
# **********************************************************************************************************************
# Based off Zybook implementation.
def percolate_down(item_index, min_heap, list_size):
child_index = 2 * item_index + 1
value = min_heap[item_index]
while child_index < list_size:
# Find the max among the item and all the item's children
max_value = value
max_index = -1
i = 0
while i < 2 and i + child_index < list_size:
if min_heap[i + child_index] > max_value:
max_value = min_heap[i + child_index]
max_index = i + child_index
i = i + 1
if max_value == value:
return
# Swap heap_list[node_index] and heap_list[max_index]
temp = min_heap[item_index]
min_heap[item_index] = min_heap[max_index]
min_heap[max_index] = temp
item_index = max_index
child_index = 2 * item_index + 1
# Based off Zybook implementation.
def heap_sort(min_heap):
i = len(min_heap) // 2 - 1
while i >= 0:
percolate_down(i, min_heap, len(min_heap))
i = i - 1
i = len(min_heap) - 1
while i > 0:
# Swap min_heap[0] and min_heap[i]
temp = min_heap[0]
min_heap[0] = min_heap[i]
min_heap[i] = temp
percolate_down(0, min_heap, i)
i = i - 1