这基本上是气泡排序的一种变体。该算法分为两个阶段-奇数阶段和偶数阶段。该算法一直运行到对数组元素进行排序,并在每次迭代中出现两个阶段-奇数阶段和偶数阶段。
在奇数阶段,我们对奇数索引元素执行冒泡排序,在偶数阶段,我们对偶数索引元素执行冒泡排序。
C++
// A C++ Program to implement Odd-Even / Brick Sort
#include
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 ot 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 Contribute 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 Contribute 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
输出 :
-9 2 10 34
我们使用下面的数组= {3,2,3,8,5,5,6,4,1}来演示上述算法