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

Function sort

Exercises/Modules/Chapter 11/Soln11_04/words.cpp:31–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29}
30
31void 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
54void words::show_words(const Words& words)
55{

Callers 2

sortMethod · 0.70
mainFunction · 0.70

Calls 1

swapFunction · 0.70

Tested by

no test coverage detected