Parity sorting algorithm in C language
Odd-even ordering, or parity-changing, or brick sorting, is a relatively simple sort algorithm, originally invented for parallel computing with local interconnection. This is a sort of comparison that is similar to the bubble sort feature. In this algorithm, an exchange is exchanged by comparing the adjacent (odd-even) position pairs in the array, if the parity is in the wrong order (the first is greater than the second). The next step repeats the operation, but for all (even-odd) position number pairs. To go on so alternately.
The process of sorting a list of random numbers by using the parity sort method
Sequencing of processor arrays
In a parallel calculation sort, each processor corresponds to a value and has only a local interconnect with the left and right neighbors. All processors can compare and exchange operations with neighbors at the same time, alternating in odd-even, odd-odd order. The algorithm was first published by Habermann in 1972 and shows the efficiency of parallel processing.
The algorithm can be effectively extended to cases where each processor has multiple values. In the Baudet–stevenson parity merge partitioning algorithm, each processor sorts its own array of substrings at each step, and then performs a merge partition or a transposition merge with the neighbor.
Batcher odd and even merge sort
Batcher parity merging is a related but more efficient sort algorithm, using comparison-exchange and perfect-shuffle operation.
The batcher approach is efficient on parallel computing processors with a wide range of interconnections.
Algorithm
Example: to be ranked array [6 2 4 1 5 9]
For the first time, the odd sequence is compared with its neighbor pairs, such as 6 and 2, 4 and 1, and 5 and 9.
[6 2 4 1 5 9]
After the exchange becomes
[2 6 1 4 5 9]
Second comparison of even series, i.e. 6 and 1 ratio, 5 and 5 ratio
[2 6 1 4 5 9]
After the exchange becomes
[2 1 6 4 5 9]
The third is the odd sequence, and the 2,6,5 is chosen to compare them to their neighbor columns.
[2 1 6 4 5 9]
After Exchange
[1 2 4 6 5 9]
The four-trip couple series
[1 2 4 6 5 9]
One exchange
[1 2 4 5 6 9]
The following performance of the single processor algorithm, similar to bubble sort, simpler but not particularly efficient.
Completed on 2014.10.8 12:05//LANGUAGE:C99////Copyright (C) Codingwu (mail:oskernel@126.com)//Blog address: HTTP://WWW.CNBL ogs.com/archimedes/#include <stdio.h> #include <stdlib.h> #include <stdbool.h> void swap (int *a, int *
b) {int t;
t = *a;
*a = *b;
*b = t;
} void PrintArray (int a[], int count) {int i;
for (i = 0; i < count; i++) printf ("%d", a[i]);
printf ("\ n");
} void Odd_even_sort (int a[], int size) {bool sorted = false;
while (!sorted) {sorted = true;
for (int i = 1; i < size-1 i = 2) {if (A[i] > a[i + 1]) {swap (&a[i), &a[i + 1]);
sorted = false; for (int i = 0; i < size-1 i = 2) {if (A[i] > a[i + 1]) {swap (&a[i), &am
P;a[i+1]);
sorted = false;
int main (void) {int a[] = {3, 5, 1, 6, 9, 7, 8, 0, 11};
int n = sizeof (a)/sizeof (*A);
Odd_even_sort (A, n);
PrintArray (A, n);
return 0;
}
Thank you for reading, I hope to help you, thank you for your support for this site!