
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program for Odd-Even Sort / Brick Sort
In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given an array, we need to sort it using brick sort.
Here we have two phases: Odd and Even Phase. In the odd phase, bubble sort is performed on odd indexed elements and in the even phase, bubble sort is performed on even indexed elements.
Now let’s observe the solution in the implementation below−
Example
def oddEvenSort(arr, n): # flag isSorted = 0 while isSorted == 0: isSorted = 1 temp = 0 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 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 return arr = [1,4,2,3,6,5,8,7] n = len(arr) oddEvenSort(arr, n) print(“Sorted sequence is:”) for i in range(0, n): print(arr[i], end =" ")
Output
Sorted sequence is: 1 2 3 4 5 6 7 8
All the variables are declared in the local scope and their references are seen in the figure above.
Conclusion
In this article, we have learned about how we can make a Python Program for Odd-Even Sort /Brick Sort
- Related Articles
- C/C++ Program for Odd-Even Sort (Brick Sort)?
- C/C++ Program for the Odd-Even Sort (Brick Sort)?
- Odd even sort in an array - JavaScript
- Program to sort all even and odd numbers in increasing and decreasing order respectively in Python
- Python Program for Bubble Sort
- Python Program for Insertion Sort
- Python Program for Selection Sort
- Python Program for Cocktail Sort
- Python Program for Counting Sort
- Python Program for Cycle Sort
- Python Program for Gnome Sort
- Python Program for Heap Sort
- Python Program for Merge Sort
- Python Program for Stooge Sort
- Python Program for Binary Insertion Sort

Advertisements