Is the shelf in order? An algorithm to check sorting
The PEEMS team · Published November 10, 2026 · 4 min read

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:

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 calledis_sortedthat 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 Falseanswers "not in order" right away and stops.return Trueonly 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


