-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemorySort.py
More file actions
346 lines (238 loc) · 8.32 KB
/
MemorySort.py
File metadata and controls
346 lines (238 loc) · 8.32 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
Purpose:
This file contains memorySort(), which can sort a list with a time complexity of O(n + k).
n = amount of elements in list
k = distance between the smallest integer and highest integer
This file also contains variations of the sorting algorithm so that it can be used with lists containing different object types
Author(s):
Drake T. Setera
Date:
1/21/2025
Version:
5.1.0
"""
def memorySort(lst: list, mode: str = '') -> list:
"""Sorts the inputted list in ascending order utilizing memory to speed up computation time
Args:
lst (list): List to get sorted
mode (str):
: (default) = sorts list using mode of first element in the given list
: 'a' or 'ascii' = list containing strings (sorted based on string ascii value)
: 'c' or 'chr' = list containing characters
: 'f' or 'float' = list containing float
: 'i' or 'int' = list containing integers
: 'n' or 'None' = list containing integers with no repeats
: 's' or 'str' = list containing strings (sorted based on letter order)
Returns:
list: sorted list
"""
if mode == '':
if isinstance(lst, list):
if isinstance(lst[0], int):
return memorySortI(lst)
if isinstance(lst[0], float):
return memorySortF(lst)
if isinstance(lst[0], str):
return memorySortS(lst)
elif isinstance(lst, str):
return memorySortString(lst)
if mode.lower() == 'i' or mode.lower() == 'int' or mode.lower() == '':
return memorySortI(lst)
if mode.lower() == 'f' or mode.lower() == 'float':
return memorySortF(lst)
if mode.lower() == 'n' or mode.lower() == 'none':
return memorySortN(lst)
if mode.lower() == 'c' or mode.lower() == 'chr':
return memorySortC(lst)
if mode.lower() == 's' or mode.lower() == 'str':
return memorySortS(lst)
if mode.lower() == 'a' or mode.lower() == 'ascii':
return memorySortA(lst)
return None
def memorySortI(lst: list) -> list:
"""Sorts the inputted list of integers in ascending order utilizing memory to speed up computation time
Args:
lst (list): List of integers to get sorted
Returns:
list: sorted list of integers
"""
high, low = lst[0], lst[0]
for l in lst:
if l > high:
high: int = l
elif l < low:
low: int = l
ran: int = high - low + 1
amount: list = [0] * ran
for l in lst:
amount[l - low] += 1
output: list = [0] * len(lst)
p: int = 0
for o in range(len(amount)):
for _ in range(amount[o]):
output[p] = o + low
p: int = p + 1
return output
def memorySortN(lst: list) -> list:
"""Sorts the inputted list of integers in ascending order utilizing memory to speed up computation time
Args:
lst (list): List of integers with no repeating elements as well as
all elements between the highest and lowest are all used once
Returns:
list: sorted list of integers
"""
high, low = lst[0], lst[0]
for l in lst:
if l > high:
high: int = l
elif l < low:
low: int = l
ran: int = high - low + 1
output: list = [None] * ran
for l in lst:
output[l - low] = l
return output
def memorySortC(lst: list) -> list:
"""Sorts the inputted list of character elements in ascending order utilizing memory to speed up computation time
Args:
lst (list): List of character elements to get sorted
Returns:
list: sorted list of character elements
"""
high: int = ord(lst[0])
low: int = high
for l in lst:
temp: int = ord(l)
if temp > high:
high: int = temp
elif temp < low:
low: int = temp
ran: int = high - low + 1
amount: list = [0] * ran
for l in lst:
amount[ord(l) - low] += 1
output: list = [''] * len(lst)
p: int = 0
for o in range(len(amount)):
for _ in range(amount[o]):
output[p] = chr(o + low)
p: int = p + 1
return output
def memorySortA(lst: list) -> list:
"""Sorts the inputted list of string elements in ascending order based on ascii value utilizing memory to speed up computation time
Args:
lst (list): List of string elements to get sorted
Returns:
list: sorted list of string elements
"""
high: int = 0
for l in lst[0]:
high = high * 128 + ord(l)
low: int = high
for l in lst:
temp: int = 0
for i in l:
temp = temp * 128 + ord(i)
if temp > high:
high: int = temp
elif temp < low:
low: int = temp
ran: int = high - low + 1
amount: list = [0] * ran
for l in lst:
temp: int = 0
for i in l:
temp = temp * 128 + ord(i)
amount[temp - low] += 1
output: list = [''] * len(lst)
p: int = 0
for o in range(len(amount)):
if amount[o] != 0:
i = o + low
out = ''
while i != 0:
letter = chr(i % 128)
i = i // 128
out = letter + out
for _ in range(amount[o]):
output[p] = out
p: int = p + 1
return output
def memorySortF(lst: list) -> list:
"""Sorts the inputted list of float in ascending order utilizing memory to speed up computation time
Args:
lst (list): List of float to get sorted
Returns:
list: sorted list of float
"""
high, low = int(lst[0]), int(lst[0])
for l in lst:
temp = int(l)
if temp > high:
high: int = temp
elif temp < low:
low: int = temp
ran: int = high - low + 1
amount: list = [0] * ran
for l in lst:
if amount[int(l) - low] == 0:
amount[int(l) - low] = [l]
else:
amount[int(l) - low].append(l)
output: list = [0] * len(lst)
p: int = 0
for am in amount:
if isinstance(am,list):
am.sort()
for a in am:
output[p] = a
p: int = p + 1
return output
def memorySortS(lst: list) -> list:
"""Sorts the inputted list of string elements in ascending order utilizing memory to speed up computation time
Args:
lst (list): List of string elements to get sorted
Returns:
list: sorted list of string elements
"""
if len(lst) == 0:
return []
if len(lst) == 1:
return lst
high: int = -1
low: int = 256
for l in lst:
if len(l) > 0:
temp: int = ord(l[0])
if temp > high:
high: int = temp
if temp < low:
low: int = temp
if (low == high) or (low == 256 and high == -1):
return lst
ran: int = high - low + 1
amount: list = [0] * ran
for l in lst:
if amount[ord(l[0]) - low] == 0:
amount[ord(l[0]) - low] = [l]
else:
amount[ord(l[0]) - low].append(l)
for am in range(len(amount)):
if isinstance(amount[am], list):
for a in range(len(amount[am])):
amount[am][a] = amount[am][a][1:]
amount[am] = memorySortS(amount[am])
output: list = [''] * len(lst)
p: int = 0
for o in range(len(amount)):
if isinstance(amount[o], list):
for a in range(len(amount[o])):
output[p] = chr(o + low) + amount[o][a]
p: int = p + 1
return output
def memorySortString(string: str) -> str:
temp = memorySortC(string)
output = ''
for t in temp:
output = output + t
return output