Last active
December 25, 2024 01:37
-
-
Save kimdcottrell/5a1e26ed4b65aa3c84efe67c2f51e02f to your computer and use it in GitHub Desktop.
A very cursed sort inspired by the three point turn in Austin Powers. No, this is not efficient at all. But it is funny.
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
list = [99, 2, 17, 103, 77, 3, 1, 8] | |
sorted = [] | |
i, n = 0, 1 # when we get to the end of the list, these indexes become -1, -2, then they become 0, 1 again when they re-rereach the top | |
reverse = False | |
things_to_append_on_reversal = [] | |
while list: | |
print(f"indexes: i {i}, n {n}, list: {list}") | |
if len(list) == 1: | |
sorted.append(list[i]) | |
if list[i] < list[n]: | |
if not sorted: | |
# if nothing exists yet, add it | |
sorted.insert(0, list[i]) | |
list.pop(i) | |
print(f"if nothing exists yet: sorted {sorted}, list:{list}, reverse: {reverse}") | |
elif list[i] < sorted[0]: | |
sorted.insert(0, list[i]) | |
list.pop(i) | |
print(f"list[{i}] < sorted[0]: sorted {sorted}, list:{list}, reverse: {reverse}") | |
elif list[i] < sorted[-1]: | |
# remove the last number from sorted, put it on the end of the list | |
list.append(sorted[-1]) | |
sorted.pop() | |
# if you append to the list now, the checks occuring in the same reversal sequence will be off by 1, so do it later | |
things_to_append_on_reversal.append(list[i]) | |
# add the current number to sorted, remove from the list | |
list.pop(i) | |
print(f"list[{i}] > sorted[-1]: sorted {sorted}, list:{list}, reverse: {reverse}") | |
else: | |
sorted.append(list[i]) | |
list.pop(i) | |
print(f"else: sorted {sorted}, list:{list}, reverse: {reverse}") | |
# iterate depending on reversing instruction | |
i = n | |
if reverse: | |
n-=1 | |
else: | |
n+=1 | |
# if we hit the end or beginning of a list, reverse | |
if n >= len(list) - 1 and not reverse: | |
n = -2 | |
i = -1 | |
reverse = True | |
list += things_to_append_on_reversal | |
things_to_append_on_reversal = [] | |
if abs(n) == len(list) and reverse: | |
n = 0 | |
i = 1 | |
reverse = False | |
list += things_to_append_on_reversal | |
things_to_append_on_reversal = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment