Module 14 · The Algorithms You Will Use Weekly: accumulate to nth_element
When to Use an Algorithm, and When a Plain Loop Is Clearer
Free with an accountReading
Kenji needs the largest of a million readings, so he sorts all of them and reads the last one. Bob needs a median and writes a 12-line loop for it. His answer is wrong whenever the count is even. Both programs run. Amara asks each of them one question: what did the job need? This lesson is her answer, with a chart, a flowchart, three measurements and the cases where the loop wins.
Checking what this lesson needs for you…
What is inside When to Use an Algorithm, and When a Plain Loop Is Clearer
- Five questions before you write a loop
- What the Core Guidelines say
- The comparison chart
- The decision flowchart
- Measured: an algorithm against the loop you would write
- Measured: sort, partial_sort and nth_element for the same question
- When the loop is clearer
- The honest default
- When not to use them