› 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 an Initial Python Sorting Attempt Was Actually Bubble Sort (Q&A Learning Post)
- This topic is empty.
-
AuthorPosts
-
May 17, 2026 at 7:25 am #6609
Understanding Why an Initial Python Sorting Attempt Was Actually Bubble Sort (Q&A Learning Post)
A learner was experimenting with sorting algorithms in Python and initially wrote the following function while attempting to implement Selection Sort:
def selectionsort(L): for j in range(len(L)): for i in range(1, len(L)): if L(i) > L(i + 1): L(i), L(i + 1) = L(i + 1), L(i)During the discussion, it became clear that although the function was named
selectionsort, the underlying logic was actually much closer to Bubble Sort.The following Q&A explains the reasoning in detail.
Q1. Why was the learner’s original code identified as Bubble Sort?
The key reason was this comparison:
if L[i] > L[i + 1]:This compares adjacent elements:
L[i]L[i + 1]
Repeatedly comparing neighboring elements is the defining behavior of Bubble Sort.
Q2. Why is adjacent comparison important in Bubble Sort?
Bubble Sort works by:
- Comparing neighboring elements
- Swapping them if they are out of order
- Repeating multiple passes through the list
Large values gradually “bubble” toward the end.
Q3. What was syntactically incorrect in the learner’s original code?
The learner wrote:
L(i)In Python:
()is used for function calls[]is used for indexing lists
Correct syntax:
L[i]
Q4. What would a corrected Bubble Sort version look like?
def bubblesort(L): for j in range(len(L)): for i in range(len(L) - 1): if L[i] > L[i + 1]: L[i], L[i + 1] = L[i + 1], L[i]
Q5. Why can
L[i + 1]cause an index error?Suppose:
i = len(L) - 1Then:
i + 1goes beyond the list boundary.
That is why Bubble Sort usually uses:
range(len(L) - 1)
Q6. What is the main idea behind Selection Sort?
Selection Sort:
- Chooses a position
- Searches the remaining array
- Finds the smallest value
- Places it into the correct position
Unlike Bubble Sort, it does not rely on neighbor-only comparisons.
Q7. What does a standard Selection Sort look like?
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]
Q8. What is the role of
min_index?min_index = iThis assumes:
“The current position contains the minimum value for now.”
The algorithm then searches for a smaller value.
Q9. Why did MIT’s version not use
min_index?MIT often teaches a simplified Selection Sort implementation:
def selection_sort(L): for i in range(len(L)): for j in range(i, len(L)): if L[j] < L[i]: L[i], L[j] = L[j], L[i]This version swaps immediately whenever a smaller value is found.
Q10. Why is MIT’s version still Selection Sort?
Because the comparison strategy is:
L[j] < L[i]This compares:
- one fixed position
i - against all later positions
j
That is the core Selection Sort idea:
“Find the best candidate for this position.”
Q11. Why did MIT’s version appear similar to Bubble Sort?
The learner noticed that MIT’s version performs many swaps:
L[i], L[j] = L[j], L[i]This visually resembles Bubble Sort.
However, the important distinction is:
- Bubble Sort compares neighbors
- Selection Sort compares a position against the remaining array
Q12. What is the main conceptual difference between the two algorithms?
Bubble Sort mindset
“Fix neighboring inversions repeatedly.”
Selection Sort mindset
“Select the smallest remaining element.”
Q13. Which algorithm usually performs fewer swaps?
Selection Sort generally performs fewer swaps because:
- it often waits until the smallest value is found
- then performs one swap
Bubble Sort swaps repeatedly throughout the process.
Q14. What is the time complexity of both algorithms?
Both commonly have time complexity:
O(n²)because nested loops are used.
Q15. Why are these algorithms still important to learn?
Even though Python already provides:
sorted(L)these algorithms help learners understand:
- loops
- indexing
- comparisons
- swapping
- algorithmic thinking
- time complexity
- foundational computer science concepts
-
AuthorPosts
- You must be logged in to reply to this topic.
