› 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 Swapping With smallest Does Not Properly Work in Selection Sort
- This topic is empty.
-
AuthorPosts
-
May 28, 2026 at 3:10 pm #6693
Understanding Why Swapping With
smallestDoes Not Properly Work in Selection SortA beginner learning Selection Sort may naturally wonder:
If
smallestalready contains the smallest value, why can’t we simply swap using:L[i], smallest = smallest, L[i]
At first glance, this seems logical because:
smallestalready contains the correct smallest value- Python supports tuple swapping
- the smallest value appears to move into the correct position
This naturally raises an important question:
If
smallestcontains the value, why isL[smallest_j]still necessary?Let’s break it down carefully.
1. The Original List
Suppose the list is:
L = [7, 3, 9, 1]
During Selection Sort, the algorithm eventually finds:
smallest = 1 smallest_j = 3
Meaning:
smalleststores the value1smallest_jstores where that value exists in the list
2. A Crucial Difference
This is the key idea:
Expression Meaning smallestthe value 1smallest_jthe index 3L[smallest_j]the actual storage location inside the list This distinction is extremely important.
3. Why Real Swapping Requires Locations
A true swap means:
Move value A into location B AND Move value B into location ASo Python must modify:
- two actual positions inside the list
That is why Selection Sort correctly uses:
L[i], L[smallest_j] = L[smallest_j], L[i]
because both sides refer to real list locations.
4. What Goes Wrong With Using
smallestNow consider:
L[i], smallest = smallest, L[i]
Suppose:
i = 0
Python performs:
L[0] = 1 smallest = 7Result:
L = [1, 3, 9, 1]
Notice something important:
- the original
1still remains at index3 - the value
7disappeared from the list
The algorithm accidentally duplicated one value and lost another.
5. Why This Happens
The reason is subtle but fundamental.
The variable:
smallest
is only a temporary variable holding a value.
It is NOT the same thing as:
- the actual slot inside the list structure
Changing:
smallest
does not automatically change:
L[3]
because they are not the same storage location.
6. Visualizing the Difference
Conceptually:
Index: 0 1 2 3 Value: 7 3 9 1Now:
smallest = 1
can be imagined like:
- a sticky note containing the number
1
But:
L[3]
is:
- an actual box inside the list memory structure
Swapping requires moving contents between boxes.
Changing the sticky note does not modify the original box.
7. The Correct Selection Sort Swap
That is why Selection Sort correctly uses:
L[i], L[smallest_j] = L[smallest_j], L[i]
because:
- both sides refer to actual list positions
- both values remain preserved
- nothing gets duplicated or lost
8. The Core Insight
This example introduces an important programming principle:
- a variable holding a value is not always the same as a reference to mutable storage
- algorithms often depend on modifying real data structures, not temporary copies
Understanding this distinction is a major step toward understanding:
- memory
- references
- data structures
- algorithm implementation
So even this small Selection Sort detail introduces a foundational programming concept.
-
AuthorPosts
- You must be logged in to reply to this topic.

