📜  奇偶排序/砖排序的Java程序

📅  最后修改于: 2021-05-04 22:57:01             🧑  作者: Mango

这基本上是气泡排序的一种变体。该算法分为两个阶段-奇数阶段和偶数阶段。该算法一直运行到对数组元素进行排序,并在每次迭代中出现两个阶段-奇数阶段和偶数阶段。

在奇数阶段,我们对奇数索引元素执行冒泡排序,在偶数阶段,我们对偶数索引元素执行冒泡排序。

// 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)>
输出:
-9 2 10 34

请参阅关于奇偶排序/砖块排序的完整文章,以了解更多详细信息!