-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
216 lines (158 loc) · 4.32 KB
/
main.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import random
import time
class Sort:
arr = list()
tm = None
def __init__(self, cnt=None, min=None, max=None):
if cnt == None:
return None
for i in range(cnt):
n = random.randrange(min, max+1)
self.arr.append(n)
def __iter__(self):
return iter(self.arr)
def time_out(self):
return self.tm
def rem(self):
self.arr.clear()
def InsertionSort(self):
self.tm = 0
data = list(self.arr)
start = time.time()
for i in range(len(data)):
ind = i - 1
ob = data[i]
while ind >= 0 and data[ind] > ob:
data[ind + 1] = data[ind]
ind -= 1
data[ind + 1] = ob
self.tm = time.time()-start
return data
def CocktailSort(self):
self.tm = 0
data = list(self.arr)
start = time.time()
left = 0
right = len(data) - 1
while left < right:
temp = right + 1
for i in range(right, left, -1):
if data[i-1] > data[i]:
temp = i
data[i], data[i - 1] = data[i - 1], data[i]
left = temp
temp = left - 1
for i in range(left, right):
if data[i] > data[i + 1]:
temp = i
data[i], data[i + 1] = data[i + 1], data[i]
right = temp
self.tm = time.time() - start
return data
def heap (self, arr, n, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n and arr[i] < arr[left]:
largest = left
if right < n and arr[largest] < arr[right]:
largest = right
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
self.heap(arr, n, largest)
def HeapSort(self):
self.tm = 0
data = list(self.arr)
start = time.time()
for i in range(len(data), -1, -1):
self.heap(data, len(data), i)
for i in range(len(data)-1, 0, -1):
data[i], data[0] = data[0], data[i]
self.heap(data, i, 0)
self.tm = time.time() - start
return data
def supporting(self, arr, value):
cntArr = [0] * 10
for i in range(len(arr)):
place = (arr[i] // value) % 10
cntArr[place] += 1
for i in range(1, len(cntArr)):
cntArr[i] += cntArr[i-1]
outArr = [0] * len(arr)
i = len(arr) - 1
while i >= 0:
current = arr[i]
place = (arr[i] // value) % 10
cntArr[place] -= 1
newPos = cntArr[place]
outArr[newPos] = current
i -= 1
return outArr
def RadixSort(self):
self.tm = 0
data = list(self.arr)
start = time.time()
maxValue = max(data)
number_digit = 0
while int(maxValue) > 0:
maxValue /= 10.0
number_digit += 1
digit = 1
outData = data
for i in range (number_digit):
outData = self.supporting(outData, digit)
digit *= 10
self.tm = time.time() - start
return outData
def save(arr):
filename = "test"
f = open(filename + ".txt", "a", encoding='utf-8')
for i in range(len(arr)):
f.write(str(arr[i]))
f.write(" ")
f.write("\n")
f.close()
def choise(ind):
if ind == 1:
global bull
bull.rem()
print("введите количество значений:")
num = int(input())
print("начало диапазона:")
start = int(input())
print("конец диапазона:")
end = int(input())
bull = Sort(num, start, end)
elif ind == 2:
print(' '.join(map(str, bull)), "\n")
elif ind == 3:
arr0 = list(bull.InsertSort())
print(bull.time_out(), " sec \n")
elif ind == 4:
arr0 = list(bull.CocktailSort())
print(bull.time_out(), " sec \n")
elif ind == 5:
arr0 = list(bull.HeapSort())
print(bull.time_out(), " sec \n")
elif ind == 6:
arr0 = list(bull.RadixSort())
print(bull.time_out(), " sec \n")
if ind != 2 and ind != 1:
save(arr0)
def menu():
ind = -1
while True:
print("1. Input")
print("2. Output")
print("3. InsertSort")
print("4. CocktailSort")
print("5. HeapSort")
print("6. RadixSort")
print("0. exit")
ind = int(input())
if ind == 0:
break
print("")
choise(ind)
bull = Sort()
menu()