|
|
|
Sample input to the odd-even sort algorithm:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 |
Phase 1's even phase will compare the following pairs:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: |
Result after Phase 1's even phase:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 |
Phase 1's odd phase will compare the following pairs:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: |
Result after Phase 1's odd phase:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 |
Phase 2's even phase will compare the following pairs:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: |
Result after Phase 2's even phase:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 |
Phase 2's odd phase will compare the following pairs:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 odd: |
Result after Phase 2's odd phase:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 odd: 1 2 5 3 8 4 9 6 7 |
Phase 3's even phase will compare the following pairs:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 odd: 1 2 5 3 8 4 9 6 7 Ph3: even: |
Result after Phase 3's even phase:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 odd: 1 2 5 3 8 4 9 6 7 Ph3: even: 1 2 3 5 4 8 6 9 7 |
Phase 3's odd phase will compare the following pairs:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 odd: 1 2 5 3 8 4 9 6 7 Ph3: even: 1 2 3 5 4 8 6 9 7 odd: |
Result after Phase 3's odd phase:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 odd: 1 2 5 3 8 4 9 6 7 Ph3: even: 1 2 3 5 4 8 6 9 7 odd: 1 2 3 4 5 6 8 7 9 |
Phase 4's even phase will compare the following pairs:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 odd: 1 2 5 3 8 4 9 6 7 Ph3: even: 1 2 3 5 4 8 6 9 7 odd: 1 2 3 4 5 6 8 7 9 Ph4: even: |
Result after Phase 4's even phase:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 odd: 1 2 5 3 8 4 9 6 7 Ph3: even: 1 2 3 5 4 8 6 9 7 odd: 1 2 3 4 5 6 8 7 9 Ph4: even: 1 2 3 4 5 6 7 8 9 |
Phase 4's odd phase will compare the following pairs:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 odd: 1 2 5 3 8 4 9 6 7 Ph3: even: 1 2 3 5 4 8 6 9 7 odd: 1 2 3 4 5 6 8 7 9 Ph4: even: 1 2 3 4 5 6 7 8 9 odd: |
Result after Phase 4's odd phase:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 5 9 1 4 2 3 6 7 Ph1: even: 5 8 1 9 2 4 3 6 7 odd: 5 1 8 2 9 3 4 6 7 Ph2: even: 1 5 2 8 3 9 4 6 7 odd: 1 2 5 3 8 4 9 6 7 Ph3: even: 1 2 3 5 4 8 6 9 7 odd: 1 2 3 4 5 6 8 7 9 Ph4: even: 1 2 3 4 5 6 7 8 9 odd: 1 2 3 4 5 6 7 8 9 |
void oddEvenSort(int a[], int n) { int isSorted = 0; // Initially array is unsorted int i; while ( isSorted == 0 ) { isSorted = 1; // Assume the array is sorted... // Even phase for (i = 0; i < n-1; i = i + 2) { if (a[i] > a[i+1]) { SWAP(a[i], a[i+1]); isSorted = 0; // Detects unsorted } } // Odd phase for (i = 1; i < n-1; i = i + 2) { if (a[i] > a[i+1]) { SWAP(a[i], a[i+1]); isSorted = 0; // Detects unsorted } } } } |
DEMO: /home/cs355001/demo/CUDA/7-sort/cpu-odd-even.c