From 8a335b6cd1edcf6e51d4f8b5ead9137b68372bef Mon Sep 17 00:00:00 2001 From: Nayif Ehan <128936184+las-r@users.noreply.github.com> Date: Sat, 21 Feb 2026 14:42:29 -0500 Subject: [PATCH 1/6] Create bozo_sort.py --- sorts/bozo_sort.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 sorts/bozo_sort.py diff --git a/sorts/bozo_sort.py b/sorts/bozo_sort.py new file mode 100644 index 000000000000..6681a941a0fb --- /dev/null +++ b/sorts/bozo_sort.py @@ -0,0 +1,44 @@ +""" +This is a pure Python implementation of the bozosort algorithm. + +Bozosort chooses two elements at random and swaps them. It does +this repeatedly until the list is sorted. + +For more info, check "Bozosort": https://en.wikipedia.org/wiki/Bogosort#Related_algorithms + +For doctests run following command: +python -m doctest -v bozo_sort.py +or +python3 -m doctest -v bozo_sort.py + +For manual testing run: +python bozo_sort.py +""" + +import random + + +def bozo_sort(array: list): + """ + Pure Python implementation of the bozosort algorithm. + Examples: + >>> bozo_sort([1, 14, 20, 9, 5]) + [1, 5, 9, 14, 20] + >>> bozo_sort([8, 16, 0, 4, 10]) + [0, 4, 8, 10, 16] + """ + + while array != sorted(array): + index_a = random.randint(0, len(array) - 1) + index_b = random.randint(0, len(array) - 1) + + array[index_a], array[index_b] = array[index_b], array[index_a] + + return array + + +if __name__ == "__main__": + user_array = input("Enter numbers separated by spaces: ") + unsorted = [int(i) for i in user_array.split(" ")] + + print(bozo_sort(unsorted)) From 2a55aa29d3624ff1731388e73124e83113310b24 Mon Sep 17 00:00:00 2001 From: Nayif Ehan <128936184+las-r@users.noreply.github.com> Date: Sat, 21 Feb 2026 14:46:41 -0500 Subject: [PATCH 2/6] Update bozo_sort.py --- sorts/bozo_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/bozo_sort.py b/sorts/bozo_sort.py index 6681a941a0fb..7095c0b3174e 100644 --- a/sorts/bozo_sort.py +++ b/sorts/bozo_sort.py @@ -18,7 +18,7 @@ import random -def bozo_sort(array: list): +def bozo_sort(array: list) -> list: """ Pure Python implementation of the bozosort algorithm. Examples: From bf619d9aa872d8cbcd08ed465139fd9fcb2fbcfb Mon Sep 17 00:00:00 2001 From: Nayif Ehan <128936184+las-r@users.noreply.github.com> Date: Thu, 19 Mar 2026 15:03:58 -0400 Subject: [PATCH 3/6] Remove use of sorted() Replaced with custom is_sorted() --- sorts/bozo_sort.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sorts/bozo_sort.py b/sorts/bozo_sort.py index 7095c0b3174e..fd737410e000 100644 --- a/sorts/bozo_sort.py +++ b/sorts/bozo_sort.py @@ -27,8 +27,14 @@ def bozo_sort(array: list) -> list: >>> bozo_sort([8, 16, 0, 4, 10]) [0, 4, 8, 10, 16] """ - - while array != sorted(array): + + def is_sorted(array): + for i, n in enumerate(array): + if i < len(array) - 1 and n > array[i + 1]: + return False + return True + + while not is_sorted(array): index_a = random.randint(0, len(array) - 1) index_b = random.randint(0, len(array) - 1) From 8c1ab9a2b87d3c836af51b5f453d2dc6336f3d3d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Mar 2026 19:04:20 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/bozo_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/bozo_sort.py b/sorts/bozo_sort.py index fd737410e000..a7a9c4c87eca 100644 --- a/sorts/bozo_sort.py +++ b/sorts/bozo_sort.py @@ -27,7 +27,7 @@ def bozo_sort(array: list) -> list: >>> bozo_sort([8, 16, 0, 4, 10]) [0, 4, 8, 10, 16] """ - + def is_sorted(array): for i, n in enumerate(array): if i < len(array) - 1 and n > array[i + 1]: From 7c4cc3abdc2bb5da1b7659c919b1941489276fe3 Mon Sep 17 00:00:00 2001 From: Nayif Ehan <128936184+las-r@users.noreply.github.com> Date: Thu, 19 Mar 2026 15:07:11 -0400 Subject: [PATCH 5/6] Add type hints to is_sorted() --- sorts/bozo_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/bozo_sort.py b/sorts/bozo_sort.py index a7a9c4c87eca..86245cfa0a7c 100644 --- a/sorts/bozo_sort.py +++ b/sorts/bozo_sort.py @@ -27,8 +27,8 @@ def bozo_sort(array: list) -> list: >>> bozo_sort([8, 16, 0, 4, 10]) [0, 4, 8, 10, 16] """ - - def is_sorted(array): + + def is_sorted(array: list) -> bool: for i, n in enumerate(array): if i < len(array) - 1 and n > array[i + 1]: return False From d35bb684230bd405190aa1ff5a163ea48d7c5075 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Mar 2026 19:07:31 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/bozo_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/bozo_sort.py b/sorts/bozo_sort.py index 86245cfa0a7c..2ccbd1a330da 100644 --- a/sorts/bozo_sort.py +++ b/sorts/bozo_sort.py @@ -27,7 +27,7 @@ def bozo_sort(array: list) -> list: >>> bozo_sort([8, 16, 0, 4, 10]) [0, 4, 8, 10, 16] """ - + def is_sorted(array: list) -> bool: for i, n in enumerate(array): if i < len(array) - 1 and n > array[i + 1]: