› 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 Insertion Sort Uses j = j – 1 (Q&A Learning Post)
- This topic is empty.
-
AuthorPosts
-
June 13, 2026 at 4:14 am #6886
While studying Insertion Sort, a learner encountered the following line:
j = j - 1and wondered:
Why do we keep subtracting 1 from
j? What exactly isjdoing?The following Q&A explains the idea step by step.
Q1. What does
jrepresent in Insertion Sort?Consider the standard algorithm:
for i in range(1, len(L)): key = L[i] j = i - 1Here:
keyis the element we want to insert into the sorted portion.jstarts at the element immediately to the left ofkey.
For example:
2 5 4 6 ^ keySince:
i = 2we get:
j = i - 1 j = 1So:
2 5 4 6 ^ j
Q2. Why does Insertion Sort look to the left?
The portion before
keyis already sorted.For example:
2 5 | 4 6The algorithm wants to insert
4into:2 5Therefore it must examine elements to the left of
4.
Q3. What happens during the first comparison?
The loop checks:
L[j] > keywhich becomes:
5 > 4This is true.
Therefore:
L[j + 1] = L[j]moves 5 one position right.
Result:
2 5 5 6
Q4. Why is
j = j - 1executed?After moving 5, we still do not know whether 4 belongs before or after 2.
Therefore we move one step left:
j = j - 1which changes:
j = 1to:
j = 0Now:
2 5 5 6 ^ jThe algorithm is now examining the next element to the left.
Q5. What would happen if we did not decrease
j?Suppose we removed:
j = j - 1Then
jwould stay at the same position forever.The loop would repeatedly examine:
5again and again.
This could lead to an infinite loop.
Q6. What does moving
jleft accomplish?It allows the algorithm to search for the correct insertion position.
Think of
jas a person walking left through the sorted section:2 5 4 6 ^ jAfter one step:
2 5 5 6 ^ jThe algorithm keeps walking left until it finds where the key belongs.
Q7. What happens after
jreaches 0?The algorithm checks:
2 > 4This is false.
Therefore the loop stops.
At this moment:
2 5 5 6 ^ j
Q8. Why does the algorithm insert at
L[j + 1]?After the loop stops:
jpoints to the last element that is less than or equal to the key.
In our example:
2 5 5 6 ^ jSince 4 belongs immediately after 2:
L[j + 1] = keybecomes:
2 4 5 6
Q9. Can
jbecome negative?Yes.
Consider:
5 2 4 6The key is:
2The algorithm shifts 5 right:
5 5 4 6Then:
j = j - 1changes:
j = 0to:
j = -1This tells us:
“We have moved past the beginning of the list.”
Q10. Why is
j >= 0part of the while condition?The loop usually looks like:
while j >= 0 and L[j] > key:This prevents Python from continuing to move left beyond the beginning of the list.
Once:
j = -1the loop stops.
Q11. What is the main intuition behind
j = j - 1?Think of
jas a pointer searching leftward through the sorted portion.2 5 4 6 ^ jAfter shifting 5:
2 5 5 6 ^ jThe statement:
j = j - 1simply means:
“Move one position further left and continue searching for the correct insertion point.”
Q12. What is the final takeaway?
The statement:
j = j - 1is essential because it allows Insertion Sort to move left through the already sorted portion of the list.
Without it:
- the algorithm could not examine earlier elements,
- the correct insertion position could not be found,
- and the loop might never terminate.
In short:
j = j - 1tells Insertion Sort to keep walking left through the sorted section until the proper place for the key is found. -
AuthorPosts
- You must be logged in to reply to this topic.
