› 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 the Logical Mistake in a Classical Selection Sort Attempt (Q&A Learning Post)
- This topic is empty.
-
AuthorPosts
-
May 24, 2026 at 3:38 am #6636
A learner studying classical Selection Sort in Python understood several important ideas correctly:
- the algorithm should search the remaining array
- the smallest value should be identified
- swapping should happen only once at the end of each pass
The learner eventually arrived at the following conceptual approach:
def selectionsort(L): for i in range(len(L)): minindex = i for j in range(i, len(L)): if L[i] > L[j]: minindex = j L[i], L[j] = L[j], L[i] print(L)Assuming all syntax issues were corrected, an important logical discussion emerged:
- Was the algorithm conceptually correct?
- Why did the sorting logic still contain a mistake?
- Why was
minindexnot being used properly?
The following Q&A explains the deeper logical issue.
Q1. What did the learner understand correctly?
The learner correctly understood several important Selection Sort concepts:
- one position should remain fixed during each pass
- the remaining array should be searched
- the smallest value should be tracked
- swapping should happen only once at the end
These are all core ideas behind classical Selection Sort.
Q2. What was the main logical mistake?
The learner correctly updated:
minindex = jwhen a smaller value was found.
However, during the final swap, the algorithm used:
L[i], L[j] = L[j], L[i]instead of:
L[i], L[minindex] = L[minindex], L[i]This was the core logical issue.
Q3. Why was using
jincorrect?At the end of the inner loop:
jsimply contains:
“the last value reached by the loop.”
It does not necessarily contain:
“the position of the smallest element.”
That information was stored separately inside:
minindex
Q4. What was the learner trying to achieve conceptually?
The learner’s idea was:
“Search the remaining array completely, remember the smallest element, and swap once at the end.”
That is exactly the correct classical Selection Sort strategy.
Q5. Why is
minindexnecessary?Suppose the list is:
[64, 25, 12, 22, 11]During the first pass:
- 64 is initially smallest
- then 25 becomes smallest
- then 12 becomes smallest
- finally 11 becomes smallest
The algorithm therefore needs a variable that continuously remembers:
“Where is the smallest value currently located?”
That variable is:
minindex
Q6. What happens to
jduring the loop?The variable:
jchanges continuously as the inner loop runs.
Eventually it simply reaches the last index of the range.
Therefore:
jrepresents the current scanning positionminindexrepresents the smallest value found so far
These two variables serve different purposes.
Q7. What should the correct final swap look like?
Correct classical Selection Sort swap:
L[i], L[minindex] = L[minindex], L[i]This swaps:
- the current position
- with the true smallest element found during the pass
Q8. What does the correct classical Selection Sort look like?
def selectionsort(L): for i in range(len(L)): minindex = i for j in range(i + 1, len(L)): if L[j] < L[minindex]: minindex = j L[i], L[minindex] = L[minindex], L[i] return L
Q9. Why is the comparison written this way?
Correct comparison:
if L[j] < L[minindex]This means:
- compare the current scanning value
- against the smallest value found so far
The learner originally compared:
L[i] > L[j]which keeps comparing against the original starting value instead of the continuously updated minimum.
Q10. Why is this an important conceptual insight?
This discussion highlights an important algorithm-design idea:
“Tracking variables must actually be used during later decisions.”
The learner successfully created:
- a scanning variable (
j) - a tracking variable (
minindex)
But the final swap still relied on the scanning variable instead of the tracking variable.
Q11. What is the overall mental model of classical Selection Sort?
Outer loop
“Choose current position.”
minindex = i“Assume current position is smallest for now.”
Inner loop
“Search remaining array for a smaller value.”
minindex = j“Remember where the new minimum exists.”
Final swap
“Place the true minimum into the correct position.”
Q12. What is the biggest takeaway from this discussion?
The learner’s logic was already very close to correct classical Selection Sort.
The major conceptual insight was:
“Once a tracking variable like
minindexis created, the final swap must use that tracked position — not merely the last loop variable.” -
AuthorPosts
- You must be logged in to reply to this topic.
