Python Program for OddEven Sort / Brick Sort (original) (raw)
Given an array of integers, the task is to sort the elements in ascending order using Odd-Even Sort, also known as Brick Sort. This algorithm repeatedly performs two passes over the list one comparing adjacent odd-indexed pairs and the other comparing even-indexed pairs until the entire list becomes sorted.
**For Example:
**Input: [34, 2, 10, -9]
**Output: [-9, 2, 10, 34]
**Explanation: The algorithm alternates between odd and even phases, comparing and swapping elements in each phase until the array is sorted.
Algorithm Steps
- Start with an unsorted array.
- Perform an odd phase, where each odd-indexed element is compared with the next one and swapped if needed.
- Perform an even phase, where even-indexed elements are compared with the next one and swapped if needed.
- Repeat the odd and even phases alternately until no swaps are needed.
- The array becomes fully sorted when both passes make no swaps.
Working of Odd-Even Sort
Let’s understand how the algorithm works step by step using the example [34, 2, 10, -9]:
Initial Array: [34, 2, 10, -9]
- **Odd Phase (Compare indices 1 and 2): 2 and 10 are already in order -> [34, 2, 10, -9]
- **Even Phase (Compare indices 0 and 1, 2 and 3): Swap 34 and 2 -> [2, 34, 10, -9] and Swap 10 and -9 -> [2, 34, -9, 10]
- **Next Odd Phase: Compare 34 and -9 -> swap -> [2, -9, 34, 10]
- **Next Even Phase: Compare 2 and -9 -> swap -> [-9, 2, 34, 10] and Compare 34 and 10 -> swap -> [-9, 2, 10, 34]
No further swaps needed the array is now sorted.
Using Odd-Even Logic
In this approach, sorting is performed manually through alternating odd and even phases using loops and swaps. It gives a clear understanding of how the algorithm operates internally.
Python `
def oddEvenSort(arr, n): isSorted = 0 while isSorted == 0: isSorted = 1
# Odd phase
for i in range(1, n-1, 2):
if arr[i] > arr[i+1]:
arr[i], arr[i+1] = arr[i+1], arr[i]
isSorted = 0
# Even phase
for i in range(0, n-1, 2):
if arr[i] > arr[i+1]:
arr[i], arr[i+1] = arr[i+1], arr[i]
isSorted = 0arr = [34, 2, 10, -9] n = len(arr)
oddEvenSort(arr, n) print("Sorted array:", arr)
`
Output
Sorted array: [-9, 2, 10, 34]
**Explanation:
- The variable isSorted ensures the loop runs until no swaps occur.
- In each odd phase, elements at indices 1, 3, 5, … are compared with their next element.
- In the even phase, elements at indices 0, 2, 4, … are compared similarly.
- The swapping process continues until all elements are in the correct order.
Using NumPy for Vectorized Odd-Even Sorting
For larger datasets, NumPy can be used to optimize comparison and swapping using vectorized operations, which reduces execution time significantly compared to standard loops.
Python `
import numpy as np
def oddEvenSort_np(arr): arr = np.array(arr) n = len(arr) isSorted = False
while not isSorted:
isSorted = True
# Odd phase
odd_pairs = arr[1:n-1:2] > arr[2:n:2]
arr[1:n-1:2][odd_pairs], arr[2:n:2][odd_pairs] = arr[2:n:2][odd_pairs], arr[1:n-1:2][odd_pairs]
if np.any(odd_pairs):
isSorted = False
# Even phase
even_pairs = arr[0:n-1:2] > arr[1:n:2]
arr[0:n-1:2][even_pairs], arr[1:n:2][even_pairs] = arr[1:n:2][even_pairs], arr[0:n-1:2][even_pairs]
if np.any(even_pairs):
isSorted = False
return arr.tolist()arr = [34, 2, 10, -9] print("Sorted array:", oddEvenSort_np(arr))
`
Output
Sorted array: [-9, 2, 10, 34]
**Explanation:
- The list is converted to a NumPy array for vectorized processing.
- arr[1:n-1:2] and arr[2:n:2] select odd and even-indexed pairs.
- Boolean masks like odd_pairs help identify where swaps are needed.
Using Built-in sorted() Function
This approach leverages Python’s built-in sorted() function to simplify the sorting process. It sorts odd-indexed and even-indexed elements separately in each iteration, reducing manual comparisons and improving readability.
Python `
def oddEvenSort(arr, n): isSorted = 0 while isSorted == 0: isSorted = 1
# Sort odd and even indexed parts separately
arr[1::2], arr[2::2] = sorted(arr[1::2]), sorted(arr[2::2])
# Perform even-phase swaps if needed
for i in range(0, n-1, 2):
if arr[i] > arr[i+1]:
arr[i], arr[i+1] = arr[i+1], arr[i]
isSorted = 0arr = [34, 2, 10, -9] n = len(arr)
oddEvenSort(arr, n) print("Sorted array:", arr)
`
Output
Sorted array: [-9, 10, 2, 34]
**Explanation:
- The slicing arr[1::2] and arr[2::2] extract the odd and even indexed elements.
- sorted() sorts these slices internally, simplifying comparison logic.
- The second loop ensures the final adjustments are made to complete sorting.
Please refer complete article on Odd-Even Sort / Brick Sort for more details!