Open In App

Python Program for Odd-Even Sort / Brick Sort

Last Updated : 28 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases.

In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even indexed elements. 

Python3




# Python Program to implement
# Odd-Even / Brick Sort
 
def oddEvenSort(arr, n):
    # Initially array is unsorted
    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 = [34, 2, 10, -9]
n = len(arr)
 
oddEvenSort(arr, n);
for i in range(0, n):
    print(arr[i], end =" ")
     
# Code Contribute by Mohit Gupta_OMG <(0_o)>


Output

-9 2 10 34 

Time Complexity: O(n2)
Auxiliary Space: O(1)

Python Program for Odd-Even Sort / Brick Sort Using the sorted() function 

In this method, the sorted() function is used to sort odd-indexed and even-indexed elements of the array separately, and then assign them back to the original array using slicing. 

This replaces the first for loop in the original code. The rest of the code remains the same.

Below is the code for the above approach: 

Python3




def oddEvenSort(arr, n):
    isSorted = 0
    while isSorted == 0:
        isSorted = 1
        arr[1::2], arr[2::2] = sorted(arr[1::2]), sorted(arr[2::2])
         
        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 = [34, 2, 10, -9]
n = len(arr)
 
oddEvenSort(arr, n);
for i in range(0, n):
    print(arr[i], end =" ")


Output

-9 10 2 34 

Time Complexity: O(n2)
Auxiliary Space: O(1)

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



Previous Article
Next Article

Similar Reads

Python Program to Check if a Number is Odd or Even
Given a number and our task is to check number is Even or Odd using Python. Those numbers which are completely divisible by 2 or give 0 as the remainder are known as even number and those that are not or gives a remainder other than 0 are known as odd numbers. Example: Input: 2Output: Even numberInput: 41Output: Odd NumberCheck Even or Odd Number u
3 min read
Python program to count Even and Odd numbers in a List
Given a list of numbers, write a Python program to count Even and Odd numbers in a List. Example: Input: list1 = [2, 7, 5, 64, 14]Output: Even = 3, odd = 2 Input: list2 = [12, 14, 95, 3]Output: Even = 2, odd = 2 Example 1: Count Even and Odd numbers from the given list using for loop Iterate each element in the list using for loop and check if num
9 min read
Python program to find the sum of all even and odd digits of an integer list
The following article shows how given an integer list, we can produce the sum of all its odd and even digits. Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Ex
5 min read
Python Program to Print Largest Even and Largest Odd Number in a List
Auxiliary Given a list. The task is to print the largest even and largest odd number in a list. Examples: Input: 1 3 5 8 6 10 Output: Largest even number is 10 Largest odd number is 5 Input: 123 234 236 694 809 Output: Largest odd number is 809 Largest even number is 694 The first approach uses two methods , one for computing largest even number an
7 min read
Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List
Given a list. The task is to find the sum of Negative, Positive Even, and Positive Odd numbers present in the List. Examples: Input: -7 5 60 -34 1 Output: Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6 Input: 1 -1 50 -2 0 -3 Output: Sum of negative numbers is -6 Sum of even positive numbers is 50
7 min read
Python program to count Even and Odd numbers in a Dictionary
Given a python dictionary, the task is to count even and odd numbers present in the dictionary. Examples: Input : {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e' : 5}Output : Even = 2, odd = 3Input : {'x': 4, 'y':9, 'z':16}Output : Even = 2, odd = 1 Approach using values() Function: Traverse the dictionary and extract its elements using values() function and
3 min read
Python Program for Frequencies of even and odd numbers in a matrix
Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 18 } Output : Frequency of odd number = 4 Frequency
4 min read
Python Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order
Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where the first node is even, second odd, third even an
7 min read
Python Program For Rearranging A Linked List Such That All Even And Odd Positioned Nodes Are Together
Rearrange a linked list in such a way that all odd position nodes are together and all even positions node are together, Examples: Input: 1-&gt;2-&gt;3-&gt;4 Output: 1-&gt;3-&gt;2-&gt;4 Input: 10-&gt;22-&gt;30-&gt;43-&gt;56-&gt;70 Output: 10-&gt;30-&gt;56-&gt;22-&gt;43-&gt;70Recommended: Please solve it on "PRACTICE" first, before moving on to the
6 min read
Python Program To Check Whether The Length Of Given Linked List Is Even Or Odd
Given a linked list, the task is to make a function which checks whether the length of the linked list is even or odd. Examples: Input : 1-&gt;2-&gt;3-&gt;4-&gt;NULL Output : Even Input : 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL Output : OddRecommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method 1: Count the codes linea
4 min read