2nd Written Assignment: How Gravity (Bead Sort) Ruined My Christmas
Current Session Stats
It's that time of the year. The Christmas cookies are hitting the shelves, the fairy lights are twinkling, and the Hellenic Open University (HOU) decides to send its own special gift: The 2nd Written Assignment.
Submission Deadline: January 4, 2026 In short: New Year's Eve with the compiler.
I opened the brief (12 pages, if you please) and my eyes landed on Exercise 2. Forget Bubble Sort and Quick Sort. This year we have... physics.
The Beast: Bead Sort (or Gravity Sort)
The assignment asks us to implement the Bead Sort algorithm. The idea is simple yet ingenious: You have numbers represented by "beads" on horizontal rows. You let them fall due to gravity and – miraculously – they end up sorted.
The Theory (Looks Easy on Paper)
Imagine an abacus.
- Each number is a row of beads (horizontal).
- You turn the abacus vertically.
- The beads fall to the lowest possible points.
- You count the beads from bottom to top and you have the numbers in ascending order.
In real life, gravity is free. In C, you have to write nested loops to simulate it.
The Implementation (Where We Wept)
The problem isn't the idea. The problem is how you translate "beads" and "rods" into C arrays.
The brief gives us a hint: We need an auxiliary array counts that tracks how many beads are on each vertical "rod".
- assignment_2
- src
- bead_sort.c
- wildfire_stats.c (Exercise 1 - easy)
- cities_matrix.c (Exercise 3 - the nightmare of 2D arrays)
- src
Essentially, the algorithm works in 4 steps:
- Reading: We take the numbers (with defensive programming, let's not forget, values 0-100).
- Counting (
counts): Instead of moving beads one by one, we count how many beads each column has. - Reconstruction (
B): We build the sorted arrayBby looking at thecountsarray. - Printing: The results on the screen.
Here is a taste of the "logic" of falling in pseudo-C (without giving away the solution, Professor!):
// Imaginary code simulating the pain
// N = 10 (rows), M = 50 (max value)
int counts[M]; // The "rods"
// Step 2: Filling the rods
for (each number in InputArray) {
// For each number, add a "bead" to the corresponding rods
// This is the part where I got confused 3 times with indexes.
// Remember: In C, arrays start at 0.
}The Iceberg of Defensive Programming
The University has an obsession (and between us, rightly so) with "defensive programming". In Exercise 1 with the wildfires, if the user enters a negative number of fires, we must tell them "Invalid value. Try again".
In Bead Sort, we must check if the numbers are between 0 and 100.
Enter element 4: -1
The value entered must be between 0 and 100. Please repeat input.I've written this do-while loop so many times I see it in my sleep. In an ideal world, I would create a Function and be done with it. But at the University, the brief was clear: "No use of functions". So, copy-paste and God help us.
Conclusion
The Bead Sort method is impressive to watch, but probably not the most efficient for sorting millions of numbers (unless you have special hardware). However, it taught me something important: How to think "laterally". To turn a physics problem into an array index problem.
Now, if you'll excuse me, I'm off to battle with Exercise 3: Two-dimensional arrays and city distances. I hope I don't get lost along the way.
The deadline is 04/01/2026. Don't leave it for New Year's Day. The system locks and does not forgive.