SECTION 11 / 24

Check ascending marks with adjacent comparisons

1 min read
Non-decreasing order allows equal neighbours. The pair 63 then 62 is enough to disprove it.
Non-decreasing order allows equal neighbours. The pair 63 then 62 is enough to disprove it.
REAL COURSE DATA · BUILD AND UNPACK LISTS

Check adjacent marks for ascending order

Follow one occurrence at a time. Choose another input to test empty lists, repeated IDs and different parameters.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next change, then step through the list. The original course records remain unchanged.

L = the list of Mathematics marks
Ascending = true
while length(L) >= 2 AND Ascending:
    if first(L) > first(rest(L)):
        Ascending = false
    else:
        L = rest(L)
return Ascending

Ascending here means non-decreasing: every mark is less than or equal to the next. Equality is allowed. Compare a neighbour pair, then advance by ONE position. Advancing by two would skip the pair across the removed boundary. One decrease is enough to stop with false.

The original dataset order starts 68,62,57,… and is not sorted. The ascending option sorts all 30 Maths values for a successful comparison trace. A lecture-style short sequence deliberately contains 63,62 near the end to illustrate early failure; it is labelled as a teaching sequence rather than the original order.

An empty or singleton list has no violating adjacent pair, so this algorithm returns true. A full successful scan over n elements makes n−1 neighbour comparisons for n ≥ 1. We count these comparisons separately from the work done by helper functions such as length or rest.

Try it yourself

Is [42,44,44,52] ascending under this rule?

Show explained answer

Yes. Equal neighbouring values are allowed; this is non-decreasing order.

Try it yourself

Why not compare every mark with every other mark?

Show explained answer

For this order test, adjacent comparisons suffice: if each value is ≤ the next, the entire sequence is non-decreasing.

Try it yourself

Does finding three correct neighbour pairs prove the rest of a long list is sorted?

Show explained answer

No. Continue until all neighbours are checked or a decrease is found.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑