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

  1. Start with an unsorted array.
  2. Perform an odd phase, where each odd-indexed element is compared with the next one and swapped if needed.
  3. Perform an even phase, where even-indexed elements are compared with the next one and swapped if needed.
  4. Repeat the odd and even phases alternately until no swaps are needed.
  5. 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]

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 = 0

arr = [34, 2, 10, -9] n = len(arr)

oddEvenSort(arr, n) print("Sorted array:", arr)

`

Output

Sorted array: [-9, 2, 10, 34]

**Explanation:

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:

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 = 0

arr = [34, 2, 10, -9] n = len(arr)

oddEvenSort(arr, n) print("Sorted array:", arr)

`

Output

Sorted array: [-9, 10, 2, 34]

**Explanation:

Please refer complete article on Odd-Even Sort / Brick Sort for more details!