Odd-Even Sort / Brick Sort Last Updated : 31 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like 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. C++ // A C++ Program to implement Odd-Even / Brick Sort #include<bits/stdc++.h> using namespace std; // A function to sort the algorithm using Odd Even sort void oddEvenSort(int arr[], int n) { bool isSorted = false; // Initially array is unsorted while (!isSorted) { isSorted = true; // Perform Bubble sort on odd indexed element for (int i=1; i<=n-2; i=i+2) { if (arr[i] > arr[i+1]) { swap(arr[i], arr[i+1]); isSorted = false; } } // Perform Bubble sort on even indexed element for (int i=0; i<=n-2; i=i+2) { if (arr[i] > arr[i+1]) { swap(arr[i], arr[i+1]); isSorted = false; } } } return; } // A utility function to print an array of size n void printArray(int arr[], int n) { for (int i=0; i < n; i++) cout << arr[i] << " "; cout << "\n"; } // Driver program to test above functions. int main() { int arr[] = {34, 2, 10, -9}; int n = sizeof(arr)/sizeof(arr[0]); oddEvenSort(arr, n); printArray(arr, n); return (0); } Java // Java Program to implement // Odd-Even / Brick Sort import java.io.*; class GFG { public static void oddEvenSort(int arr[], int n) { boolean isSorted = false; // Initially array is unsorted while (!isSorted) { isSorted = true; int temp =0; // Perform Bubble sort on odd indexed element for (int i=1; i<=n-2; i=i+2) { if (arr[i] > arr[i+1]) { temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; isSorted = false; } } // Perform Bubble sort on even indexed element for (int i=0; i<=n-2; i=i+2) { if (arr[i] > arr[i+1]) { temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; isSorted = false; } } } return; } public static void main (String[] args) { int arr[] = {34, 2, 10, -9}; int n = arr.length; oddEvenSort(arr, n); for (int i=0; i < n; i++) System.out.print(arr[i] + " "); System.out.println(" "); } } // Code Contributed by Mohit Gupta_OMG <(0_o)> 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 Contributed by Mohit Gupta_OMG <(0_o)> C# // C# Program to implement // Odd-Even / Brick Sort using System; class GFG { public static void oddEvenSort(int []arr, int n) { // Initially array is unsorted bool isSorted = false; while (!isSorted) { isSorted = true; int temp =0; // Perform Bubble sort on // odd indexed element for (int i = 1; i <= n - 2; i = i + 2) { if (arr[i] > arr[i+1]) { temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; isSorted = false; } } // Perform Bubble sort on // even indexed element for (int i = 0; i <= n - 2; i = i + 2) { if (arr[i] > arr[i+1]) { temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; isSorted = false; } } } return; } // Driver code public static void Main () { int []arr = {34, 2, 10, -9}; int n = arr.Length; // Function calling oddEvenSort(arr, n); for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); Console.WriteLine(" "); } } // This code is contributed by Sam007 JavaScript <script> // Javascript Program to implement // Odd-Even / Brick Sort function oddEvenSort(arr, n) { // Initially array is unsorted let isSorted = false; while (!isSorted) { isSorted = true; let temp =0; // Perform Bubble sort on odd indexed element for (let i=1; i<=n-2; i=i+2) { if (arr[i] > arr[i+1]) { temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; isSorted = false; } } // Perform Bubble sort on even indexed element for (let i=0; i<=n-2; i=i+2) { if (arr[i] > arr[i+1]) { temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; isSorted = false; } } } return; } // Driver Code let arr = [34, 2, 10, -9]; let n = arr.length; oddEvenSort(arr, n); for (let i=0; i < n; i++) document.write(arr[i] + " "); document.write(" "); </script> Output : -9 2 10 34 We demonstrate the above algorithm using the below illustration on the array = {3, 2, 3, 8, 5, 6, 4, 1} Please refer wiki for proof of correctness.Time Complexity : O(N2) where, N = Number of elements in the input array.Auxiliary Space : O(1). Just like bubble sort this is also an in-place algorithm.Exercise In our program in each iteration we first do bubble sort on odd indexed elements and then a bubble sort on the even indexed elements.Will we get a sorted result if we first perform a bubble sort on even indexed element first and then on the odd indexed element ?References https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort Comment More infoAdvertise with us Next Article Odd-Even Sort / Brick Sort K kartik Improve Article Tags : Sorting DSA Practice Tags : Sorting Similar Reads Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ 3 min read Introduction to Sorting Techniques â Data Structure and Algorithm Tutorials Sorting refers to rearrangement of a given array or list of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure. Why Sorting Algorithms are ImportantThe sorting algorithm is important in Com 3 min read Most Common Sorting AlgorithmsSelection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an 8 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Heap Sort - Data Structures and Algorithms Tutorials Heap sort is a comparison-based sorting technique based on Binary Heap Data Structure. It can be seen as an optimization over selection sort where we first find the max (or min) element and swap it with the last (or first). We repeat the same process for the remaining elements. In Heap Sort, we use 14 min read Counting Sort - Data Structures and Algorithms Tutorials Counting Sort is a non-comparison-based sorting algorithm. It is particularly efficient when the range of input values is small compared to the number of elements to be sorted. The basic idea behind Counting Sort is to count the frequency of each distinct element in the input array and use that info 9 min read Like