forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrick_sort.py
More file actions
25 lines (20 loc) · 715 Bytes
/
brick_sort.py
File metadata and controls
25 lines (20 loc) · 715 Bytes
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
def brick_sort(arr):
"""Performs an odd-even in-place sort, which is a variation of a bubble
sort.
https://www.geeksforgeeks.org/odd-even-sort-brick-sort/
:param arr: the array of values to sort
:return: the sorted array
"""
# Initially array is unsorted
is_sorted = False
while not is_sorted:
is_sorted = True
for i in range(1, len(arr) - 1, 2):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
is_sorted = False
for i in range(0, len(arr) - 1, 2):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
is_sorted = False
return arr