From 6c317b93aabd1a18282c7d934f1649104f09a1da Mon Sep 17 00:00:00 2001 From: Bonnie <653778@pdsb.net> Date: Sat, 7 Aug 2021 17:58:42 -0400 Subject: [PATCH 1/4] Create exchange_sort.py added exchange sort --- sorts/exchange_sort.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 sorts/exchange_sort.py diff --git a/sorts/exchange_sort.py b/sorts/exchange_sort.py new file mode 100644 index 000000000000..b55cf64b1f42 --- /dev/null +++ b/sorts/exchange_sort.py @@ -0,0 +1,25 @@ +def exchange_sort(numbers: list[int]) -> list[int]: + """ + Uses exchange sort to sort a list of numbers. + Source: https://en.wikipedia.org/wiki/Sorting_algorithm#Exchange_sort + >>> exchange_sort([5, 4, 3, 2, 1]) + [1, 2, 3, 4, 5] + >>> exchange_sort([-1, -2, -3]) + [-5, -4, -3, -2, -1] + >>> exchange_sort([1, 2, 3, 4, 5]) + [1, 2, 3, 4, 5] + >>> exchange_sort([0, 10, -2, 5, 3]) + [-2, 0, 3, 5, 10] + >>> exchange_sort([]) + [] + """ + for i in range(len(numbers)): + for j in range(i + 1, len(numbers)): + if numbers[j] < numbers[i]: + numbers[i], numbers[j] = numbers[j], numbers[i] + return numbers + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(exchange_sort(unsorted)) \ No newline at end of file From 17f0a9848f2d6b594f7117955f451a936e1268a0 Mon Sep 17 00:00:00 2001 From: Bonnie <653778@pdsb.net> Date: Sat, 7 Aug 2021 18:15:28 -0400 Subject: [PATCH 2/4] Fixed doctest in exchange_sort.py --- sorts/exchange_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/exchange_sort.py b/sorts/exchange_sort.py index b55cf64b1f42..e3f702c63891 100644 --- a/sorts/exchange_sort.py +++ b/sorts/exchange_sort.py @@ -5,7 +5,7 @@ def exchange_sort(numbers: list[int]) -> list[int]: >>> exchange_sort([5, 4, 3, 2, 1]) [1, 2, 3, 4, 5] >>> exchange_sort([-1, -2, -3]) - [-5, -4, -3, -2, -1] + [-3, -2, -1] >>> exchange_sort([1, 2, 3, 4, 5]) [1, 2, 3, 4, 5] >>> exchange_sort([0, 10, -2, 5, 3]) From 37a713ae81aa6cf0abfd9236776abebcf22c32fd Mon Sep 17 00:00:00 2001 From: Bonnie <653778@pdsb.net> Date: Wed, 11 Aug 2021 19:22:19 -0400 Subject: [PATCH 3/4] Fixed formatting error and added new length variable added empty line at end of exchange_sort.py and turned len(numbers) into a variable --- sorts/exchange_sort.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sorts/exchange_sort.py b/sorts/exchange_sort.py index e3f702c63891..2fef00549b68 100644 --- a/sorts/exchange_sort.py +++ b/sorts/exchange_sort.py @@ -13,8 +13,9 @@ def exchange_sort(numbers: list[int]) -> list[int]: >>> exchange_sort([]) [] """ - for i in range(len(numbers)): - for j in range(i + 1, len(numbers)): + numbers_length = len(numbers) + for i in range(numbers_length): + for j in range(i + 1, numbers_length): if numbers[j] < numbers[i]: numbers[i], numbers[j] = numbers[j], numbers[i] return numbers @@ -22,4 +23,4 @@ def exchange_sort(numbers: list[int]) -> list[int]: if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - print(exchange_sort(unsorted)) \ No newline at end of file + print(exchange_sort(unsorted)) From aeb878c4064c1acf404a7660605c9b3867981f5b Mon Sep 17 00:00:00 2001 From: Bonnie <653778@pdsb.net> Date: Sat, 14 Aug 2021 18:56:09 -0400 Subject: [PATCH 4/4] Fixed formatting errors with black added empty line --- sorts/exchange_sort.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sorts/exchange_sort.py b/sorts/exchange_sort.py index 2fef00549b68..1ce78a9dc0cb 100644 --- a/sorts/exchange_sort.py +++ b/sorts/exchange_sort.py @@ -20,6 +20,7 @@ def exchange_sort(numbers: list[int]) -> list[int]: numbers[i], numbers[j] = numbers[j], numbers[i] return numbers + if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")]