› 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 › Understanding Why min_index = i Must Be Initialized Before the Inner Loop in Selection Sort (Q&A Learning Post)
- This topic is empty.
-
AuthorPosts
-
May 23, 2026 at 9:08 am #6629
A learner studying classical Selection Sort in Python understood that the algorithm should:
- search the remaining array
- find the smallest value
- swap only once at the end of each pass
The learner was using the following Selection Sort implementation:
def selectionsort(L): for i in range(len(L)): min_index = i for j in range(i + 1, len(L)): if L[j] < L[min_index]: min_index = j L[i], L[min_index] = L[min_index], L[i] return LDuring the discussion, an important question arose:
“Is it mandatory to place
min_index = ibetween the two loops? What happens if it is placed after the inner loop instead?”The following Q&A explains why the placement of
min_indexis extremely important.
Q1. Why does Selection Sort use
min_index?Selection Sort needs to remember:
“Which position currently contains the smallest value?”
That information is stored inside:
min_index
Q2. Why is
min_index = iplaced before the inner loop?Before searching begins, the algorithm must make an initial assumption:
“The current position contains the smallest value for now.”
That assumption is written as:
min_index = iOnly after making this assumption can the inner loop begin searching for smaller values.
Q3. What does the correct structure look like?
Correct Selection Sort structure:
for i in range(len(L)): min_index = i for j in range(i + 1, len(L)): if L[j] < L[min_index]: min_index = jThis order is essential.
Q4. What happens inside the inner loop?
The inner loop searches the remaining portion of the array:
for j in range(i + 1, len(L))Whenever a smaller value is found:
if L[j] < L[min_index]: min_index = jthe algorithm updates the location of the smallest element.
Q5. Why can’t
min_index = ibe placed after the inner loop?Suppose someone writes:
for i in range(len(L)): for j in range(i + 1, len(L)): if L[j] < L[min_index]: min_index = j min_index = iThis creates a major problem.
The variable:
min_indexis being used before it has been initialized.
Python therefore does not know what value it contains.
Q6. What error can this produce?
Python may produce:
UnboundLocalErrorbecause the variable is referenced before assignment.
Q7. Why does the inner loop need
min_indeximmediately?The inner loop needs a starting comparison reference.
Without:
min_index = iPython cannot answer:
“Compared to which element should smaller values be searched?”
Q8. What happens during an actual pass?
Suppose:
L = [64, 25, 12, 22, 11]At the beginning of the first pass:
i = 0The algorithm assumes:
64 is smallest for nowSo:
min_index = 0Then comparisons occur:
25 < 64 12 < 25 11 < 12Eventually:
min_index = 4Now the algorithm knows where the true minimum exists.
Q9. Why must
min_indexreset during every outer pass?The outer loop:
for i in range(len(L))starts a completely new search during each pass.
Therefore every pass requires a fresh assumption:
min_index = iIf it is not reset:
- previous pass values interfere
- incorrect comparisons occur
- sorting becomes unreliable
Q10. What is the overall mental model of classical Selection Sort?
Outer loop
“Choose the current position.”
min_index = i“Assume current position is smallest for now.”
Inner loop
“Search remaining array for something smaller.”
Final swap
“Place the true smallest value into the correct position.”
Q11. What is the biggest takeaway from this discussion?
The placement of:
min_index = iis not arbitrary.
It must occur:
- inside the outer loop
- before the inner loop begins searching
because the inner loop needs an initial reference point before comparisons can start.
-
AuthorPosts
- You must be logged in to reply to this topic.
