r/learnprogramming • • 1d ago

Loops and sorting

Im 1st sem student, currently learning programming basics course.

I find myself confused and lost at 1) nested loops, their logics and algorithmic conditions. 2) in sorting.

Like I was trying to learn sorting algorithm bubble one, when I tried to understand the condition n-1-i I got completely blank, idk if it's normal or not, but I'm really weak here.

Anyone faces the same and got some solution, do share, it will help a lot!!

0 Upvotes

21 comments sorted by

View all comments

2

u/Tokyo_STEM_school 1d ago

Totally normal — nested loops and bubble sort clicked late for a lot of people, including me. The way I finally got it was to stop thinking of it as code and think of it as a job with two parts:

The outer loop just picks which position you're working on, one at a time. The inner loop does the actual comparing for that position. So 'nested' really just means 'for each item, do this whole other job.'

For bubble sort, the n-1-i part is the bit that confused me too. Each full pass pushes the biggest number to the end, and it stays there. So after one pass you never need to check the last spot again, after two passes you skip the last two, and so on. The i is just counting how many passes you've already finished — that's why the part you still need to check keeps shrinking.

What helped me most was tracing one tiny list by hand, like [5, 3, 8, 4], and writing down the list after every single swap. It feels slow, but after one full pass the pattern suddenly makes sense. You're not weak at this — you're at the exact point where it starts to click.

1

u/Icy-Stretch-4643 23h ago

Tysm !!, I'll now see nested loops with this sense, and yes I'll surely practice paper dry runs too, they'll help me a lot in my paper code finals too.