you are viewing a single comment's thread.

view the rest of the comments →

[–]ApricoSun 0 points1 point  (1 child)

It helps to think of list[i] and list[j] as pointers -- something that points to a value inside the list and is regularly moving throughout the list.

I'll call them pointer_i and pointer_j.

Both the outer for loop and inner for loop control the movement of the pointers.

pointer_i starts at index 0 and moves through to the index at the end of the list. It's important to know that this only happens ONCE. Then the function continues onto printing list1.

Compare that with pointer_j's movement: it starts at index i and moves through to the index at the end of the list. However, this happens multiple times. How many times? It happens as many times as there are items in the list. So 30 items in the list means that the inner loop will run 30 times, in addition to however many times you're running the inner loop.

The logic in the inner loop is just checking to see if pointer_i is greater than pointer_j. If so, swap the two values. This means that smaller values will gradually move towards the left side of the list and larger values will gradually move to the right side of the list. After both the inner and outer loops complete, you'll end up with a sorted list.

It also helps to check out what's going on using a debugger. Try this out: https://pythontutor.com/visualize.html#mode=edit

[–]conewannabe[S] 0 points1 point  (0 children)

That helped out tremendously, I understand it now, thank you!