# C/C++ Program for the Odd-Even Sort (Brick Sort)?

CC++Server Side ProgrammingProgramming

The odd-even sword also known as the brick sort is a similar sorting technique, like bubble sort. This sorting technique is subdivided into 2 phases odd phase and even phase, Both these phases simultaneously at every iteration until all the elements get sorted.

The Odd phase of this programming technique works as a bubble sort but only on the elements that have an odd index.

Similarly, the even phase works only on the elements that have an even index.

For making this concept more clear let's take an example :

Input: a[]={3,5,7,6,1,4,2}
Output: 1 2 3 4 5 6 7

## Explanation

The even-odd sort also known as brick sort, is a simple sorting technique that was designed keeping in mind parallel processing. It uses comparisons to sort its elements. the comparing takes place on all odd/even pair with their ages and elements. if any pair is in a wrong order then and the Order is switched to make it correct. this process goes on until the list gets sorted. as it was developed for parallel processes, it can awesome one value per processor and both the processes work concurrently on exchange-compare type operation. This algorithm was originally presented, and shown to be efficient on such processors

## Example

#include <stdio.h>
#include <math.h>
#define MAX 7
void swap(int *,int *);
void oddeven_sort(int *);
int main() {
int a[]={3,5,7,6,1,4,2}, i;
oddeven_sort(a);
for (i = 0;i < MAX;i++) {
printf(" %d", a[i]);
}
}
void swap(int * x, int * y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void oddeven_sort(int * x) {
int sort = 0, i;
while (!sort) {
sort = 1;
for (i = 1;i < MAX;i += 2) {
if (x[i] > x[i+1]) {
swap(&x[i], &x[i+1]);
sort = 0;
}
}
for (i = 0;i < MAX - 1;i += 2) {
if (x[i] > x[i + 1]) {
swap(&x[i], &x[i + 1]);
sort = 0;
}
}
}
}

## Output

1234567
Published on 19-Aug-2019 07:26:35