A quick tip for anyone: you don’t need a temp variable when swapping two variables. In python, you can write the following code: x, y = y, x This swaps both of the variables in one line because python allows recursive referencing of variables, and it allows multiple identifiers to be assigned in one line.
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.
sir there is an error in the first code as in there 0th index will not be included hence first element would not be sorted it should be - for j in range(i-1,-1,-1) thanks sir for posting such videos and making people good learners you have helped me to gain interest and made me capable of finding bugs xoxoxoxo :)
Hi there, do you mind please explaining to me your for loop which you coded above? I know that your code works I just don't understand the second argument in your range function. Isn't it true that the second argument in the range function refers to where you want the iteration to stop? So it would be 'range[start, stop, step]' And isn't it true that the value "-1" refers to the last item in a list? So if you have "-1" as the second argument in your range function, doesn't that mean you want the iteration to stop at the last item in the list? Your code works so I'm obviously misunderstanding something. If you could explain to me why I'm wrong I'd very much appreciate it.
@@Aaron_17 if you start from 0th index and move in the forward direction, then Yes, -1 refers to the last element of the list. However, if you start from (n-1)th index and move in the reverse direction (here n is the size of the list), then -1 means that you want to iterate from n-1 to 0, inclusive.
Thanks for the videos Sir, two corrections required in the code are , Arr[j]=currNum inside the if loop so that the currnum value is retained, second is for range is (i-1,-1,-1)
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.
Yeah, really great job, man. I know this is a bit old but it's still a goodie. Having been an educator in a different area of study for many years, I honestly have to say that what charmed me the most was that you explained each line and what was happening in the algorithm by referring to what is left or right. So many other youtubers have forgotten to keep this compass rose available for listeners and simple rushed through the code.
For those who are struggling to keep up with this video, this is the correct and working code for the nested loop version: def insertion(lst): for i in range(1, len(lst)): print(i) for j in range(i-1,-1,-1): print(j) if lst[j]>lst[j+1]: lst[j],lst[j+1]=lst[j+1],lst[j] print(lst) else: break return lst # "Optimized" insertion sort algo based on nesterd loops def insertion(A): for i in range(1, len(A)): curNum=A[i] for j in range(i-1, -1, -1): if A[j] > curNum: A[j+1] = A[j] A[j]=curNum else: break return A #The code below will t
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
consideration : The left of the current number is already sorted You can use binary search method for comparision and then swap that will decrease time complexity O(n^2) -----> O(nlogn)
Too many errors - needs redoing. The while version should use j = i and condition j > 0. as it is, one element is left unsorted. Also the for loop version has a problem as mentioned in comments. Shame to spoil good footage with confusing inaccuracies.
There are 2 problems in this python code with the 2 for loops. for j in range(i - 1, 0, -1) is not valid. You are missing the first 2 numbers. It should be for j in range(i - 1, -1, -1) The if min_value < a_list[j]: ... else also does not work if you have only 2 numbers to swap. If you have 2 numbers, the code never enters the else and the first value is not overwritten by the min_value
Hi Joe, great video! However, I found an issue in your optimized code around 4:28: You wrote: for j in range(i-1, 0, -1): if A[j] > curNum: A[j+1] = A[j] "A[j+1] = A[j] " is not correct. This will cause over-wright the element A[j+1] by A[j]. A swap is more appropriate as below. Of course, this way there is no more optimization as you are trying to do: for j in range(i-1, 0, -1): if A[j] > curNum: A[j+1], A[j] = A[j], A[j+1]
@@oggiai Sure. here is the whole code for the one I mentioned. If we use "A[j+1] = A[j]" under "A[j] > curNum:", it will lead to incorrect sorted list because "A[j+1] = A[j]" over-writes elements instead of swap elements def insertion_sort(A): for i in range(1, len(A)): curNum = A[i] for j in range(i-1, 0, -1): if A[j] > curNum: A[j+1], A[j] = A[j], A[j+1] else: A[j+1] = curNum break return A
this is true but it will not fix the first index at the first solution using insertion sort to do this correctly you should implement : for j in range(i-1,-1,-1): because python does not take the last index in the range
+Nathan Puritt actually, this code sorts an array. Sorry for the confusion in terminology: in Python arrays are called Lists, but they're most similar in Java to ArrayLists, which are based on Arrays. I don't have any videos on sorting Linked Lists, and I think it's pretty unusual that people try to sort Linked Lists. I also have a bunch of videos on Java sorting algorithms for arrays and arrayLists.
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.
Joe James Ok thanks Joe. Any chance if you can make the corrections in the video also? Perhaps include an annotation at the start or something, so that it doesn't get confusing for new bees like me. I know editing the video might be a bit tough though. Oh, and just to let you know, I love your videos. They are short, precise and just make the right sense. Keep up :)
Yeah, I already edited the video the best I could. TH-cam has very rudimentary editing features, so I think depending on your browser my edits may not even show up.
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 ???
I think it's because j+1 is only equal to i on the first iteration of the inner for loop. On the subsequent iterations, the value of j decrements by 1.
sir there is an error in the first code as in there 0th index will not be included hence first element would not be sorted it should be - for j in range(i-1,-1,-1)
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.
+Никола Пауновић a very common problem in python is tabs. When you look at the screen you can't tell the difference between 4 spaces and a tab, but the Python interpreter sees that as an error. Your text editor probably has an option that lets you view hidden characters, so turn that on and make sure you do not have a mixture of spaces and tabs. You must either use all tabs for indents, or always the same number of spaces for indents throughout any python program. If that doesn't solve the problem then tell me what your error message is.
+Joe James Thx for the quick response. No, I don't get an error message. Problem is that the first number in the list doesn't get sorted. Here's a fiddle with code pasted from your github repo: pythonfiddle.com/insertion-sort-not-working. I've just added a return statement at the end.
+Joe James Ok, I think I got it. Your inner loop seems to never get to the first element of the outer one. Although, when you insert the while loop in its place, everything's fine. Here's my code with nested for loops that I got to work: def insertion_sort(A): for i in range(1, len(A)): for j in range(i, 0, -1): if A[j] < A[j-1]: A[j], A[j-1] = A[j-1], A[j] else: break return A Now, with j-1 it reaches for the first element of the outer loop when doing the comparison.
sir there is an error in the first code as in there 0th index will not be included hence first element would not be sorted it should be - for j in range(i-1,-1,-1)
Joe your shift insertion sort, still not right, the lowest won't show up 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 return A A=[2,4,5,3,1,9,6] print(InsertionSort(A)) # print result [1] is missing #[2, 2, 3, 4, 5, 6, 9]
seems to work fine for me. Don't call the sort using print(insertion_sort1(A)). Call it like this: A = [5, 9, 1, 2, 4, 8, 6, 3, 7] print(A) insertion_sort1(A) print(A) A=[2,4,5,3,1,9,6] insertion_sort3(A) print(A)
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?
+Wiktor Winczura your problem is data type. You're sorting integers as strings. If Python sees these as Integers it will sort them correctly, but if it sees them as strings then it just starts reading characters from the left as if it were alphabetizing words. So you probably need to convert your data to integers.
There are errors. I took time to try to fix my algorithm, here's what I have for my insertion sort algorithm: (works after a few tests) def insertion_sort(list): for i in range(1, len(list)): currentNumber = list[i] for j in range(i-1, -1, -1): if(currentNumber currentNumber): list[j+1] = list[j] else: list[j+1] = currentNumber break
Ok, this is not the insertion sort I read in geeksforgeeks, is it not supposed to INSERT a value to the beginning in a comparison? the insertion sort I did as practice before checking this video was fast, then I come here, and I find TOO much swapping and no INSERT in the algorithm, sight, oh well, I saving these examples anyway XD, by the way, actually inserting the numbers is faster than swapping them XD, the one I wrote is 2x faster than the curNum one case XD.
You will see the difference if the list has some immense amount of values in it... Computers nowadays are fast so 10,000 or 100,000 values are not much... Try using it on a much bigger list and you will see the difference.
a = [5,6,9,1,3,0,15] 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 this code is not workin its returning the same original list
Right, Python’s built-in sorting algorithm is very fast. You may never need to write your own sorting algorithm. But sorting algorithms is a common part of every intro algorithms course, and hence are a frequent interview topic.
def insertionsort(A): for i in range(0,len(A)): curnum=A[i] for j in range(i-1,-1,-1): if A[j]>curnum and j==0: A[j+1]=A[j] A[j]=curnum elif A[j]>curnum: A[j+1]=A[j] else: A[j+1]=curnum break return A this is the code for shifting.. the shifting code given in video is wrong
A quick tip for anyone: you don’t need a temp variable when swapping two variables. In python, you can write the following code:
x, y = y, x
This swaps both of the variables in one line because python allows recursive referencing of variables, and it allows multiple identifiers to be assigned in one line.
Yes true
2:57
correction required:: for j in range(i-1,-1,-1):
check for test case:: A=[10,9,8,5,3,7,92,185,78,1,0,65]
thank you for this correction because i found same problem but i couldn't solve it so thank you
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.
Bhasin H. Data Structures with Python...and Algorithms in Python 2023
Educohack Team. Python-Based Data Structures and Algorithms 2024.pdf
best teacher of sorting algorithms in python i found on youtube so far... Thank you Joe James.
sir there is an error in the first code as in there 0th index will not be included hence first element would not be sorted it should be - for j in range(i-1,-1,-1) thanks sir for posting such videos and making people good learners you have helped me to gain interest and made me capable of finding bugs
xoxoxoxo :)
лол, не я одна заметила
yes , you are absolutely correct my dear friend !
good one
Hi there, do you mind please explaining to me your for loop which you coded above? I know that your code works I just don't understand the second argument in your range function. Isn't it true that the second argument in the range function refers to where you want the iteration to stop? So it would be 'range[start, stop, step]' And isn't it true that the value "-1" refers to the last item in a list? So if you have "-1" as the second argument in your range function, doesn't that mean you want the iteration to stop at the last item in the list? Your code works so I'm obviously misunderstanding something. If you could explain to me why I'm wrong I'd very much appreciate it.
@@Aaron_17 if you start from 0th index and move in the forward direction, then Yes, -1 refers to the last element of the list.
However, if you start from (n-1)th index and move in the reverse direction (here n is the size of the list), then -1 means that you want to iterate from n-1 to 0, inclusive.
Thanks for the videos Sir, two corrections required in the code are , Arr[j]=currNum inside the if loop so that the currnum value is retained, second is for range is (i-1,-1,-1)
can you please send the code of the fixed version of the last example I don't get it
@@baranpolat2910
def insertion_sort(A):
for i in range(1, len(A)):
curNum = A[i]
for j in range(i-1, -1, -1):
if A[j] > curNum:
A[j+1] = A[j]
A[j] = curNum
else:
A[j+1] = curNum
break
this video is still usefull after 5 years........... thank you sir , great explanation
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.
Yeah, really great job, man. I know this is a bit old but it's still a goodie. Having been an educator in a different area of study for many years, I honestly have to say that what charmed me the most was that you explained each line and what was happening in the algorithm by referring to what is left or right. So many other youtubers have forgotten to keep this compass rose available for listeners and simple rushed through the code.
Thanks.
For those who are struggling to keep up with this video, this is the correct and working code for the nested loop version:
def insertion(lst):
for i in range(1, len(lst)):
print(i)
for j in range(i-1,-1,-1):
print(j)
if lst[j]>lst[j+1]:
lst[j],lst[j+1]=lst[j+1],lst[j]
print(lst)
else:
break
return lst
# "Optimized" insertion sort algo based on nesterd loops
def insertion(A):
for i in range(1, len(A)):
curNum=A[i]
for j in range(i-1, -1, -1):
if A[j] > curNum:
A[j+1] = A[j]
A[j]=curNum
else:
break
return A
#The code below will t
thank you very much
Thank you for the video.
I think the stop parameter of the second for-loop should be -1, not 0.
for j in range(i-1, -1, -1)
I think so too, because if it is 0 that means we always assume the first number is the smallest. Good catch, I was wondering what was going on.
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
set 0 in the for loop parameter for the ending index to -1 and it should work fine
See code on my GitHub repository here, github.com/joeyajames/Python
Fast and simple to understand!
Thanks for making such wonderful content!! 😃
Incredible video. Exactly what I was looking for!
Mr.Joe u very beautifully explained the insertion sort 😊
thank you Joe James for these lovely tutorials you have helped me a lot in getting better at Python
These videos are incredibly helpful. Thank you for helping make this information available!
consideration : The left of the current number is already sorted
You can use binary search method for comparision and then swap that will decrease time complexity
O(n^2) -----> O(nlogn)
Too many errors - needs redoing. The while version should use j = i and condition j > 0. as it is, one element is left unsorted. Also the for loop version has a problem as mentioned in comments. Shame to spoil good footage with confusing inaccuracies.
That while loop cleared all my doubts
At 2:27 it will be
for j in range(i-1,-1,-1):
Yup...or it can also be done by for j in range(i,0,-1):
And then by applying the condition a[j]
Yup , got the solution from your comment, Thanks a lot macha
There are 2 problems in this python code with the 2 for loops.
for j in range(i - 1, 0, -1) is not valid. You are missing the first 2 numbers. It should be for j in range(i - 1, -1, -1)
The if min_value < a_list[j]: ... else also does not work if you have only 2 numbers to swap. If you have 2 numbers, the code never enters the else and the first value is not overwritten by the min_value
Another way of doing the same:
def InsertionSort(Sortee):
for index,item in enumerate(Sortee):
if index==0:
continue
ti=index
while item
Hi Joe, great video! However, I found an issue in your optimized code around 4:28:
You wrote:
for j in range(i-1, 0, -1):
if A[j] > curNum:
A[j+1] = A[j]
"A[j+1] = A[j] " is not correct. This will cause over-wright the element A[j+1] by A[j]. A swap is more appropriate as below. Of course, this way there is no more optimization as you are trying to do:
for j in range(i-1, 0, -1):
if A[j] > curNum:
A[j+1], A[j] = A[j], A[j+1]
Thanks Sabrina. You should share your code for the whole algorithm.
@@oggiai Sure. here is the whole code for the one I mentioned. If we use "A[j+1] = A[j]" under "A[j] > curNum:", it will lead to incorrect sorted list because "A[j+1] = A[j]" over-writes elements instead of swap elements
def insertion_sort(A):
for i in range(1, len(A)):
curNum = A[i]
for j in range(i-1, 0, -1):
if A[j] > curNum:
A[j+1], A[j] = A[j], A[j+1]
else:
A[j+1] = curNum
break
return A
@@guccilover2009 this is still incorrect, plz run the list where A= [99,1,3,2]
@@cloudprograms9612 yes, because again you should change>> for j in range(i-1, 0, -1): with>> for j in range(i-1, -1, -1):
this is true but it will not fix the first index at the first solution using insertion sort
to do this correctly you should implement :
for j in range(i-1,-1,-1):
because python does not take the last index in the range
Please put the code in the description so that we can directly copy paste it ;)
Www.github.com/joeyajames/python
tanks Joe james
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?
+Nathan Puritt actually, this code sorts an array. Sorry for the confusion in terminology: in Python arrays are called Lists, but they're most similar in Java to ArrayLists, which are based on Arrays. I don't have any videos on sorting Linked Lists, and I think it's pretty unusual that people try to sort Linked Lists. I also have a bunch of videos on Java sorting algorithms for arrays and arrayLists.
unfortunately, I do not want to code a linked list but I am required to for my schooling. Thanks for your help.
how did you light up the like button at the end?
thanks, those videos are very helpful!
what is the break keyword used for here? i know what it does but what purpose it serves here
thank you for this
You bet!
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.
+Ketan Singh the code on GitHub is always the latest code. I made a correction in line 4 in the range( ), so please use the github code.
Joe James Ok thanks Joe. Any chance if you can make the corrections in the video also? Perhaps include an annotation at the start or something, so that it doesn't get confusing for new bees like me. I know editing the video might be a bit tough though.
Oh, and just to let you know, I love your videos. They are short, precise and just make the right sense. Keep up :)
Yeah, I already edited the video the best I could. TH-cam has very rudimentary editing features, so I think depending on your browser my edits may not even show up.
Oh !! I didn't know youtube has such a behaviour as well. Yes, I do not see any changes in the video. Anyways thanks for pointing that out.
The python code at [4:35] has a bug. To fix it, we could add the following code between line 6 and 7. "if j==0: A[j] = curNum"
It should be.....for j in range(i-1,-1,-1):
As we also want to include index 0 in j
Great
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 ???
I think it's because j+1 is only equal to i on the first iteration of the inner for loop. On the subsequent iterations, the value of j decrements by 1.
sir there is an error in the first code as in there 0th index will not be included hence first element would not be sorted it should be - for j in range(i-1,-1,-1)
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.
+Никола Пауновић a very common problem in python is tabs. When you look at the screen you can't tell the difference between 4 spaces and a tab, but the Python interpreter sees that as an error. Your text editor probably has an option that lets you view hidden characters, so turn that on and make sure you do not have a mixture of spaces and tabs. You must either use all tabs for indents, or always the same number of spaces for indents throughout any python program. If that doesn't solve the problem then tell me what your error message is.
+Joe James
Thx for the quick response. No, I don't get an error message. Problem is that the first number in the list doesn't get sorted. Here's a fiddle with code pasted from your github repo: pythonfiddle.com/insertion-sort-not-working. I've just added a return statement at the end.
+Joe James Ok, I think I got it. Your inner loop seems to never get to the first element of the outer one. Although, when you insert the while loop in its place, everything's fine. Here's my code with nested for loops that I got to work:
def insertion_sort(A):
for i in range(1, len(A)):
for j in range(i, 0, -1):
if A[j] < A[j-1]:
A[j], A[j-1] = A[j-1], A[j]
else:
break
return A
Now, with j-1 it reaches for the first element of the outer loop when doing the comparison.
+Никола Пауновић yeah, i just made this change and it worked, for j in range(i-1, -1, -1):
sir there is an error in the first code as in there 0th index will not be included hence first element would not be sorted it should be - for j in range(i-1,-1,-1)
Joe your shift insertion sort, still not right, the lowest won't show up
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
return A
A=[2,4,5,3,1,9,6]
print(InsertionSort(A))
# print result [1] is missing
#[2, 2, 3, 4, 5, 6, 9]
seems to work fine for me.
Don't call the sort using print(insertion_sort1(A)). Call it like this:
A = [5, 9, 1, 2, 4, 8, 6, 3, 7]
print(A)
insertion_sort1(A)
print(A)
A=[2,4,5,3,1,9,6]
insertion_sort3(A)
print(A)
@@oggiai Thank you Joe, Yes all work now, guess something wrong from my end when I run the code, Thanks
The last example of code does not work correctly.
This is incorrent...
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?
+Wiktor Winczura your problem is data type. You're sorting integers as strings. If Python sees these as Integers it will sort them correctly, but if it sees them as strings then it just starts reading characters from the left as if it were alphabetizing words. So you probably need to convert your data to integers.
thank you
There are errors. I took time to try to fix my algorithm, here's what I have for my insertion sort algorithm: (works after a few tests)
def insertion_sort(list):
for i in range(1, len(list)):
currentNumber = list[i]
for j in range(i-1, -1, -1):
if(currentNumber currentNumber):
list[j+1] = list[j]
else:
list[j+1] = currentNumber
break
Error in "for j in range (i-1,.....)
Ok, this is not the insertion sort I read in geeksforgeeks, is it not supposed to INSERT a value to the beginning in a comparison? the insertion sort I did as practice before checking this video was fast, then I come here, and I find TOO much swapping and no INSERT in the algorithm, sight, oh well, I saving these examples anyway XD, by the way, actually inserting the numbers is faster than swapping them XD, the one I wrote is 2x faster than the curNum one case XD.
I think the shifting method may not be correct.
THX
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.
You will see the difference if the list has some immense amount of values in it... Computers nowadays are fast so 10,000 or 100,000 values are not much... Try using it on a much bigger list and you will see the difference.
a = [5,6,9,1,3,0,15]
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
this code is not workin its returning the same original list
As python has internal timsort algorithm to sort data , do we need to write other sorting algorithms in python . What case is this used
Right, Python’s built-in sorting algorithm is very fast. You may never need to write your own sorting algorithm. But sorting algorithms is a common part of every intro algorithms course, and hence are a frequent interview topic.
wrong ,you should type
else:
j+=1
break
A[j]=curNum
wait who's joe?
Joe mama
joe about to get joe self brain damage
isnt this just bubblesort
Wait... isn't this is bubble sort?!
no its going on infinite. .... mergesort(A,first,mid) in mergesort2() is not correct
Did you retype the code yourself, or run code from my GitHub site?
who's here because of coding interviews? lmao
what is the janky looking '-1' edited into the video?
Swaaaaap
😂 😂 😂
No offence
what will it return?? babaji ka thullu??
Swaaaap
CODE IS WRONG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
def insertionsort(A):
for i in range(0,len(A)):
curnum=A[i]
for j in range(i-1,-1,-1):
if A[j]>curnum and j==0:
A[j+1]=A[j]
A[j]=curnum
elif A[j]>curnum:
A[j+1]=A[j]
else:
A[j+1]=curnum
break
return A
this is the code for shifting.. the shifting code given in video is wrong