r/learnprogramming • • 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

16 comments sorted by

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.

2

u/otheyhigh 5h ago

This! Try to build the habit of visualizing what you are coding. By visualizing, I mean any form of problem solving (diagrams, mathematical solutions, pure algorithms, etc)

Once you build the conceptual steps to solve a problem, you can then convert it to code.

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?

•

u/Icy-Stretch-4643 35m ago

Actually I saw code of bubble sort, it infuriated me because I couldn't do it on my own. I'll now try another sort logic by myself, actually my assignment had question of merging two sorted arrays, I wanted to learn sorting before.

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.

•

u/Icy-Stretch-4643 31m 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.

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.

•

u/Icy-Stretch-4643 27m ago

Thanks for the valuable advice, feeling better when heard that it's normal. I do out loud technique often, I'll now surely add paper tracking too in habit.

1

u/atarivcs 5h ago

Are you confused about nested loops in general, or about their specific use in bubble sort?

•

u/Icy-Stretch-4643 24m ago

Our professor haven't taught nested loops separately, he just explained 2d arrays combo with nested loops, so I only know how to use nested loops with arrays medium level problems.

When I try sorting, I just struggle at starting, checking and updating conditions, it happens in single loops too, but escaping from paper dry run is the reason.

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.

•

u/Icy-Stretch-4643 18m ago

Noted, ill try and practice it, TY!!

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.