Pure implementation of the bogosort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending Examples: >>> bogo_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> bogo_sort([
(collection: list)
| 17 | |
| 18 | |
| 19 | def bogo_sort(collection: list) -> list: |
| 20 | """Pure implementation of the bogosort algorithm in Python |
| 21 | :param collection: some mutable ordered collection with heterogeneous |
| 22 | comparable items inside |
| 23 | :return: the same collection ordered by ascending |
| 24 | Examples: |
| 25 | >>> bogo_sort([0, 5, 3, 2, 2]) |
| 26 | [0, 2, 2, 3, 5] |
| 27 | >>> bogo_sort([]) |
| 28 | [] |
| 29 | >>> bogo_sort([-2, -5, -45]) |
| 30 | [-45, -5, -2] |
| 31 | """ |
| 32 | |
| 33 | def is_sorted(collection: list) -> bool: |
| 34 | for i in range(len(collection) - 1): |
| 35 | if collection[i] > collection[i + 1]: |
| 36 | return False |
| 37 | return True |
| 38 | |
| 39 | while not is_sorted(collection): |
| 40 | random.shuffle(collection) |
| 41 | return collection |
| 42 | |
| 43 | |
| 44 | if __name__ == "__main__": |