From f1fe6da9fa32166ff7e2152ba471837388ade5e3 Mon Sep 17 00:00:00 2001 From: Ocean Monjur <75680423+OCM-7898@users.noreply.github.com> Date: Wed, 10 Feb 2021 23:20:58 +0600 Subject: [PATCH 1/3] odd_even_sort.py --- sorts/odd_even_sort.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 sorts/odd_even_sort.py diff --git a/sorts/odd_even_sort.py b/sorts/odd_even_sort.py new file mode 100644 index 000000000000..d5f2de60d558 --- /dev/null +++ b/sorts/odd_even_sort.py @@ -0,0 +1,47 @@ +"""For reference +https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort +""" + + +def odd_even_sort(input_list): + """this algorithm uses the same idea of bubblesort, + but by first dividing in two phase (odd and even). + Originally developed for use on parallel processors + with local interconnections. + :param collection: mutable ordered sequence of elements + :return: same collection in ascending order + Examples: + >>> odd_even_sort([5 , 4 ,3 ,2 ,1]) + [1, 2, 3, 4, 5] + >>> odd_even_sort([]) + [] + >>> odd_even_sort([-10 ,-1 ,10 ,2]) + [-10, -1, 2, 10] + >>> odd_even_sort([1 ,2 ,3 ,4]) + [1, 2, 3, 4] + """ + sorted = False + while sorted is False: # Until all the indices are traversed keep looping + sorted = True + for i in range(0, len(input_list) - 1, 2): # iterating over all even indices + if input_list[i] > input_list[i + 1]: + + input_list[i], input_list[i + 1] = input_list[i + 1], input_list[i] + # swapping if elements not in order + sorted = False + + for i in range(1, len(input_list) - 1, 2): # iterating over all odd indices + if input_list[i] > input_list[i + 1]: + input_list[i], input_list[i + 1] = input_list[i + 1], input_list[i] + # swapping if elements not in order + sorted = False + return input_list + + +if __name__ == "__main__": + print("Enter list to be sorted") + input_list = [int(x) for x in input().split()] + # inputing elements of the list in one line + sorted_list = odd_even_sort(input_list) + print("The sorted list is") + print(sorted_list) From 30cf0fdf72a2626d6563f43d157c33b7f16b5d9c Mon Sep 17 00:00:00 2001 From: Ocean Monjur <75680423+OCM-7898@users.noreply.github.com> Date: Wed, 10 Feb 2021 23:27:51 +0600 Subject: [PATCH 2/3] Update odd_even_sort.py --- sorts/odd_even_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/odd_even_sort.py b/sorts/odd_even_sort.py index d5f2de60d558..f828cdcd2ab8 100644 --- a/sorts/odd_even_sort.py +++ b/sorts/odd_even_sort.py @@ -3,7 +3,7 @@ """ -def odd_even_sort(input_list): +def odd_even_sort(input_list) -> list: """this algorithm uses the same idea of bubblesort, but by first dividing in two phase (odd and even). Originally developed for use on parallel processors From 8537235d9ce45b9f6dbae49148f72f3dc4fc9950 Mon Sep 17 00:00:00 2001 From: Ocean Monjur <75680423+OCM-7898@users.noreply.github.com> Date: Wed, 10 Feb 2021 23:29:37 +0600 Subject: [PATCH 3/3] Update odd_even_sort.py --- sorts/odd_even_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/odd_even_sort.py b/sorts/odd_even_sort.py index f828cdcd2ab8..557337ee77bc 100644 --- a/sorts/odd_even_sort.py +++ b/sorts/odd_even_sort.py @@ -3,7 +3,7 @@ """ -def odd_even_sort(input_list) -> list: +def odd_even_sort(input_list: list) -> list: """this algorithm uses the same idea of bubblesort, but by first dividing in two phase (odd and even). Originally developed for use on parallel processors