Skip to main content

Is the shelf in order? An algorithm to check sorting

The PEEMS team · Published November 10, 2026 · 4 min read

Pemi CS level 'Is the shelf in order?': an is_sorted Python function on the left, and a robot reading a sign with the numbers 3 4 5 6 on the right

To check whether a shelf is in order, compare each pair of neighbours. This post reads a Python is_sorted function through a Pemi CS level.

A librarian looks at a row of numbered books and wonders: is this row in order? She does not need to rearrange the whole shelf. She just runs a finger along it, comparing each pair of neighbouring books. That way of working is an algorithm.

The idea: compare neighbouring pairs

If the numbers should go up, then each book is not smaller than the one just before it. Finding one pair the wrong way round is enough to say, "not in order." If you compare every pair and find none reversed, the row is sorted.

The nice part is that we never need to look at the whole row at once, only one pair at a time.

The level: the is_sorted function

In the Pemi CS level "Is the shelf in order?" (for students already comfortable with Python), a robot reads a sign showing a list of numbers, then says "sorted" or "mixed". The key part is the function the student writes:

Pemi CS level about checking order: a Python is_sorted function and a robot beside a sign showing the numbers

def is_sorted(numbers):
    for i in range(1, len(numbers)):
        if numbers[i] < numbers[i - 1]:
            return False
    return True

Reading it line by line

  • def is_sorted(numbers): defines a function called is_sorted that receives a list of numbers.
  • for i in range(1, len(numbers)): walks through positions from the second to the last.
  • if numbers[i] < numbers[i - 1]: compares the current number with the one just before it. If it is smaller, the pair is reversed.
  • return False answers "not in order" right away and stops.
  • return True only runs if the loop finished without finding a reversed pair.

Three ideas students already know, a function, a loop and a condition, work together to solve a real problem.

Try a short list

For 3, 4, 5, 6: the pairs (3,4), (4,5), (5,6) all go up, so the answer is True. For 3, 5, 4, 6: at the pair (5,4) the order is reversed, so the function returns False immediately and looks no further.

An activity without a computer

Lay out 6 numbered cards. Two players: one shuffles or arranges them, the other may only compare two cards side by side to reach a verdict. Swap roles, then try a row with two equal numbers and see how the verdict changes.

Questions to talk about with your child

  • Why is it enough to compare neighbours instead of every possible pair?
  • What happens if the list is empty, or has only one number?
  • How would you change the code to check for a list that goes down?

Frequently asked questions

Is checking the order the same as sorting? No. Checking just answers "yes or no"; sorting changes the list so it is in order. Checking is usually the simpler step.

Why can the function stop early? Because one reversed pair is enough to know the answer. The return statement ends the function on the spot.

What grade is this for? The level is meant for older students who are comfortable with Python. Parents can try the card activity above with younger children, no computer needed.

Do I need a powerful computer? No. Pemi CS needs no special hardware: an ordinary computer that browses the web is enough. We recommend a computer or tablet; the phone interface is not optimised yet.

Want to try it together?

Pemi CS has more than 3,000 levels, including algorithm levels in Python. It is open now and international families are welcome to register their interest by email.

Keep reading: From block coding to Python: when is the right time to switch? · What is a function? Pemi defines its first one

The PEEMS team

Nov 10, 2026

Switching to Python too early frustrates children; too late and they get bored. This article lists signs of readiness, a path from picture blocks to text blocks to Python, and why ideas should come before syntax.

Nov 10, 2026

A function is a named group of commands you can call again and again. This post uses a Pemi CS level and a lemonade recipe to explain defining and calling a function.

Nov 10, 2026

A first Python snippet with for and if/else is really the text blocks your child already knows, written out as code. This post reads a Pemi CS level that counts red and green signs, line by line.