-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_sort.py
More file actions
152 lines (120 loc) · 3.8 KB
/
heap_sort.py
File metadata and controls
152 lines (120 loc) · 3.8 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import sorting_array
import sys
class style():
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
UNDERLINE = '\033[4m'
RESET = '\033[0m'
# process implementaion
# To heapify subtree rooted at index i.
# n is size of heap
def heapify(arr, N, i):
largest = i # Initialize largest as root
l = 2 * i + 1 # left = 2*i + 1
r = 2 * i + 2 # right = 2*i + 2
# See if left child of root exists and is
# greater than root
if l < N and arr[largest] < arr[l]:
largest = l
# See if right child of root exists and is
# greater than root
if r < N and arr[largest] < arr[r]:
largest = r
# Change root, if needed
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i] # swap
# Heapify the root.
heapify(arr, N, largest)
# The main function to sort an array of given size
def heapSort(arr):
N = len(arr)
# Build a maxheap.
for i in range(N // 2 - 1, -1, -1):
heapify(arr, N, i)
# One by one extract elements
for i in range(N - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # swap
heapify(arr, i, 0)
# selection sorting
def heap_sorting():
print(style.CYAN + "Create your array first.")
try:
heap_array = []
array_size = eval(input("Enter the size of your array: "))
for i in range(array_size):
array_element = eval(input("Enter the " + str(i) + " element of your array: "))
heap_array.append(array_element)
print("Your unsorted array is " + str(heap_array) + " .")
# Traverse through all array elements
heapSort(heap_array)
print("your Sorted array is: ")
for i in range(len(heap_array)):
print("%d" % heap_array[i], end=" , ")
back_select = input(style.RED + "For review press 0 on any other key to close: ")
if back_select == '0':
heap_sorting()
else:
heap_sort()
except:
print("invalid input")
heap_sorting()
# code review
def heap_sort_code():
print(style.WHITE + """
def heapify(arr, N, i):
largest = i # Initialize largest as root
l = 2 * i + 1 # left = 2*i + 1
r = 2 * i + 2 # right = 2*i + 2
# See if left child of root exists and is
# greater than root
if l < N and arr[largest] < arr[l]:
largest = l
# See if right child of root exists and is
# greater than root
if r < N and arr[largest] < arr[r]:
largest = r
# Change root, if needed
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i] # swap
# Heapify the root.
heapify(arr, N, largest)
# The main function to sort an array of given size
def heapSort(arr):
N = len(arr)
# Build a maxheap.
for i in range(N // 2 - 1, -1, -1):
heapify(arr, N, i)
# One by one extract elements
for i in range(N - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # swap
heapify(arr, i, 0)
""")
back_selection = input(style.RED + "Enter any key to exit: ")
if back_selection == "0":
heap_sort()
else:
heap_sort()
def heap_sort():
print(style.CYAN + """
[+] Select an option:
1: code review
2: implementaion
3: back
""")
selection = input("Enter youre selection: ")
if selection == '1':
heap_sort_code()
elif selection == '2':
heap_sorting()
elif selection == '3':
sorting_array.array_sorting()
else:
print(style.RED + "[+] Invalid input try again")
heap_sort()