From 4a87d5d19f5f450c4417b0b041305e64b43f625f Mon Sep 17 00:00:00 2001 From: Kanakalatha Vemuru Date: Sat, 23 Oct 2021 16:45:18 +0530 Subject: [PATCH 1/5] Added circle sort implementation --- sorts/circle_sort.py | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 sorts/circle_sort.py diff --git a/sorts/circle_sort.py b/sorts/circle_sort.py new file mode 100644 index 000000000000..d7890a648033 --- /dev/null +++ b/sorts/circle_sort.py @@ -0,0 +1,80 @@ +""" +This is a Python implementation of the circle sort algorithm + +For doctests run following command: +python3 -m doctest -v circle_sort.py + +For manual testing run: +python3 circle_sort.py +""" + + +def circle_sort(collection: list) -> list: + """A pure Python implementation of circle sort algorithm + + :param collection: a mutable collection of comparable items in any order + :return: the same collection in ascending order + + Examples: + >>> circle_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + >>> circle_sort([]) + [] + >>> circle_sort([-2, 5, 0, -45]) + [-45, -2, 0, 5] + >>> collections = ([], [0, 5, 3, 2, 2], [-2, 5, 0, -45]) + >>> all(sorted(collection) == circle_sort(collection) for collection in collections) + True + """ + + if len(collection) < 2: + return collection + + def circle_sort_util(collection: list, low: int, high: int) -> bool: + swapped = False + + if low == high: + return swapped + + left = low + right = high + + while left < right: + if collection[left] > collection[right]: + collection[left], collection[right] = ( + collection[right], + collection[left], + ) + swapped = True + + left += 1 + right -= 1 + + if left == right: + if collection[left] > collection[right + 1]: + collection[left], collection[right + 1] = ( + collection[right + 1], + collection[left], + ) + + swapped = True + + mid = low + int((high - low) / 2) + left_swap = circle_sort_util(collection, low, mid) + right_swap = circle_sort_util(collection, mid + 1, high) + + return swapped or left_swap or right_swap + + is_not_sorted = True + + while is_not_sorted == True: + is_not_sorted = circle_sort_util(collection, 0, len(collection) - 1) + + return collection + + +# hard coded driver function to run the program +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(circle_sort(unsorted)) \ No newline at end of file From 09e5719a1e3c6e0e381498058cf65daf6559fa85 Mon Sep 17 00:00:00 2001 From: Kanakalatha Vemuru Date: Sat, 23 Oct 2021 21:11:07 +0530 Subject: [PATCH 2/5] Added modifications --- sorts/circle_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/circle_sort.py b/sorts/circle_sort.py index d7890a648033..2e73cfc91b50 100644 --- a/sorts/circle_sort.py +++ b/sorts/circle_sort.py @@ -77,4 +77,4 @@ def circle_sort_util(collection: list, low: int, high: int) -> bool: if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - print(circle_sort(unsorted)) \ No newline at end of file + print(circle_sort(unsorted)) From 7b6258ec07d9763ea105b7c91a783caf76e14066 Mon Sep 17 00:00:00 2001 From: Kanakalatha Vemuru Date: Sat, 23 Oct 2021 23:12:58 +0530 Subject: [PATCH 3/5] Added modifications --- sorts/circle_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/circle_sort.py b/sorts/circle_sort.py index 2e73cfc91b50..31a6c47fd3ee 100644 --- a/sorts/circle_sort.py +++ b/sorts/circle_sort.py @@ -67,7 +67,7 @@ def circle_sort_util(collection: list, low: int, high: int) -> bool: is_not_sorted = True - while is_not_sorted == True: + while is_not_sorted is True: is_not_sorted = circle_sort_util(collection, 0, len(collection) - 1) return collection From ca4dbc63b18a85865ee7ac453d5f19d0ada3edf7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 26 Oct 2021 19:14:34 +0200 Subject: [PATCH 4/5] Update circle_sort.py --- sorts/circle_sort.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sorts/circle_sort.py b/sorts/circle_sort.py index 31a6c47fd3ee..9114086cf725 100644 --- a/sorts/circle_sort.py +++ b/sorts/circle_sort.py @@ -73,7 +73,6 @@ def circle_sort_util(collection: list, low: int, high: int) -> bool: return collection -# hard coded driver function to run the program if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] From 3e0d8c98d4b4eca33d9337a9c46b3800ebd73b64 Mon Sep 17 00:00:00 2001 From: John Law Date: Sun, 17 Jul 2022 05:38:43 +0800 Subject: [PATCH 5/5] Update circle_sort.py --- sorts/circle_sort.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sorts/circle_sort.py b/sorts/circle_sort.py index 9114086cf725..da3c59059516 100644 --- a/sorts/circle_sort.py +++ b/sorts/circle_sort.py @@ -31,6 +31,14 @@ def circle_sort(collection: list) -> list: return collection def circle_sort_util(collection: list, low: int, high: int) -> bool: + """ + >>> arr = [5,4,3,2,1] + >>> circle_sort_util(lst, 0, 2) + True + >>> arr + [3, 4, 5, 2, 1] + """ + swapped = False if low == high: