r/learnprogramming • u/Icy-Stretch-4643 • 8h 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
7
u/fasta_guy88 8h ago
Try doing a small sort by hand. 4 numbers. Write down on a sheet of paper what happens at each step. Computer programs are just fast ways of doing something you can do with pencil and paper. Try the slow way.
Alternatively, learn to use a debugger and step through the program line by line as it sorts 4 numbers.
1
u/Dazzling_Music_2411 8h ago
Take a break, then go back and figure it out when you're rested. Repeat as many times as necessary.
So, have you mastered bubblesort yet?
1
u/Tokyo_STEM_school 7h 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/captainAwesomePants 6h ago
This is a SUPER normal step to get stuck on as a first semester student. Loops with mathy expressions are not a natural way to think through instructions. It gets a lot easier with practice, I promise. You've gotta practice, but eventually it starts to make a lot more sense. It can help to walk through them with paper and pencil, or to explain how they work step by step out loud to someone else, or to try and restate the logic in English, and ultimately just writing some small programs every day will help most of all.
1
u/atarivcs 5h ago
Are you confused about nested loops in general, or about their specific use in bubble sort?
1
u/iOSCaleb 3h ago
> lost at 1) nested loops
This part is pretty easy. Think about how counting in any given base works. Counting in base 10 for example, the ones digit is incremented over and over until it reaches the largest digit, 9, and the next step causes the tens digit to increment and resets the ones digit, and the process repeats. When the tens digit reaches 9, the next time that it is incremented it goes back to 0 and the hundreds digit is increased.
Nested loops work much the same way. It’d be a good exercise to write code where three nested loops, one for each digit, count from 0 to 999.
Of course, loops can do more than just count upward. But thinking about how counting works will help you get used to the way they interact.
2
u/gomorycut 4h ago
leave the n-i-1 alone.
bubble i and i+1
repeat it n times.
Write code that makes sense to you.
1
u/BranchLatter4294 8h ago
Practice. Use print statements to watch what happens inside the loops.
1
u/Recycled5000 2h ago
Ok, so nested loops are good for a number of problems.
First, they are akin to multiplication. Things in an inner loop occur over and over as the outer loop executes.
Second, they are good for two dimensional problems like printing a chess or checkerboard. Also, printing a star pyramid.
Though sorting works on a 1 dimensional data structure, a sorting algorithm must compare elements of the same array with each other. This results in a somewhat 2D structure to the approach.
The limits chosen are optimization. Naively, we might simply loop over all elements (outer loop) and all elements (inner loop). But we know that much of that 2D space will be non productive. By applying some reasoning we can eliminate a fair portion of that 2D space to make this algorithm more efficient.