Java Program for OddEven Sort / Brick Sort (original) (raw)
Last Updated : 31 May, 2022
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)>
`
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!
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 Bubble Sort Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. Bubble Sort in Java is not the best method to sort an array but is one of the most basic implementations for one to learn. In this article, we will learn how to write 2 min read
- Java Program for Comb Sort Comb Sort is mainly an improvement over Bubble Sort. Bubble sort always compares adjacent values. So all inversions are removed one by one. Comb Sort improves on Bubble Sort by using gap of size more than 1. The gap starts with a large value and shrinks by a factor of 1.3 in every iteration until it 2 min read
- Java Program for Counting Sort Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Java // Java implementation of Counting So 2 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 ShellSort In shellSort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h'th element is sorted. Java // Java implementation of ShellSort class ShellSort { /* An utility function to print array of si 2 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 for Heap Sort Heap sort is a comparison-based sorting technique based on the Binary Heap data structure. It is similar to the selection sort where first find the maximum element and place it at the end. We repeat the same process for the remaining element. Heap Sort in JavaBelow is the implementation of Heap Sort 3 min read
- Java Program to Sort Names in an Alphabetical Order For, sorting names in an Alphabetical order there are multiple ways to sort the array, like using inbuilt Arrays.sort() method or using normal sorting algorithms like the bubble sort, merge sort. Here let's use the bubble sort and inbuilt sort. Example: Input : Array[] = {"Sourabh", "Anoop, "Harsh", 3 min read