The Selection Sort

Now that we have stored all of the weights, we want to find the three highest of the weights. We'll use a sorting algorithm called a selection sort, which can be expressed in English as follows:

1.
Repeat the following steps three times, once through for each weight that we want to select.
2.
Search through the list (i.e., the Weight Vec), keeping track of the highest weight seen so far in the list and the index of that highest weight.
3.
When we get to the end of the list, store the highest weight we've found in another list (the “output list”, which in this case is the Vec SortedWeight).
4.
Finally, set the highest weight we've found in the original list to 0, so we won't select it as the highest value again on the next pass ...

Get C++: A Dialog Programming with the C++ Standard Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.