Python Program for Brick Sort Algorithm

Brick Sort Algorithm:

Brick Sort, commonly known as OddEven Sort, is a variant of Bubblesort. The sorting algorithm is separated into two stages: odd and even. The control loops until the array is sorted, ensuring that the even and odd stages are completed in each iteration.

You may be wondering what these odd and even stages represent. We will only sort the elements existing at the odd indexes when the control executes the odd phase. Similarly, during the event phase execution, the control will only sort the elements at the even indexes.

Examples:

Example1:

Input:

Given List = [12, 9, 20, 2, 1, 5, -4]

Output:

The given list after sorting =  [-4, 1, 2, 5, 9, 12, 20]

Example2:

Input:

Given List = [4, -12, 56, 125, 0, -3, 7]

Output:

The given list after sorting = [-12, -3, 0, 4, 7, 56, 125]

Program for Brick Sort Algorithm in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

• Give the list as static input and store it in a variable.
• Calculate the length of the given list using the len() function andÂ store it in another variable.
• Take a variable say sortng and initialize its value with zero.
• Loop until the above variable is equal to zero using the while loop.
• Inside the while loop, make the above sortng variable as 1.
• Loop from 1 to the list length-1 with the step size of 2 using the for loop. It is for sorting the Odd entries.
• Inside the loop, check if the iterator value of the given list is greater than the [iterator+1] value using the if conditional statement.
• If it is true, then swap both the values.
• Again assign 0 to the sortng variable.
• Loop from 0 to the list length-1 with the step size of 2 using the for loop. It is for sorting the Even entries.
• Inside the loop, check if the iterator value of the given list is greater than the [iterator+1] value using the if conditional statement.
• If it is true, then swap both the values.
• Make again the sortng variable as 0.
• Print the given list after sorting.
• The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [12, 9, 20, 2, 1, 5, -4]
# Calculate the length of the given list using the len() function and
# store it in another variable.
lst_len = len(gvn_lst)
# Take a variable and initialize its value with zero.
sortng = 0
# Loop until the above variable is equal to zero using the while loop.
while sortng == 0:
# Inside the while loop, make the above sortng variable as 1.
sortng = 1
# Loop from 1 to the list length-1 with the step size of 2 using the for loop.
# It is for sorting the Odd entries.
for itr in range(1, lst_len-1, 2):
# Inside the loop, check if the iterator value of the given list is
# greater than the [iterator+1] value using the if conditional statement.
if gvn_lst[itr] > gvn_lst[itr+1]:
# If it is true, then swap both the values.
gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr]
# Again assign 0 to the sortng variable.
sortng = 0
# Loop from 0 to the list length-1 with the step size of 2 using the for loop.
# It is for sorting the Even entries.
for itr in range(0, lst_len-1, 2):
# Inside the loop, check if the iterator value of the given list is greater
# than the [iterator+1] value using the if conditional statement.
if gvn_lst[itr] > gvn_lst[itr+1]:
# If it is true, then swap both the values.
gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr]
# Make again the sortng variable as 0.
sortng = 0
# Print the given list after sorting.
print("The given list after sorting = ", gvn_lst)


Output:

The given list after sorting =  [-4, 1, 2, 5, 9, 12, 20]

Method #2: Using Built-in Functions (User Input)

Approach:

• Give the list as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Calculate the length of the given list using the len() function andÂ store it in another variable.
• Take a variable say sortng and initialize its value with zero.
• Loop until the above variable is equal to zero using the while loop.
• Inside the while loop, make the above sortng variable as 1.
• Loop from 1 to the list length-1 with the step size of 2 using the for loop. It is for sorting the Odd entries.
• Inside the loop, check if the iterator value of the given list is greater than the [iterator+1] value using the if conditional statement.
• If it is true, then swap both the values.
• Again assign 0 to the sortng variable.
• Loop from 0 to the list length-1 with the step size of 2 using the for loop. It is for sorting the Even entries.
• Inside the loop, check if the iterator value of the given list is greater than the [iterator+1] value using the if conditional statement.
• If it is true, then swap both the values.
• Make again the sortng variable as 0.
• Print the given list after sorting.
• The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))

# Calculate the length of the given list using the len() function and
# store it in another variable.
lst_len = len(gvn_lst)
# Take a variable and initialize its value with zero.
sortng = 0
# Loop until the above variable is equal to zero using the while loop.
while sortng == 0:
# Inside the while loop, make the above sortng variable as 1.
sortng = 1
# Loop from 1 to the list length-1 with the step size of 2 using the for loop.
# It is for sorting the Odd entries.
for itr in range(1, lst_len-1, 2):
# Inside the loop, check if the iterator value of the given list is
# greater than the [iterator+1] value using the if conditional statement.
if gvn_lst[itr] > gvn_lst[itr+1]:
# If it is true, then swap both the values.
gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr]
# Again assign 0 to the sortng variable.
sortng = 0
# Loop from 0 to the list length-1 with the step size of 2 using the for loop.
# It is for sorting the Even entries.
for itr in range(0, lst_len-1, 2):
# Inside the loop, check if the iterator value of the given list is greater
# than the [iterator+1] value using the if conditional statement.
if gvn_lst[itr] > gvn_lst[itr+1]:
# If it is true, then swap both the values.
gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr]
# Make again the sortng variable as 0.
sortng = 0
# Print the given list after sorting.
print("The given list after sorting = ", gvn_lst)



Output:

Enter some random List Elements separated by spaces = 4 -12 56 125 0 -3 7
The given list after sorting = [-12, -3, 0, 4, 7, 56, 125]