

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- Python Program for Insertion Sort
- Python Program for Selection Sort
- Python Program for Bubble 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
- Python Program for Iterative Merge Sort
Advertisements