Skip to content

Commit c0d278c

Browse files
authored
Add files via upload
1 parent 3fe3e15 commit c0d278c

1 file changed

Lines changed: 113 additions & 5 deletions

File tree

MemorySort.py

Lines changed: 113 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,60 @@
1-
def memorySort(lst: list) -> list:
2-
"""Sorts the inputted list in ascending order utilizing memory to speed up runtime
1+
2+
"""
3+
Purpose:
4+
This file contains memorySort(), which can sort a list with a time complexity of O(n + m).
5+
n = amount of elements in list
6+
m = distance between the smallest integer and highest integer
7+
8+
This file also contains variations of the sorting algorithm so that it can be used with different lists
9+
10+
11+
Author(s):
12+
Drake T. Setera
13+
14+
Date:
15+
11/25/2024
16+
17+
Version:
18+
3.0.0
19+
"""
20+
21+
22+
23+
24+
def memorySort(lst: list, mode: str = 'i') -> list:
25+
"""Sorts the inputted list in ascending order utilizing memory to speed up computation time
326
427
Args:
5-
lst (list): A list containing integers
28+
lst (list): List to get sorted
29+
mode (str):
30+
'i' = (default) list containing integers
31+
'n' = list containing integers with no repeats
32+
's' = list containing string elements
633
734
Returns:
8-
list: A sorted list of integers
35+
list: sorted list
936
"""
37+
38+
if mode.lower() == 'i':
39+
return memorySortI(lst)
40+
if mode.lower() == 'n':
41+
return memorySortN(lst)
42+
if mode.lower() == 's':
43+
return memorySortS(lst)
44+
return None
45+
46+
47+
48+
def memorySortI(lst: list) -> list:
49+
"""Sorts the inputted list of integers in ascending order utilizing memory to speed up computation time
50+
51+
Args:
52+
lst (list): List of integers to get sorted
53+
54+
Returns:
55+
list: sorted list of integers
56+
"""
57+
1058
high, low = lst[0], lst[0]
1159
for l in lst:
1260
if l > high:
@@ -20,10 +68,70 @@ def memorySort(lst: list) -> list:
2068
amount[l - low] += 1
2169

2270
output: list = [0] * len(lst)
23-
p = 0
71+
p: int = 0
2472
for o in range(len(amount)):
2573
for _ in range(amount[o]):
2674
output[p] = o + low
2775
p: int = p + 1
2876

2977
return output
78+
79+
80+
81+
def memorySortN(lst: list) -> list:
82+
"""Sorts the inputted list of integers in ascending order utilizing memory to speed up computation time
83+
84+
Args:
85+
lst (list): List of integers with no repeating elements as well as
86+
all elements between the highest and lowest are all used once
87+
88+
Returns:
89+
list: sorted list of integers
90+
"""
91+
92+
high, low = lst[0], lst[0]
93+
for l in lst:
94+
if l > high:
95+
high = l
96+
elif l < low:
97+
low = l
98+
ran = high - low + 1
99+
output = [None] * ran
100+
101+
for l in lst:
102+
output[l - low] = l
103+
return output
104+
105+
106+
107+
def memorySortS(lst: list) -> list:
108+
"""Sorts the inputted list of string elements in ascending order utilizing memory to speed up computation time
109+
110+
Args:
111+
lst (list): List of string elements to get sorted
112+
113+
Returns:
114+
list: sorted list of string elements
115+
"""
116+
117+
high, low = ord(lst[0]), ord(lst[0])
118+
for l in lst:
119+
temp: int = ord(l)
120+
if temp > high:
121+
high: int = temp
122+
elif temp < low:
123+
low: int = temp
124+
ran: int = high - low + 1
125+
amount: list = [0] * ran
126+
127+
for l in lst:
128+
amount[ord(l) - low] += 1
129+
130+
output: list = [0] * len(lst)
131+
p: int = 0
132+
for o in range(len(amount)):
133+
for _ in range(amount[o]):
134+
output[p] = chr(o + low)
135+
p: int = p + 1
136+
137+
return output

0 commit comments

Comments
 (0)