Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create odd_even sort.py #195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions odd_even sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def odd_even_sort(arr):
"""
Sorts the array using the Odd-Even Sort (Brick Sort) algorithm.

Args:
arr (list): List to be sorted.

Returns:
list: Sorted list.

"""
n = len(arr)
is_sorted = 0 # Initially set as 0 to enter the loop
while is_sorted == 0:
is_sorted = 1 # Assume the list is sorted
for i in range(1, n - 1, 2):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
is_sorted = 0 # If swapping occurs, the list is not sorted
for i in range(0, n - 1, 2):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
is_sorted = 0 # If swapping occurs, the list is not sorted
return arr


# Example usage
arr = [5, 2, 3, 1, 4]
print("Original array:", arr)
sorted_arr = odd_even_sort(arr)
print("Sorted array:", sorted_arr)