Created
December 12, 2023 11:02
-
-
Save Jan200101/bb2e856962d391b9890a1fa30c8822b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
min_val = 0 | |
max_val = 100 | |
max_list = list(range(min_val, max_val, 1)) | |
min_list = max_list[::-1] | |
random_list = [random.randrange(min_val, max_val) for x in range(len(max_list))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import data | |
#@profile | |
def quick_sort(dataset): | |
if not dataset or len(dataset) == 1: | |
return dataset | |
if len(dataset) == 2: | |
a, b = dataset | |
if a > b: | |
return [b, a] | |
return [a, b] | |
pivot = dataset[-1] | |
dataset = dataset[:-1] | |
left_data = [x for x in dataset if x < pivot] | |
right_data = [x for x in dataset if pivot <= x] | |
left_values = quick_sort(left_data) | |
right_values = quick_sort(right_data) | |
return left_values + [pivot] + right_values | |
def bubble_sort(dataset): | |
while True: | |
pv = None | |
unsorted_found = 0 | |
for i, v in enumerate(dataset.copy()): | |
if pv is not None and pv > v: | |
dataset[i-1] = v | |
dataset[i] = pv | |
unsorted_found += 1 | |
continue | |
pv = v | |
if not unsorted_found: | |
return dataset | |
def gestapo_sort(dataset): | |
while True: | |
pv = None | |
unsorted_found = 0 | |
for v in dataset: | |
if pv is not None and pv > v: | |
unsorted_found += 1 | |
pv = v | |
if unsorted_found: | |
dataset.pop() | |
else: | |
break | |
return dataset | |
def test_alg(alg): | |
sorted_max_list = alg(data.max_list.copy()) | |
sorted_min_list = alg(data.min_list.copy()) | |
sorted_random_list = alg(data.random_list.copy()) | |
print(sorted_min_list) | |
print(f"Tested {alg.__name__}") | |
test_alg(quick_sort) | |
test_alg(bubble_sort) | |
test_alg(gestapo_sort) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment