-
Notifications
You must be signed in to change notification settings - Fork 65
/
oddeven.py
40 lines (31 loc) · 1.09 KB
/
oddeven.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
""" Implementation of Odd-Even Sort algorithm
"""
def oddeven(data):
""" OddEvenSort is a variation of bubble sort where the sorting is divided
into two phases, Odd and Even Phase and it runs until all the elements
are sorted. In the odd phase we perform bubble sort on odd indexed
elements and in the even phase we perform bubble sort on even indexed
elements.
:param array: list of elements that needs to be sorted
:type array: list
"""
is_sorted = False
data_len = len(data)
while not is_sorted:
is_sorted = True
for i in range(1, data_len-1, 2):
if data[i] > data[i+1]:
data[i], data[i+1] = data[i+1], data[i]
is_sorted = False
for i in range(0, data_len-1, 2):
if data[i] > data[i+1]:
data[i], data[i+1] = data[i+1], data[i]
is_sorted = False
def main():
""" operational function """
arr = [34, 56, 23, 67, 3, 68]
print(f"unsorted array: {arr}")
oddeven(arr)
print(f" sorted array: {arr}")
if __name__ == "__main__":
main()