![]() |
VOOZH | about |
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.
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.
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.
Sorted array: [-9, 2, 10, 34]
Explanation:
For larger datasets, NumPy can be used to optimize comparison and swapping using vectorized operations, which reduces execution time significantly compared to standard loops.
Sorted array: [-9, 2, 10, 34]
Explanation:
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.
Sorted array: [-9, 10, 2, 34]
Explanation:
Please refer complete article on Odd-Even Sort / Brick Sort for more details!