From 81673c70f14ceec02c2d829acae57fb99d3a5045 Mon Sep 17 00:00:00 2001 From: Krishna-singhal <65902764+Krishna-Singhal@users.noreply.github.com> Date: Sun, 22 Oct 2023 11:32:52 +0530 Subject: [PATCH 1/9] don't need to return list because list is mutable * Don't need to return list as list is mutable * use advantage of python in swapping * filter blank inputs from input list * minor changes --- sorts/double_sort.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/sorts/double_sort.py b/sorts/double_sort.py index a19641d94752..683611755b4d 100644 --- a/sorts/double_sort.py +++ b/sorts/double_sort.py @@ -22,21 +22,16 @@ def double_sort(lst): if ( lst[j + 1] < lst[j] ): # applying bubble sort algorithm from left to right (or forwards) - temp = lst[j + 1] - lst[j + 1] = lst[j] - lst[j] = temp + lst[j], lst[j + 1] = lst[j + 1], lst[j] if ( lst[no_of_elements - 1 - j] < lst[no_of_elements - 2 - j] ): # applying bubble sort algorithm from right to left (or backwards) - temp = lst[no_of_elements - 1 - j] - lst[no_of_elements - 1 - j] = lst[no_of_elements - 2 - j] - lst[no_of_elements - 2 - j] = temp - return lst + lst[no_of_elements - 1 - j], lst[no_of_elements - 2 - j] = lst[no_of_elements - 2 - j], lst[no_of_elements - 1 - j] if __name__ == "__main__": - print("enter the list to be sorted") - lst = [int(x) for x in input().split()] # inputing elements of the list in one line - sorted_lst = double_sort(lst) + print("Enter the list to be sorted: ") + lst = [int(x) if x for x in input().split()] # inputing elements of the list in one line + double_sort(lst) print("the sorted list is") - print(sorted_lst) + print(lst) From c7673b32e792ea7cc1bf8dcae6fc6931df6bba25 Mon Sep 17 00:00:00 2001 From: Krishna-singhal <65902764+Krishna-Singhal@users.noreply.github.com> Date: Sun, 22 Oct 2023 11:48:20 +0530 Subject: [PATCH 2/9] minor mistake --- sorts/double_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/double_sort.py b/sorts/double_sort.py index 683611755b4d..4ebe72a352c4 100644 --- a/sorts/double_sort.py +++ b/sorts/double_sort.py @@ -31,7 +31,7 @@ def double_sort(lst): if __name__ == "__main__": print("Enter the list to be sorted: ") - lst = [int(x) if x for x in input().split()] # inputing elements of the list in one line + lst = [int(x) for x in input().split() if x] # inputing elements of the list in one line double_sort(lst) print("the sorted list is") print(lst) From fc0ea327c2d3a992602f48f8d4a887df1605a615 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Oct 2023 06:18:54 +0000 Subject: [PATCH 3/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/double_sort.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sorts/double_sort.py b/sorts/double_sort.py index 4ebe72a352c4..709123ba531f 100644 --- a/sorts/double_sort.py +++ b/sorts/double_sort.py @@ -26,12 +26,17 @@ def double_sort(lst): if ( lst[no_of_elements - 1 - j] < lst[no_of_elements - 2 - j] ): # applying bubble sort algorithm from right to left (or backwards) - lst[no_of_elements - 1 - j], lst[no_of_elements - 2 - j] = lst[no_of_elements - 2 - j], lst[no_of_elements - 1 - j] + lst[no_of_elements - 1 - j], lst[no_of_elements - 2 - j] = ( + lst[no_of_elements - 2 - j], + lst[no_of_elements - 1 - j], + ) if __name__ == "__main__": print("Enter the list to be sorted: ") - lst = [int(x) for x in input().split() if x] # inputing elements of the list in one line + lst = [ + int(x) for x in input().split() if x + ] # inputing elements of the list in one line double_sort(lst) print("the sorted list is") print(lst) From 8eed6e354b646139c85877a121e53292801c0e51 Mon Sep 17 00:00:00 2001 From: Krishna-singhal <65902764+Krishna-Singhal@users.noreply.github.com> Date: Sun, 22 Oct 2023 12:03:11 +0530 Subject: [PATCH 4/9] more readable --- sorts/double_sort.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sorts/double_sort.py b/sorts/double_sort.py index 709123ba531f..10f9e54bfabe 100644 --- a/sorts/double_sort.py +++ b/sorts/double_sort.py @@ -1,4 +1,4 @@ -def double_sort(lst): +def double_sort(lst) -> None: """This sorting algorithm sorts an array using the principle of bubble sort, but does it both from left to right and right to left. Hence, it's called "Double sort" @@ -34,9 +34,9 @@ def double_sort(lst): if __name__ == "__main__": print("Enter the list to be sorted: ") - lst = [ + unsorted = [ int(x) for x in input().split() if x ] # inputing elements of the list in one line double_sort(lst) print("the sorted list is") - print(lst) + print(unsorted) From 03e94479a13830f37a2ccfbb1a2e6d77546badaf Mon Sep 17 00:00:00 2001 From: Krishna-singhal <65902764+Krishna-Singhal@users.noreply.github.com> Date: Sun, 22 Oct 2023 12:05:23 +0530 Subject: [PATCH 5/9] Update double_sort.py --- sorts/double_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/double_sort.py b/sorts/double_sort.py index 10f9e54bfabe..492b7f6fdfbd 100644 --- a/sorts/double_sort.py +++ b/sorts/double_sort.py @@ -37,6 +37,6 @@ def double_sort(lst) -> None: unsorted = [ int(x) for x in input().split() if x ] # inputing elements of the list in one line - double_sort(lst) + double_sort(unsorted) print("the sorted list is") print(unsorted) From 38ce189f7ba9e814f5c64f762db85a4b0d9217d9 Mon Sep 17 00:00:00 2001 From: Krishna-singhal <65902764+Krishna-Singhal@users.noreply.github.com> Date: Sun, 22 Oct 2023 18:20:35 +0530 Subject: [PATCH 6/9] last fixes --- sorts/double_sort.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/sorts/double_sort.py b/sorts/double_sort.py index 492b7f6fdfbd..89d748486b40 100644 --- a/sorts/double_sort.py +++ b/sorts/double_sort.py @@ -1,4 +1,7 @@ -def double_sort(lst) -> None: +from typing import Any + + +def double_sort(collection: list[Any]) -> list[Any]: """This sorting algorithm sorts an array using the principle of bubble sort, but does it both from left to right and right to left. Hence, it's called "Double sort" @@ -14,22 +17,23 @@ def double_sort(lst) -> None: >>> double_sort([-3, 10, 16, -42, 29]) == sorted([-3, 10, 16, -42, 29]) True """ - no_of_elements = len(lst) + no_of_elements = len(collection) for _ in range( int(((no_of_elements - 1) / 2) + 1) ): # we don't need to traverse to end of list as for j in range(no_of_elements - 1): if ( - lst[j + 1] < lst[j] + collection[j + 1] < collection[j] ): # applying bubble sort algorithm from left to right (or forwards) - lst[j], lst[j + 1] = lst[j + 1], lst[j] + collection[j], collection[j + 1] = collection[j + 1], collection[j] if ( - lst[no_of_elements - 1 - j] < lst[no_of_elements - 2 - j] + collection[no_of_elements - 1 - j] < collection[no_of_elements - 2 - j] ): # applying bubble sort algorithm from right to left (or backwards) - lst[no_of_elements - 1 - j], lst[no_of_elements - 2 - j] = ( - lst[no_of_elements - 2 - j], - lst[no_of_elements - 1 - j], + collection[no_of_elements - 1 - j], collection[no_of_elements - 2 - j] = ( + collection[no_of_elements - 2 - j], + collection[no_of_elements - 1 - j], ) + return collection if __name__ == "__main__": @@ -37,6 +41,5 @@ def double_sort(lst) -> None: unsorted = [ int(x) for x in input().split() if x ] # inputing elements of the list in one line - double_sort(unsorted) print("the sorted list is") - print(unsorted) + print(double_sort(unsorted)) From 9d2215832899b53b8d4abfd08edf5d18f282612a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Oct 2023 12:51:10 +0000 Subject: [PATCH 7/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/double_sort.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sorts/double_sort.py b/sorts/double_sort.py index 89d748486b40..07981d36dad0 100644 --- a/sorts/double_sort.py +++ b/sorts/double_sort.py @@ -29,7 +29,10 @@ def double_sort(collection: list[Any]) -> list[Any]: if ( collection[no_of_elements - 1 - j] < collection[no_of_elements - 2 - j] ): # applying bubble sort algorithm from right to left (or backwards) - collection[no_of_elements - 1 - j], collection[no_of_elements - 2 - j] = ( + ( + collection[no_of_elements - 1 - j], + collection[no_of_elements - 2 - j], + ) = ( collection[no_of_elements - 2 - j], collection[no_of_elements - 1 - j], ) From dc85ae53a5eca9cad7589db525b79473cb8a1781 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 23 Oct 2023 09:46:31 +0200 Subject: [PATCH 8/9] Apply suggestions from code review --- sorts/double_sort.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sorts/double_sort.py b/sorts/double_sort.py index 07981d36dad0..7bee7fc2252b 100644 --- a/sorts/double_sort.py +++ b/sorts/double_sort.py @@ -22,13 +22,15 @@ def double_sort(collection: list[Any]) -> list[Any]: int(((no_of_elements - 1) / 2) + 1) ): # we don't need to traverse to end of list as for j in range(no_of_elements - 1): + # apply the bubble sort algorithm from left to right (or forwards) if ( collection[j + 1] < collection[j] - ): # applying bubble sort algorithm from left to right (or forwards) + ): collection[j], collection[j + 1] = collection[j + 1], collection[j] + # apply the bubble sort algorithm from right to left (or backwards) if ( collection[no_of_elements - 1 - j] < collection[no_of_elements - 2 - j] - ): # applying bubble sort algorithm from right to left (or backwards) + ): ( collection[no_of_elements - 1 - j], collection[no_of_elements - 2 - j], @@ -40,9 +42,7 @@ def double_sort(collection: list[Any]) -> list[Any]: if __name__ == "__main__": - print("Enter the list to be sorted: ") - unsorted = [ - int(x) for x in input().split() if x - ] # inputing elements of the list in one line + # allow the user to input the elements of the list on one line + unsorted = [int(x) for x in input("Enter the list to be sorted: ").split() if x] print("the sorted list is") - print(double_sort(unsorted)) + print(f"{double_sort(unsorted) = }") From 4e6e50ea2a5c2f6e2054a43446bf43919e859755 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 07:47:24 +0000 Subject: [PATCH 9/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/double_sort.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/sorts/double_sort.py b/sorts/double_sort.py index 7bee7fc2252b..bd5fdca1e63c 100644 --- a/sorts/double_sort.py +++ b/sorts/double_sort.py @@ -23,14 +23,10 @@ def double_sort(collection: list[Any]) -> list[Any]: ): # we don't need to traverse to end of list as for j in range(no_of_elements - 1): # apply the bubble sort algorithm from left to right (or forwards) - if ( - collection[j + 1] < collection[j] - ): + if collection[j + 1] < collection[j]: collection[j], collection[j + 1] = collection[j + 1], collection[j] # apply the bubble sort algorithm from right to left (or backwards) - if ( - collection[no_of_elements - 1 - j] < collection[no_of_elements - 2 - j] - ): + if collection[no_of_elements - 1 - j] < collection[no_of_elements - 2 - j]: ( collection[no_of_elements - 1 - j], collection[no_of_elements - 2 - j],