Last active
December 26, 2025 07:11
-
-
Save yamamaya/3e7691b7f129fa0c47e1fe9c3851d2f4 to your computer and use it in GitHub Desktop.
Sorting network for 9 elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define SWAP(a, b) if (arr[a] > arr[b]) { uint8_t tmp = arr[a]; arr[a] = arr[b]; arr[b] = tmp; } | |
| // Sorting network for 9 elements (25 steps) | |
| void SortingNetwork9(uint8_t *arr) { | |
| SWAP(0, 1); | |
| SWAP(3, 4); | |
| SWAP(6, 7); | |
| SWAP(1, 2); | |
| SWAP(4, 5); | |
| SWAP(7, 8); | |
| SWAP(0, 1); | |
| SWAP(3, 4); | |
| SWAP(6, 7); | |
| SWAP(0, 3); | |
| SWAP(3, 6); | |
| SWAP(0, 3); | |
| SWAP(1, 4); | |
| SWAP(4, 7); | |
| SWAP(1, 4); | |
| SWAP(2, 5); | |
| SWAP(5, 8); | |
| SWAP(2, 5); | |
| SWAP(1, 3); | |
| SWAP(5, 7); | |
| SWAP(2, 6); | |
| SWAP(4, 6); | |
| SWAP(2, 4); | |
| SWAP(2, 3); | |
| SWAP(5, 6); | |
| } | |
| // Sorting network for 7 elements (16 steps) | |
| void SortingNetwork7(uint8_t *arr) { | |
| SWAP(1, 2); | |
| SWAP(3, 4); | |
| SWAP(5, 6); | |
| SWAP(0, 2); | |
| SWAP(3, 5); | |
| SWAP(4, 6); | |
| SWAP(0, 1); | |
| SWAP(4, 5); | |
| SWAP(2, 6); | |
| SWAP(0, 4); | |
| SWAP(1, 5); | |
| SWAP(0, 3); | |
| SWAP(2, 5); | |
| SWAP(1, 3); | |
| SWAP(2, 4); | |
| SWAP(2, 3); | |
| } | |
| // Sorting network for 5 elements (9 steps) | |
| void SortingNetwork5(uint8_t *arr) { | |
| SWAP(0, 1); | |
| SWAP(3, 4); | |
| SWAP(2, 4); | |
| SWAP(2, 3); | |
| SWAP(0, 3); | |
| SWAP(0, 2); | |
| SWAP(1, 4); | |
| SWAP(1, 3); | |
| SWAP(1, 2); | |
| } | |
| // Sorting network for 3 elements (3 steps) | |
| void SortingNetwork3(uint8_t *arr) { | |
| SWAP(0, 1); | |
| SWAP(1, 2); | |
| SWAP(0, 1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment