algorithmsortingflowchart

What is the problem with this flowchart for outputing the two smallest numbers in a list?


I am taking an online class and I want to know where I went wrong in a flowchart assignment. I received a score of 66%, which indicates that I may have made a mistake. I believe my answer was correct, although I may not have used the most efficient approach.

The Prompt :

Now, draw the flowchart for an algorithm (described below) that will find the two smallest values in the collection. You can assume that the collection has at least two values and that all values are distinct.

The algorithm works by making one pass through the collection and keeping track of the two smallest values seen so far:

  • Initialize the two smallest values so far to be the first two elements of the list

  • For each remaining value in the list: if the value is smaller than either of the two smallest so far, replace the larger of the two

There are a few ways to represent the two smallest values seen so far, but for this assignment, please use the following approach:

  • Set “min1” to the first value of the collection and “min2” to the second

  • Next, look at each remaining element in the collection:

    • If the element is less than “min1” and “min1” is less than “min2”,

      Then set “min2” to that element’s value

    • Otherwise, if the element is less than “min1” but “min1” is greater than “min2”,

      Then set “min1” to that element’s value

    • Otherwise, if the element is greater than “min1” and is less than “min2”,

      Then set “min2” to that element’s value

  • After inspecting all elements, output “min1” and “min2,” which hold the two smallest values

I made this flowchart:

The Flowchart

What could be the reason(s) I didn't get the maximum score?


Solution

  • Some of the issues:

    There are several ways to make a flowchart, and what is considered correct could depend on what your course material has. For instance, it is not uncommon to actually ask a question in a decision-box (diamond). So like "Is item<min1 and min1<min2?", and the outgoing arrows could have labels "Yes" and "No" instead of "True" and "False".

    Another remark concerns the logic presented in the assignment, so it cannot be considered a mistake in your flowchart:

    If the first two tests are false, then it follows that item>min1. Having this condition tested in the bottom case is overkill: it is guaranteed to be true. So that last test could be expressed as "Is item<min2?" only.