Python: InsertionSort algorithm

Python

Video is ready, Click Here to View ×


Insertion Sort algorithm explained in animated demo, with example Python 3.3 code implementation.

28 thoughts on “Python: InsertionSort algorithm

  1. Hey joe, i have a question here, inside the second "for loop", why we using "j+1" instead of "i", i tried change "j+1" with "i" and the result is wrong. can you explain it to me, why it becomes wrong when i use "i" instead of "j+1" , but the value of "i" and "j+1" is actually the same ???

  2. to make the code in the video works(if you don't like the one in the GitHub):
    ——–
    in line 5:
    instead of :
    if A[j] > curNum:
    A[j+1] = A[j]

    ######add an if statement, and become:
    if A[j] > curNum:
    A[j+1] = A[j]
    if j == 0:
    A[j] = curNum

    reason:
    the list: A = [7, 8, 5, 4, 9, 2]
    in the outer range, when i =2, curNum = 5
    j = i – 1 = 2-1 = 1
    —#####first loop of range for j:
    A[j] = 8 > curNum=5 —> [7, 8, 8, 4,9,2]
    –#######second loop for j:
    j = 0
    A[j] = 7 > curNum = 5 —> [7,7,8,4,9,2]
    and the program ends ( same for the rest of the list, that's why u may get a lot of 7s), it doesn't have a chance to put the curNum into the position it suppose to be
    so I used:
    if j == 0:
    A[j] = curNum
    works fine!!

  3. Nice Work, but I am a bit stuck on the part where you optimize the code: I used a list with elements [ 7,5,3,2,1,4,6,9,8] and I get the output as [7,7,7,7,7,7,7,8,9]. Can you help me out

  4. Hey Joe, the optimized code at [4:35] doesn't seem to work correctly. Can you confirm if there's a mistake in the video? I checked your github, the there the code looks different from the one you showed in here.

  5. Been trying this several times to sort in ascending order and below is code which worked for me, hope it helps someone else. By the way Joe thanks for the excellent videos.

    def insertion_sort(A):
    for i in range(1, len(A)+1):
    for j in range(i-1, 0, -1 ):
    if A[j] > A[j-1]:
    break
    else:
    A[j-1], A[j] = A[j], A[j-1]
    return A

  6. Thanks, I did the code and realized the first index was never sorted. Took me a while to solve the issue on my own, but later realized that the answer was down in the comment section lol.

  7. I may be missing something, but this code is not working for me.

    def insertion_sort(A):
    for i in range(1, len(A)):
    for j in range(i-1, 0, -1):
    if A[j] > A[j+1]:
    A[j], A[j+1] = A[j+1], A[j]
    else:
    break

    I even tried copy/paste from your github account, but still doesn't appear to work.

  8. What if you have numbers with more than 1 digit? For example this algorithm sorts my numbers in a way that 33,000 seems smaller than 679 because 679 starts with the 6 which is bigger than 3. Therefore sorting my numbers [33000,679]. How would you solve this?

  9. Thanks for all these videos, Joe. I've been programming in Python for about a year now, but none of the books I've read teach anything about algorithms or data structures. I finally feel like I'm learning REAL computer science. Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *