-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_5.py
More file actions
54 lines (51 loc) · 1.55 KB
/
lab_5.py
File metadata and controls
54 lines (51 loc) · 1.55 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
#Govinda KC
#cs 2302 lab 5A
# Prof: Diego Aguirre
# TA: Manoj Sah
# last modified 11/27/2018
# importing neccessary libraries
import Heap
import random
# This method sorts by the properties of min heap using the relation of parent and child
def heap_sort(heap_list):
hip = Heap.Heap()
i = len(heap_list)//2 -1
while i >= 0:
hip.move_down(i, heap_list,len(heap_list))
i = i-1
i = len(heap_list) -1
while i > 0:
temp = heap_list[0]
heap_list[0] = heap_list[i]
heap_list[i] = temp
hip.move_down(0, heap_list, i)
i = i-1
# this try and except method is applicable to avoid the code-run obstruction.
try:
def read_file():
heap_list =[]
# open the file and reads
_file = open('test_file.txt', 'r+')
# reads the file splitted by comma
line = _file.read().split(',')
for num in line:
# This below appends the num to the list
heap_list.append(num)
heap_list = list(map(int, heap_list))
return heap_list
except:
print('file not foud, check the file name')
def main():
# This is for hard coded values
print('Testing the hard-coded values:')
hard_list = [7, 43, 35, 23, 75]
print('\nbefore sorting:', hard_list)
heap_sort(hard_list)
print('after sorting:', hard_list)
# This is for txt file after reading it.
print('\nTesting by using the text file')
read_list = read_file()
print('\nThe list before heap sort:', read_list)
heap_sort(read_list)
print('The list after heap sort', read_list)
main()