GoCoding.org

Program for Brick Sort using Python

by | Jun 22, 2021 | Python Programs

Introduction

The Brick sort algorithm is also known as odd-even sort. There are two phases in the algorithm: odd and even; and each iteration includes these two phases. The sorting algorithm performs bubble sort on the given array elements.

Program

def sort(ip_arr, n):
    is_sorted = 0
    while is_sorted == 0:
        is_sorted = 1
        temp = 0
        for i in range(1, n-1, 2):
            if ip_arr[i] > ip_arr[i + 1]:
                ip_arr[i], ip_arr[i + 1] = ip_arr[i + 1], ip_arr[i]
                is_sorted = 0
        for i in range(0, n-1, 2):
            if ip_arr[i] > ip_arr[i + 1]:
                ip_arr[i], ip_arr[i + 1] = ip_arr[i + 1], ip_arr[i]
                is_sorted = 0
    return


ip_arr = [10, 2, 51, 3]
sort(ip_arr, len(ip_arr))
print("The sorted array is :")
for i in range(0, len(ip_arr)):
    print(ip_arr[i])

Output

Program for Brick Sort using Python

Explanation

The brick sort is the modification of bubble sort algorithm. The steps followed in the above program are:

  • We have two phases, odd phase and even phase. In odd phase elements at odd index is taken and compared. If required the elements are swapped.
  • In even phase, the elements at even index is compared and swapped if required.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.