MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / odd_even_sort

Function odd_even_sort

sorts/odd_even_sort.py:8–42  ·  view source on GitHub ↗

Sort input with odd even sort. 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 :r

(input_list: list)

Source from the content-addressed store, hash-verified

6
7
8def odd_even_sort(input_list: list) -> list:
9 """
10 Sort input with odd even sort.
11
12 This algorithm uses the same idea of bubblesort,
13 but by first dividing in two phase (odd and even).
14 Originally developed for use on parallel processors
15 with local interconnections.
16 :param collection: mutable ordered sequence of elements
17 :return: same collection in ascending order
18 Examples:
19 >>> odd_even_sort([5 , 4 ,3 ,2 ,1])
20 [1, 2, 3, 4, 5]
21 >>> odd_even_sort([])
22 []
23 >>> odd_even_sort([-10 ,-1 ,10 ,2])
24 [-10, -1, 2, 10]
25 >>> odd_even_sort([1 ,2 ,3 ,4])
26 [1, 2, 3, 4]
27 """
28 is_sorted = False
29 while is_sorted is False: # Until all the indices are traversed keep looping
30 is_sorted = True
31 for i in range(0, len(input_list) - 1, 2): # iterating over all even indices
32 if input_list[i] > input_list[i + 1]:
33 input_list[i], input_list[i + 1] = input_list[i + 1], input_list[i]
34 # swapping if elements not in order
35 is_sorted = False
36
37 for i in range(1, len(input_list) - 1, 2): # iterating over all odd indices
38 if input_list[i] > input_list[i + 1]:
39 input_list[i], input_list[i + 1] = input_list[i + 1], input_list[i]
40 # swapping if elements not in order
41 is_sorted = False
42 return input_list
43
44
45if __name__ == "__main__":

Callers 1

odd_even_sort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected