SECTION 07 / 15

Keep the top three: shift safely and handle ties

2 min read
On this page 2 headings

Keeping one maximum needs one remembered value. Keeping three needs three ordered places: First ≥ Second ≥ Third. When a new maximum arrives, save the old values before overwriting them.

Shift Third from Second, then Second from First, before storing a new First.
Shift Third from Second, then Second from First, before storing a new First.
The third distinct Mathematics mark is89; the third student score counting ties is93.
The third distinct Mathematics mark is89; the third student score counting ties is93.

The four update cases

New value vWhat to change
v > FirstThird = Second; Second = First; First = v
First > v > SecondThird = Second; Second = v
Second > v > ThirdThird = v
v below ThirdNo update

Use mutually exclusive cases, such as if / else if, or explicit disjoint conditions. Independent tests that reuse a just-updated value can accidentally process one card twice. The order of assignments matters: writing First = v too early loses the old First.

REAL COURSE DATA · FOLLOW THE PROCEDURE

Keep the top three distinct marks

Read the current record, check the explanation, and watch the variables. Use Back to revisit any step.

Ready

CURRENT RECORD

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Start at the first step. Watch the current row and the values we remember.

Two meanings of top three

FieldTop three distinct mark levelsTop three student scores, counting ties
Mathematics97, 93, 8997, 97, 93
Physics92, 91, 8892, 92, 92
Chemistry97, 93, 9297, 93, 92

The strict inequalities in the lecture’s TopThreeMarks code keep distinct score levels: repeated 97 does not create another mark level. A ranking of three students instead counts two different students with 97 separately. The walkthrough uses distinct levels and lets you change the field parameter.

Procedure TopThreeMarks(Rows, Field)
    First = -1; Second = -1; Third = -1
    for each row X in Rows {
        v = X[Field]
        if (v > First) {
            Third = Second
            Second = First
            First = v
        } else if (First > v and v > Second) {
            Third = Second
            Second = v
        } else if (Second > v and v > Third) {
            Third = v
        }
    }
    if (Third == -1) { report "Fewer than 3 distinct values" }
    else { return Third }
End Procedure

The source initializes marks to 0 because these marks are nonnegative. Here −1 marks an empty slot explicitly, so a valid zero is not confused with a filled slot. This bound is for marks, not arbitrary data. If fewer than three different values exist, do not return a placeholder as a genuine cutoff.

Try it yourself

Current values are 90,80,70. What happens when 95 arrives?

Show explained answer

Third becomes 80, Second 90, First 95. Save lower slots first.

Try it yourself

Why not write First=95 before Second=First?

Show explained answer

Second would receive 95 instead of the old First 90, losing information.

Try it yourself

Do repeated 97 s create another distinct score level?

Show explained answer

No. They are equal to the existing level.

Try it yourself

What is the third distinct Mathematics level here?

Show explained answer

89. The third student score counting ties is 93.

Try it yourself

What should happen with only two distinct values?

Show explained answer

Report that no third distinct cutoff exists; do not treat an empty placeholder as a mark.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑