mirror of
https://github.com/caperren/school_archives.git
synced 2025-11-09 21:51:15 +00:00
Added missing classes from final year at OSU
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
Ensure data.txt is in the same directory, then run:
|
||||
|
||||
python3 stoogesort.py
|
||||
|
||||
For stoogeTime, simply run:
|
||||
|
||||
python3 stoogeTime.py
|
||||
@@ -0,0 +1,41 @@
|
||||
from random import randint
|
||||
from time import time
|
||||
|
||||
|
||||
MIN_INT = 0
|
||||
MAX_INT = 10000
|
||||
|
||||
|
||||
def stooge_sort(data, start_index=0, end_index=None):
|
||||
if end_index is None:
|
||||
end_index = len(data) - 1
|
||||
|
||||
data_length = end_index - start_index
|
||||
|
||||
if data[start_index] > data[end_index]:
|
||||
data[start_index], data[end_index] = data[end_index], data[start_index]
|
||||
|
||||
if data_length > 1:
|
||||
split_point = (end_index - start_index + 1) // 3
|
||||
|
||||
stooge_sort(data, 0, end_index - split_point)
|
||||
stooge_sort(data, start_index + split_point, end_index)
|
||||
stooge_sort(data, 0, end_index - split_point)
|
||||
|
||||
return data
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
start = 4
|
||||
step = 1
|
||||
n_values = [start + i * step for i in range(10)]
|
||||
n_generated_list = [[randint(MIN_INT, MAX_INT) for _ in range(n_value)] for n_value in n_values]
|
||||
|
||||
print("n_value, time")
|
||||
|
||||
for current_list in n_generated_list:
|
||||
current_length = len(current_list)
|
||||
|
||||
start_time = time()
|
||||
stooge_sort(current_list)
|
||||
print("{}, {}".format(current_length, time() - start_time))
|
||||
@@ -0,0 +1,48 @@
|
||||
INPUT_FILENAME = "data.txt"
|
||||
OUTPUT_FILENAME = "stooge.out"
|
||||
|
||||
|
||||
def stooge_sort(data, start_index=0, end_index=None):
|
||||
if end_index is None:
|
||||
end_index = len(data) - 1
|
||||
|
||||
data_length = end_index - start_index
|
||||
|
||||
if data[start_index] > data[end_index]:
|
||||
data[start_index], data[end_index] = data[end_index], data[start_index]
|
||||
|
||||
if data_length > 1:
|
||||
split_point = (end_index - start_index + 1) // 3
|
||||
|
||||
stooge_sort(data, 0, end_index - split_point)
|
||||
stooge_sort(data, start_index + split_point, end_index)
|
||||
stooge_sort(data, 0, end_index - split_point)
|
||||
|
||||
return data
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
output = []
|
||||
|
||||
with open(INPUT_FILENAME, "r") as input_file:
|
||||
file_lines = input_file.readlines()
|
||||
|
||||
for line in file_lines:
|
||||
line = line.strip("\n\r")
|
||||
line_items = line.split(" ")
|
||||
|
||||
item_count = int(line_items[0])
|
||||
data_values = [int(item) for item in line_items[1:]]
|
||||
|
||||
output.append(stooge_sort(data_values))
|
||||
|
||||
with open(OUTPUT_FILENAME, "w") as output_file:
|
||||
output_last_index = len(output) - 1
|
||||
|
||||
for index, line in enumerate(output):
|
||||
line_string = " ".join([str(value) for value in line])
|
||||
|
||||
output_file.write(line_string)
|
||||
|
||||
if index != output_last_index:
|
||||
output_file.write("\n")
|
||||
Reference in New Issue
Block a user