Java Program for Odd-Even Sort / Brick Sort Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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. 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 Contribute by Mohit Gupta_OMG <(0_o)> Output:-9 2 10 34 Time Complexity : O(N2) where, N = Number of elements in the input array.Auxiliary Space : O(1). This is an in-place algorithm, so no extra space is required. Please refer complete article on Odd-Even Sort / Brick Sort for more details! Comment More infoAdvertise with us Next Article Java Program for Menu Driven Sorting of Array K kartik Follow Improve Article Tags : Sorting Java Programs DSA Practice Tags : Sorting Similar Reads Java Program for Bitonic Sort Bitonic Sequence: A sequence is called Bitonic if it is first increasing, then decreasing. In other words, an array arr[0..n-i] is Bitonic if there exists an index i where 0<=i<=n-1 such that x0 <= x1 â¦..<= xi and xi >= xi+1â¦.. >= xn-1 A sequence, sorted in increasing order is cons 4 min read Java Program for Menu Driven Sorting of Array In Java, sorting an array consists of arranging the elements in a particular order, such as ascending or descending. This can be achieved using various algorithms like Bubble Sort, Selection Sort, or Insertion Sort. A menu-driven program allows users to select the desired sorting method dynamically. 7 min read Java Program for QuickSort Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of QuickSort that pick pivot in different ways.Always pick first element as pivot.Always pick last element as pivot (im 2 min read Java Program for Pigeonhole Sort Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists of elements where the number of elements and the number of possible key values are approximately the same. It requires O(n + Range) time where n is the number of elements in the input array and âRangeâ is the number of poss 3 min read Java Program to Check if a Given Integer is Odd or Even A number that is divisible by 2 and generates a remainder of 0 is called an even number. All the numbers ending with 0, 2, 4, 6, and 8 are even numbers. On the other hand, number that is not divisible by 2 and generates a remainder of 1 is called an odd number. All the numbers ending with 1, 3, 5,7, 7 min read Java Program to Store Even & Odd Elements of an Array into Separate Arrays Given an array with N numbers and separate those numbers into two arrays by odd numbers or even numbers. The complete operation required O(n) time complexity in the best case. For optimizing the memory uses, the first traverse through an array and calculate the total number of even and odd numbers i 2 min read Like