Skip to content

Instantly share code, notes, and snippets.

@rblaine95
Last active October 22, 2021 08:53
Show Gist options
  • Save rblaine95/444ed8dd4378f4c6044e5302384bdda4 to your computer and use it in GitHub Desktop.
Save rblaine95/444ed8dd4378f4c6044e5302384bdda4 to your computer and use it in GitHub Desktop.
Bogosort - So long as a list is not sorted, randomize the order
#!/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