IMSc Outreach

Sorting Algorithms: Putting Things in Order

#

Presented by Roseline Jerry Aloor and Shuvadeep

Introduction

#

Sorting is one of the most fundamental tasks in computing: given a collection of items, arrange them in order. Your music app sorts songs alphabetically, your email client sorts messages by date, and databases sort records millions of times per second. Behind all of these sits a sorting algorithm — a precise, step-by-step procedure for rearranging items using one basic operation: comparing two items and deciding which comes first.

In this demonstration, a two-pan balance scale stands in for a computer's comparator. Each pan holds a cup containing a different amount of material, making the cups different weights. Placing two cups on the pans reveals which is heavier — exactly what a sorting algorithm does when it "compares" two numbers or names. By systematically comparing pairs of cups and rearranging them based on the result, the demonstrators physically walk through the steps of a real sorting algorithm, showing that what looks like magic on a screen is actually a repeatable, mechanical process anyone can follow.

Precautions

#

Place cups on the pans gently and wait for the scale to settle before reading which side is lower. Keep track of which cup was in which position before the comparison so that swaps are done correctly. Avoid overfilling cups so that spills do not change the weights mid-demonstration.

Materials Required

#
  • A two-pan balance scale
  • Several identical cups filled with different amounts of sand, rice, or water to create different weights
  • Labels or markers to give each cup an identifier (optional but helpful for larger demonstrations)

Science Behind It

#

Every classical sorting algorithm is built around a single primitive: the comparison. Given two elements — say, two numbers or two names — the comparison answers a yes-or-no question: "Does the first come before the second?" On a computer, this is almost instant and costs almost nothing. On a balance scale, the same operation is visible and physical: the heavier side drops, the lighter side rises, and the answer is immediate.

The simplest algorithm to demonstrate with cups is bubble sort. Starting from one end of the row, compare the first two cups on the balance. If they are in the wrong order (the heavier one should come later but currently sits first), swap them. Move one position along and repeat, comparing the second and third cups, then the third and fourth, and so on to the end of the row. After one complete pass, the heaviest cup has "bubbled" to its correct position at the end. Repeat the whole process for the remaining cups, and after at most n − 1 passes through a row of n cups, every cup is in order. Bubble sort is easy to demonstrate but slow for large collections: it can require up to n²/2 comparisons.

A more efficient approach is merge sort. Divide the cups into two halves, sort each half independently (using the same procedure recursively), and then merge the two sorted halves into one by repeatedly comparing the lightest remaining cup in each half and taking whichever is lighter. Merge sort always uses at most n log₂n comparisons, a dramatic improvement for large n — for 1,000 cups, bubble sort might need half a million comparisons while merge sort needs fewer than ten thousand.

Information theory establishes that no comparison-based sorting algorithm can do better than about n log₂n comparisons in the worst case. There are n! possible orderings of n items, and each comparison cuts the set of remaining possibilities at most in half; it therefore takes at least log₂(n!) ≈ n log₂n − n comparisons to distinguish all orderings. The balance-scale model makes this lower bound physically intuitive: each weighing is one binary question, and no sequence of binary questions can sort n cups in fewer than log₂(n!) weighings.

← Back to Demos