Goofing around with that intro to algorithms book again

This commit is contained in:
Dan Buch 2024-04-28 22:02:16 -04:00
parent e5166ee912
commit 4c2c6ef429
Signed by: meatballhat
GPG Key ID: A12F782281063434

View File

@ -0,0 +1,38 @@
import os
import sys
def main() -> int:
in_ints: list[int] = []
with open(sys.argv[1]) as infile:
for line in infile:
in_ints.append(int(line.strip()))
insertion_sort(in_ints)
for i in in_ints:
print(i)
return 0
def insertion_sort(in_ints: list[int]) -> None:
for i in range(len(in_ints) - 1):
j = i + 1
key = in_ints[j]
print(f"{in_ints} (save {key} from {j})", file=sys.stderr)
while i > -1 and in_ints[i] > key:
in_ints[i + 1] = in_ints[i]
print(f"{in_ints} (j={j} i={i})", file=sys.stderr)
i -= 1
in_ints[i + 1] = key
print(f"{in_ints} (insert {key} at {i+1})", file=sys.stderr)
if __name__ == "__main__":
sys.exit(main())