Created
July 20, 2021 08:19
-
-
Save alcides/20595c4b9fc8aeab7a9918f1bd334ae1 to your computer and use it in GitHub Desktop.
Insertion sort written in generators (to simulate haskell), so the head of a sorted list has linear complexity.
This file contains hidden or 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
def insert_sort_generator(li): | |
if len(li) <= 0: | |
return | |
h = li[0] | |
done = False | |
for v in insert_sort_generator(li[1:]): | |
if h < v and not done: | |
yield h | |
done = True | |
yield v | |
if not done: | |
yield h | |
print(list(insert_sort_generator([3,2,1,4,5,6]))) | |
x = next(insert_sort_generator([3,2,1,4,5,6])) | |
print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment