| 29 | } |
| 30 | |
| 31 | void sort(words::Words& words, size_t start, size_t end) |
| 32 | { |
| 33 | // start index must be less than end index for 2 or more elements |
| 34 | if (!(start < end)) |
| 35 | return; |
| 36 | |
| 37 | // Choose middle address to partition set |
| 38 | swap(words, start, (start + end) / 2); // Swap middle address with start |
| 39 | |
| 40 | // Check words against chosen word |
| 41 | size_t current {start}; |
| 42 | for (size_t i {start + 1}; i <= end; i++) |
| 43 | { |
| 44 | if (*words[i] < *words[start]) // Is word less than chosen word? |
| 45 | swap(words, ++current, i); // Yes, so swap to the left |
| 46 | } |
| 47 | |
| 48 | swap(words, start, current); // Swap chosen and last swapped words |
| 49 | |
| 50 | if (current > start) sort(words, start, current - 1); // Sort left subset if exists |
| 51 | if (end > current + 1) sort(words, current + 1, end); // Sort right subset if exists |
| 52 | } |
| 53 | |
| 54 | void words::show_words(const Words& words) |
| 55 | { |