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 ???

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!!

Fix for last optimized algorithm https://github.com/joeyajames/Python/blob/master/SortingAlgorithms.py # optimized – shifts instead of swapping def insertion_sort3(A): for i in range(1, len(A)): curNum = A[i] k = 0 for j in range(i-1, -2, -1): k = j if A[j] > curNum: A[j+1] = A[j] else: break A[k+1] = curNum

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

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.

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

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.

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?

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.

Swaaaaap

😂 😂 😂

No offence

Please put the code in the description so that we can directly copy paste it 😉

isnt this just bubblesort

return(A)

print(insertionsort(MyList))

MyList = [1,3,5,7,2,1,2,1,6,9,9,7,6,55,99]

To get an output

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 ???

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!!

thank you

Fix for last optimized algorithm

https://github.com/joeyajames/Python/blob/master/SortingAlgorithms.py

# optimized – shifts instead of swapping

def insertion_sort3(A):

for i in range(1, len(A)):

curNum = A[i]

k = 0

for j in range(i-1, -2, -1):

k = j

if A[j] > curNum:

A[j+1] = A[j]

else:

break

A[k+1] = curNum

Swaaaap

thanks help a lot

tanks Joe james

It shifting one does not work for[6, 5, 4, 3, 2, 1]

i have tried both method but i don't see any major difference in total time taken just 0.01 or 0.02 sec difference.

3:19 swapping fundamentals but he uses 'shifting' because it's quicker

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

thanks, those videos are very helpful!

THX

thank you Joe James for these lovely tutorials you have helped me a lot in getting better at Python

what is the janky looking '-1' edited into the video?

These videos are incredibly helpful. Thank you for helping make this information available!

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.

Hi James, great video, now from my understanding this insertion Sort code would sort a linked list, how would I change it so that it sorts an array?

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

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.

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.

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?

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.

See code on my GitHub repository here, https://github.com/joeyajames/Python