› Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 6: Python › MITx 6.100L Introduction to CS and Programming Using Python › Why minindex Does NOT Reset After the Inner Loop in Selection Sort
- This topic is empty.
-
AuthorPosts
-
May 25, 2026 at 7:04 am #6640
A beginner learning Selection Sort in Python may naturally think:
minindexchanges only temporarily inside the inner loop- once the inner loop ends,
minindexautomatically becomesiagain - the inner loop has its own separate “temporary” version of
minindex
This often creates another layer of confusion:
- Does a loop create a separate variable scope in Python?
- Why does
minindexstill contain the last updated value? - How does Selection Sort remember the smallest element found?
The following Q&A explains the deeper conceptual idea behind variable updates inside loops in Python.
Q1. Why do beginners think
minindexresets automatically?Consider:
for i in range(len(L)):minindex = i for j in range(i, len(L)): if L[j] < L[minindex]: minindex = jSince:
minindex = iappears before the inner loopminindex = jhappens inside the inner loop
many learners imagine:
The inner loop temporarily changes
minindex, but once the loop ends, it becomesiagain.But Python does not work that way.
Q2. Does a loop create a separate variable scope in Python?
No.
In Python:
ifblocks do not create separate variable scopesforloops do not create separate variable scopes- variables updated inside loops still exist outside the loop
So:
x = 5 for i in range(3): x = 10 print(x)Output:
10The value survives after the loop ends.
Q3. What actually happens to
minindexduring Selection Sort?Consider:
L = [7, 3, 9, 1]Outer loop starts:
i = 0 minindex = 0Now the inner loop begins searching for a smaller value.
Q4. What happens during the inner loop?
First iteration:
j = 0Comparison:
7 < 7False.
So:
minindex = 0still remains.
Second iteration:
j = 1Comparison:
3 < 7True.
So now:
minindex = 1
Third iteration:
j = 2Comparison:
9 < 3False.
So:
minindex = 1still remains.
Fourth iteration:
j = 3Comparison:
1 < 3True.
Now:
minindex = 3
Q5. What happens after the inner loop ends?
A beginner may expect:
minindex = 0again automatically.
But Python does NOT reset it.
The variable still contains:
minindex = 3because that was the last assigned value.
Q6. Why is this behavior important for Selection Sort?
Selection Sort needs to remember:
Which position contained the smallest value found during scanning.
That remembered position is stored in:
minindexAfter the loop ends, the algorithm uses that saved index for swapping.
Q7. What swap happens next?
The algorithm performs:
L[i], L[minindex] = L[minindex], L[i]which becomes:
L[0], L[3] = L[3], L[0]Result:
[1, 3, 9, 7]The algorithm successfully moves the smallest value to the correct position.
Q8. When does
minindexbecomeiagain?Only when the next outer-loop iteration starts.
Python executes again:
minindex = iFor example:
i = 1 minindex = 1So:
minindexis reinitialized once per outer-loop cycle- it is NOT automatically reset after the inner loop ends
Q9. What is a simple mental model for this?
Think of:
minindexas a sticky note.
Initially:
minindex = imeans:
“Currently I believe this position contains the minimum value.”
During scanning, whenever a smaller value is found, the sticky note is updated:
minindex = jThe sticky note keeps its latest value until something changes it again.
Q10. What is the biggest conceptual takeaway?
The major insight is:
Loops in Python do not create temporary versions of variables.
So:
- changes made inside the inner loop survive after the loop ends
minindexkeeps the last smallest index found- Selection Sort depends on preserving that value for swapping
Once this mental model becomes clear, the logic of Selection Sort becomes much easier to understand.
-
AuthorPosts
- You must be logged in to reply to this topic.
