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