The odd-even sorting algorithm is also called brick sorting, which is a sorting technology similar to bubble sorting. This sorting technique is divided into two phases: odd phase and even phase, which are performed simultaneously in each iteration until all elements are sorted.
The odd phase of this programming technique is similar to bubble sort, but only sorts elements with odd indices.
Similarly, Even phaseonly sorts elements with even indexes.
To illustrate this concept more clearly, let us take an example:
Input: a[]={3,5,7,6,1,4,2} Output: 1 2 3 4 5 6 7
Even-odd sort, also known as brick sort, is a simple sorting technique , designed with parallel processing in mind. It uses comparison to sort its elements. Comparisons are made between ages and elements for all odd-even pairs. If any pair is in the wrong order, swap the order to make it correct. This process continues until the list is sorted. Since it was developed for parallel processing, it can process one value per processor and both processes perform swap-compare type operations simultaneously. This algorithm was originally proposed on such processors and proved to be efficient on such processors.
#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; } } } }
1234567
The above is the detailed content of C/C++ program for parity sorting (brick sorting). For more information, please follow other related articles on the PHP Chinese website!