MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / sort

Function sort

Exercises/Modules/Chapter 11/Soln11_06/words.cpp:39–60  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37}
38
39void 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
62size_t max_word_length(const words::Words& words)
63{

Callers 2

mainFunction · 0.70
sortMethod · 0.70

Calls 1

swapFunction · 0.70

Tested by

no test coverage detected