Last active
October 22, 2021 08:53
-
-
Save rblaine95/444ed8dd4378f4c6044e5302384bdda4 to your computer and use it in GitHub Desktop.
Bogosort - So long as a list is not sorted, randomize the order
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
#!/usr/bin/env python3 | |
from random import shuffle | |
def bogosort(l): | |
while not is_sorted(l): | |
shuffle(l) | |
return l | |
def is_sorted(l): | |
for i in range(len(l)-1): | |
if l[i] > l[i+1]: | |
return False | |
return True | |
def main(): | |
l = [i for i in range(10)] | |
shuffle(l) | |
print(l) | |
print(bogosort(l)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment