Тёмный

Insertion Sort Algorithm Explained (Full Code Included) - Python Algorithm Series for Beginners 

Derrick Sherrill
Подписаться 84 тыс.
Просмотров 91 тыс.
50% 1

This video is a part of a full algorithm series: • Bubble Sort Algorithm ...
In this one we'll cover the insertion sort algorithm.
The insertion sort algorithm breaks a list into two sublists, one with sorted values and the other with unsorted values. We move one unsorted value to the sorted sublist, and compare its value to the values to the left. We move it into the correct position by switching, each time the item to the left is larger.
#InsertionSort #Python #Algorithm
One we have sorted the element, we start the next iteration and sort the next unsorted item in the unsorted sublist.
The Insertion sort algorithm has a complexity of O(n^2) because we're still doing comparisons with two nested lists. However, this one is preferable to the bubble and selection sort algorithms. We take the "best" parts of both of those and combine them to insertion sort.
If you need more of an idea on how we use the Insertion Sort Algorithm, think of how we organize hands of playing cards. When we draw a new card into our hand, we place it in the front, find the position that it would go to within the hand, and then draw the next. The Insertion Sort Algorithm is attempting to do the same thing.
Join The Socials -- Picking Shoutouts Across RU-vid, Insta, FB, and Twitter!
FB - / codewithderrick
Insta - / codewithderrick
Twitter - / codewithderrick
LinkedIn - / derricksherrill
GitHub - github.com/Derrick-Sherrill
Thanks so much for the continued support 5440+ subscribers at the time of writing! Very incredible that I get this opportunity to share content about something I'm passionate about. Thank you for your kind words of encouragement and support. You all are awesome!
*****************************************************************
Full code from the video:
def insertion_sort(list_a):
indexing_length = range(1, len(list_a))
for i in indexing_length:
value_to_sort = list_a[i]
while list_a[i-1] (greater than) value_to_sort and i(greater than)0:
list_a[i], list_a[i-1] = list_a[i-1], list_a[i]
i = i -1
#Sorry - RU-vid doesn't allow angled brackets in the description :(
return list_a
print(insertion_sort([7,8,9,8,7,6,5,6,7,8,9,8,7,6,5,6,7,8]))
github.com/Derrick-Sherrill/P...
Packages (& Versions) used in this video:
Python 3.7
Atom Text Editor
*****************************************************************
Code from this tutorial and all my others can be found on my GitHub:
github.com/Derrick-Sherrill/D...
Check out my website:
www.derricksherrill.com/
If you liked the video - please hit the like button. It means more than you know. Thanks for watching and thank you for all your support!!
--- Channel FAQ --
What text editor do you use?
Atom - atom.io/
What Equipment do you use to film videos?
Blue Yeti Microphone - amzn.to/2PcNj5d
Mic sound shield - amzn.to/3bVNkEt
Soundfoam - amzn.to/37NV9ci
Camera desk stand - amzn.to/3bX8xhm
Box Lights - amzn.to/2PanL95
Side Lights - amzn.to/37KSNut
Green Screen - amzn.to/37SFFnc
What computer do you use/desk setup?
Film on imac (4k screen) - amzn.to/37SEu7g
Work on Macbook Pro - amzn.to/2HJ5b3G
Video Storage - amzn.to/2Pey8sw
Mouse - amzn.to/2PhCtv3
Desk - amzn.to/37O1Mv1
Chair - amzn.to/2uqHE4E
What editing software do you use?
Adobe CC - www.adobe.com/creativecloud.html
Premiere Pro for video editing
Photoshop for images
After Effects for animations
Do I have any courses available?
Yes & always working on more!
www.udemy.com/user/derrick-sh...
Where do I get my music?
I get all my music from the copyright free RU-vid audio library
ru-vid.com...
Let me know if there's anything else you want answered!
-------------------------
Always looking for suggestions on what video to make next -- leave me a comment with your project! Happy Coding!

Наука

Опубликовано:

 

13 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 135   
@maanyapuri8311
@maanyapuri8311 4 года назад
Oh god, you explain this way better than my Software teacher. Thank you so much!
@Aflesyn
@Aflesyn 4 года назад
I fell in love with your tutorials, thank you very much for your work! you also have an amazing voice, which makes watching videos especially enjoyable. Love, Jam
@timohelasvuo9650
@timohelasvuo9650 4 года назад
Thanks for the video! Another golden nugget :)
@Caimille
@Caimille Год назад
Thank you so much for your video! I was having a hard time understanding the insertion sort algorithm and now I have a much better handle on it.
@peterfrancisrobante8830
@peterfrancisrobante8830 3 года назад
having an explanation that only takes a minute is the best. keep it up!!
@HanisaMohamed
@HanisaMohamed 4 года назад
I can always rely on your videos to give a good explanation! Thanks!!!
@verceti7
@verceti7 4 года назад
You're awesome dude! Thanks! Keep the videos up
@AmanSharma-ht5zq
@AmanSharma-ht5zq 4 года назад
Thank you so much, bro. I see the neatest codes in your videos. Keep making videos on more difficult topics.
@nathanellis7819
@nathanellis7819 4 года назад
Thank you mate. These are great!
@jessijasa
@jessijasa Год назад
Thanks for the video! Very pleasant to watch. You explained everything in detail.
@shrvansudhakar7514
@shrvansudhakar7514 3 года назад
thanks a lot Derrick, please keep on doing gr8 stuff.
@emeryli2331
@emeryli2331 3 года назад
i love your voice, it's so calm, clear and comfortable
@mohammadaltaha8386
@mohammadaltaha8386 3 года назад
YOU ARE A LEGEND! Thank you so much
@swatiruhela655
@swatiruhela655 2 года назад
So nice explanation .. I was finding insertion sort very tough , but your interpretation made me easy to understand .. Thanks a lot Derrick..
@Tory_Rainman
@Tory_Rainman 2 года назад
Nice video man, thank you for the clarity
@Metrotouille2243
@Metrotouille2243 2 года назад
Thanks! was struggling to understand this one, but it was so simple!
@divyanshgupta748
@divyanshgupta748 3 года назад
This is sooo nicely explained. Thanks.
@harshyadav1214
@harshyadav1214 7 месяцев назад
Great Content man
@kenjikakashi
@kenjikakashi 2 года назад
Love your explanation. Please keep making more videos like this
@b.f.skinner4383
@b.f.skinner4383 3 года назад
This is the first sorting algorithm I've attempted to learn, this video has been a tremendous help. Thank you
@kanikakansalpathshala1624
@kanikakansalpathshala1624 2 года назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-lzS_DiMlR2Q.html
@rhondan181
@rhondan181 Год назад
Great vid, thank you!
@Captainfeso
@Captainfeso 4 года назад
You explained this so well! Even a 10 year old would understand!
@naiadbaksh3996
@naiadbaksh3996 3 года назад
As much as your explanations are great, your editing makes these videos a cut above the rest! That green screen (?) at the beginning is just *chef's kiss*
@guilhermesanchez9051
@guilhermesanchez9051 3 года назад
That was really helpful. Thanks
@nellamain5744
@nellamain5744 Год назад
THANK YOU YOU'RE SO GOOD AT EXPLAINING.
@huzaifaalam1328
@huzaifaalam1328 3 года назад
Can't believe i understood all of it in less than 6 minutes THANK YOU
@kilsat5030
@kilsat5030 3 года назад
So Good. Keep Going Bro
@samuel2318
@samuel2318 Год назад
clear explanation, thank you
@no0gon
@no0gon 4 года назад
this is what i looking for similar to macro SAS and i can test in python, thank you for your guide sensei
@zaedis5629
@zaedis5629 3 года назад
Thank you so much. The material I was given to teach me this only explained the concept and didn't explain the example code at all.
@ameerhbaig
@ameerhbaig 3 года назад
great explanation. Thanks!
@sahilnagpal007
@sahilnagpal007 4 года назад
Dude you are awesome, believe me, and mark my words.
@moritzwolff9383
@moritzwolff9383 4 года назад
There is an implementation of insertion sort that is almost twice as fast: def insertion(list): for i in range(1,len(list)): temp = list[i] index = i - 1 while index >= 0 and list[index] > temp: list[index + 1] = list[index] index -= 1 list[index + 1] = temp return list This is because Derricks implementation needs almost twice as many steps for each iteration of the while-loop compared to the implementation proposed here (two assignment statements: list_a[i] to list_a[i-1] and vice versa). You can confirm this for yourself comparing the running times of both implementations: you will see that the second implementation is almost twice as fast.
@stillbald5827
@stillbald5827 4 года назад
This is pretty clever. Thanks!
@indianyoyoer
@indianyoyoer 4 года назад
Who else watching from india 🇮🇳
@tibettenballs4962
@tibettenballs4962 3 месяца назад
Cubby
@kvelez
@kvelez Год назад
This is the best video, I finally understood it. You made it very easy, I'll subscribe. I recreated it, hope it looks good: list = []#list limit = int(input("Amount of elements to sort? "))#only int for _ in range(limit):#repetition item = input("Type the element: ") #int or str list.append(item)#addition to the list for i in range(1,len(list)):#looping through list value_to_sort = list[i]#element to the right. while list[i-1] > value_to_sort and i > 0: #compare left to right elements list[i-1], list[i] = list[i], list[i-1] #Ascending # list[i-1], list[i] = list[i], list[i-1] #Descending i -=1 print("List: ", list) #output
@shounaugustine5320
@shounaugustine5320 4 года назад
Thanks man keep making videos
@dantedt3931
@dantedt3931 2 года назад
Great explanation, thanks.
@KM-nx5sh
@KM-nx5sh 4 года назад
Thanks so much !
@emmanuelugwu918
@emmanuelugwu918 2 года назад
if i could like this video a million times i would.. thank you soo much
@prasadxev
@prasadxev 9 месяцев назад
great explanation
@sagarsingh-jv5fn
@sagarsingh-jv5fn 2 года назад
Simplest explantion avaliable online, Thanks!
@fourten0410
@fourten0410 3 года назад
Outstanding content, very clear and organized, thank you!
@kunalmeshram2323
@kunalmeshram2323 2 года назад
I just went through 10 videos but this video is just got throw too my head and other's were bounces over 🤣 thanks man
@nerapelin8851
@nerapelin8851 2 года назад
Thank you very much this helped a lot :))
@robertovillanueva7077
@robertovillanueva7077 3 года назад
Thanks a ton!
@shivaay.4565
@shivaay.4565 9 месяцев назад
KNOWLEDGEABLE CONTENT KEEP IT UP BROTHER😎 #I AM FROM INDIA
@danigetpeace3514
@danigetpeace3514 11 месяцев назад
Thankyou Very much
@chillprogrammer
@chillprogrammer 2 года назад
thanku so much bro
@Aflesyn
@Aflesyn 4 года назад
really want to see merge sort & depth-first search by you! such a cool explanation
@christineeee96
@christineeee96 4 года назад
me too!
@s-1374
@s-1374 2 года назад
Thank you
@amulyatiwari3019
@amulyatiwari3019 3 года назад
Man, what a jawline!!
@robertomanrique8004
@robertomanrique8004 2 года назад
Thank you bro for these python algorithm, great explained, please bro if you might do more videos of this topic 'Python Algorithm Series for Beginners', I'll apreciate it a lot.
@sanketgore2515
@sanketgore2515 4 года назад
Nice one👍
@zakiquereshy3170
@zakiquereshy3170 4 года назад
So much better than my professor !
@itech40
@itech40 4 года назад
thanks! :)
@nitesharu1269
@nitesharu1269 3 года назад
It's confirm now, every guy with a dead face and freakin light speed brain is a developer
@studiesonly565
@studiesonly565 3 года назад
Yeah...they said learning to code was free but took away my childhood :/
@abhinandanmohanty3833
@abhinandanmohanty3833 3 года назад
Can you please explain why i=i-1 make the next iteration??
@arunbalakrishnan8978
@arunbalakrishnan8978 3 месяца назад
That is moving towards left side in sorted list to check where should we insert the current unsorted element.
@toprecbot
@toprecbot 3 года назад
Hello good explanation,
@ontogeekchic
@ontogeekchic Год назад
Great
@abdullahshahid6216
@abdullahshahid6216 3 года назад
This has the best time and space complexity Run this code into Pythontutor to understand for i in range(1,len(arr)): current = arr[i] while i > 0 and arr[i-1] > current: arr[i] = arr[i-1] # shifting larger number to the right i= i-1 arr[i] = current
@GeronimoLTondato
@GeronimoLTondato 2 года назад
Not going to code it but you can use binary search to find the position for "value_to_sort" into the sorted array. That way the algorithm performs at O(n+log(N)), of course, you also need to leave gaps between the sorted elements to prevent new insertions to trigger array shifting.
@yashmetha1232
@yashmetha1232 4 года назад
Thanks bruh pls do videos on matplotlib and seaborn
@xerostat8961
@xerostat8961 3 года назад
this is great, but can you do the same with for loop?
@SuperBhavanishankar
@SuperBhavanishankar 2 года назад
Hi. can we do as simple as: for i in range(0,len(lst)): while (lst[i-1]>lst[i] and i>0): lst[i-1],lst[i]=lst[i],lst[i-1] i-=1 print(lst)
@rajarshimondal1343
@rajarshimondal1343 3 года назад
i like it
@jimmygizmo
@jimmygizmo 2 года назад
People who produce coding tutorials can learn a lot of excellent things from how you do yours. Thanks for the great videos! I think this series is great. Some requests for your next videos in this series (more for keeping a nice set of algos for your viewers rather than my own needs) .. but I think it would be nice to add maybe some of these: Linked List, Tree Traversal (maybe in the context of walking a directory structure. in other words, DAGs, Directed-Acyclic Graphs), maybe also parsing of tag pairs like matching parentheses or brackets or similar, since this basically is a common and juicy little application of stacks, pushing/popping arrays etc. My suggestions are coming from algorithms I was tested on during assessments at Apple, Google and Yahoo for senior roles, but also those are elegant ones I like and can be either simple or very challenging so there is a lot to work with in those. Again, thanks a lot for the excellent videos. The content, presentation and topics are excellent and also, I really love the visuals, colors and fonts etc. No time wasted on irrelevant content. You've removed all the barriers to learning the quality content in an enjoyable, fast way and that is what it is all about. One final thought. For getting a job, people must master as many of the popular algorithms as they can. The best jobs always hinge on this, regardless of experience level. (I'm not saying that's how it should be but that is the reality of algorithms and software engineering jobs.)
@parthrohit4614
@parthrohit4614 2 года назад
Can you make a RSA algorithm and SDES key generation with Inception and Description please .
@handle-2
@handle-2 3 года назад
how does line 8 exactly work? I know you said it swaps them but how
@mohammadalagha1582
@mohammadalagha1582 Год назад
Does this work for strings too? I want to sort a list alphabeticly
@roycepunay1304
@roycepunay1304 3 года назад
Simple and great explanation, thank you. One last thing what is the space and time complexity of the code and insertion sort algorithm itself?
@wdevcompany1726
@wdevcompany1726 2 года назад
time complexity is O(n^2)
@kanechulchulikpongse9549
@kanechulchulikpongse9549 4 года назад
why do we need to impose i>0? and i=i+1 works?
@sambhavbhalla2697
@sambhavbhalla2697 3 года назад
Hey , this helped me a lot , but I still couldn't figure out one thing , why you wrote i = i -1. I didn't understand this line of code, could you help me out?
@estebanlopez8927
@estebanlopez8927 Год назад
I would like to congratulate you on your hands
@abbaashussain6163
@abbaashussain6163 2 года назад
What if you want the numbers ordered as high to low? How would you do that?
@kenadams9024
@kenadams9024 3 года назад
Hey! Thankyou so much for the wonderful tutorial. I got the concept but I'm having a small doubt that If we are going ahead in the list shouldn't it be i = i + 1 as we are going to the next element?
@mohameduvais1821
@mohameduvais1821 3 года назад
for loop will take care of traversing through the list. i=i-1 is to compare the element from the unsorted list to sorted list one by one(it will move in left direction)
@sambhavbhalla2697
@sambhavbhalla2697 3 года назад
@@mohameduvais1821 but the for loop moves to the right , so idk this is confusing didnt understand that part
@CRISTIANO18786
@CRISTIANO18786 2 года назад
@@sambhavbhalla2697 essentially the while loop is a makeshift nested for loop. So for every iteration of the outer loop (the for loop) we are moving forward in the sequence and and we want to find the right slot for the current item therefore in the while loop we are going backwards or leftward so that we can check the current element against all elements that have already been sorted and find the correct spot. It seems to be rather inefficient tho, given its time complexity is O(n^2) worst case because you appear to have the identity 1+2+3+... n
@TheTeenKitchen
@TheTeenKitchen 4 года назад
Great! Can you teach insertion sort program without function?
@Narendiranath
@Narendiranath 4 года назад
Without function then you need to directly provide the list instead of the function. millioninformations.com
@user-zl5cv6vw2m
@user-zl5cv6vw2m 4 года назад
@@Narendiranath That's an easy way to get your email spammed
@Narendiranath
@Narendiranath 4 года назад
@@user-zl5cv6vw2m Thanks
@user-zl5cv6vw2m
@user-zl5cv6vw2m 4 года назад
@@Narendiranath No worries.
@adeli583
@adeli583 4 года назад
quick question: at the end why it is i = i - 1 instead of i = i + 1 ??
@aditim7430
@aditim7430 4 года назад
i have the same doubt... so if you get the answer pls let me know as well
@plackowsky4198
@plackowsky4198 4 года назад
Because that while loop works like this : if value at index i is smaller than value at i-1 it switches places. Then we need to check if our original value at i (actual i-1) is maybe smaller than value at i-2. And so it goes until our original value is greater than value at index i on the left. Then if our value is greater than the one with index-1 we exit while loop and get to the next value. Sorry if i mispelled something wrong, i understood this code today :p
@jackzero5230
@jackzero5230 3 года назад
@@plackowsky4198 you have explained it very well, infact @Derric Sherrill missed it, thanks Bro!
@Maks-wp2wj
@Maks-wp2wj 4 года назад
I dont really understand what " i = i - 1" stands for. Coz the only thing i can actually see, is we subtract 1 from "i" each time, which makes us to stay at the same exact number everytime. Can someone please this like you would do it for a child? Thank you
@parthbhodia9575
@parthbhodia9575 3 года назад
same doubt
@cosmicbandido8186
@cosmicbandido8186 3 года назад
@Maks Have exactly the same question. unfortunately you didnt get an answer
@leetcode_solutions
@leetcode_solutions 2 года назад
💯
@ariskoutsoukis7849
@ariskoutsoukis7849 4 года назад
def insertionSort(array): index = 0 for item in range(index , len(array) ): first_ptr = index second_ptr = index - 1 while second_ptr > -1: if array[first_ptr] < array[second_ptr]: array[second_ptr] , array[first_ptr] = array[first_ptr] , array[second_ptr] else: break second_ptr = second_ptr - 1 first_ptr = first_ptr - 1 index = index + 1
@Silentmjrt
@Silentmjrt 3 года назад
I have a quick question, it may seem stupid to ask but I cannot see it. I don't understand why the i = i - 1 is necessary? I thought the incrementing step through the list was the job of the for i in range() operation? I am a complete noob to all this stuff. Thank you for your great content, it's really helping me grasp applications of the foundations I've been building.
@varunbisht8576
@varunbisht8576 3 года назад
Range function only generates a sequence of values. for loop makes sure that operation in body of for loop is carried out on all the values generated by range function. it doesn't do anything related to increment until specified in body of for loop
@michaelscarn7375
@michaelscarn7375 3 года назад
Seems like indexing_range should be equal " range(1, len(list_a)+1)". Without "+1" last item won't be reached and hence sorted
@salikmalik7631
@salikmalik7631 3 года назад
I confused, We have builtin sort function in python so why we need that too much code.
@naitikmundra8511
@naitikmundra8511 3 года назад
Hey guys, optimized version of the code: def insertion_sort(list_to_be_sorted): for i in range(1, len(list_to_be_sorted)): key = list_to_be_sorted[i] j = i - 1 while j >= 0 and key < list_to_be_sorted[j]: list_to_be_sorted[j + 1] = list_to_be_sorted[j] j -= 1 list_to_be_sorted[j + 1] = key return list_to_be_sorted
@MrKB_SSJ2
@MrKB_SSJ2 Год назад
This code really is good. Thanks a lot !
@hocosta1
@hocosta1 4 года назад
Hey Derik, my name is Henrique, found you on youtube and have been watching your videos a lot, and I am loving it, you are doing a great job! Found your videos when researching how to automate some basic stuff of my daily activities I own a small distribution business where I sell construction items to small traders and I have a problem I would like to solve. with around 200 customers listed on my spreadsheet I sell to around 5 of day everyday, I could be selling much more as my customers need products to do their job, however is very hard to predict when they need (hard to remember last time they bought and if they are running out of material) I got 2 problems that I am sure could be easily sorted with 2 spreadsheets and some Python Problem 1, when I m out for a delivery I usually know there are some of my customers around that might want to buy my products, however as I am driving is just impossible to stop my car and try to call one by one to figure out who needs my products. On my spreadsheet I could sort the list of potential customer using the first 3 characters of their POSTCODE as it would tell me how close they are, I could pick the 3 customers below and 3 customers after the postcode I am delivering. customer_name Cust_whatsapp Customer_Postcode Jan_purc Feb_Pur John +551196194988 03650000 Paint Glue Mary +5511999054333 03750000 Paint none Paul +5511999999999 03760000 Glue none Question 1 - what can I do in Python to go to my sheet and select a group of customer's postcode by proximity of address I am delivering ( It would be also nice if they could be automatically sent to web.whatsapp so I could send them a message. Problem 2 On a second spreadsheet I have the purchase history, I woul love to be able to predict when my Customer is ready to buy again. Question 2- Is it possible to create a simple code to read all purchase history from columns and give me a hint of when they are more likely to buy again? some output in a third spreadsheet with a list ordered by most likely to need, this way I dont need to do it by chance and keep skipping customers to my competitors Hey Man, even if I don't get a reply, I still love what you do, sharing knowledge is the best thing a person can do! Sooner or later I will figure out how to do it. Cheers Henrique
@cosmicbandido8186
@cosmicbandido8186 3 года назад
I would suggest you hire a coder for your needs. Because as far as I see your business is selling items and not coding. In python maybe Pandas and some grafic plot modules like matplotlib can help. Not sure about your live location tracking and clients nearby though. Good luck
@kasyapdharanikota8570
@kasyapdharanikota8570 3 года назад
why was i=I-1 written ?
@knightr1der
@knightr1der 4 года назад
What's the text editor name??
@CodeWithDerrick
@CodeWithDerrick 4 года назад
Sorry for the delay! I use Atom in my tutorials with the platformio-ide-terminal package for the little terminal at the bottom. Happy Coding!
@zakirhossain7413
@zakirhossain7413 3 года назад
it runs fine without declaring list_a as an additional variable. is this bad practice? def insertion_list(list_a): # indexing_len = range(1, len(list_a)) "program runs just fine without declaring this variable. can anyone please explain if it's bad practice? for i in range(1, len(list_a)): value_sort = list_a[i] while list_a[i - 1] > value_sort and i > 0: list_a[i], list_a[i - 1] = list_a[i - 1], list_a[i] i = i -1 return list_a
@cigxhang486
@cigxhang486 2 года назад
why is it i = i - 1 instead of i = i + 1?Aren't we going to the right to check?
@subhansaadat4548
@subhansaadat4548 3 года назад
Can anyone let me know why did we write i=I-1 ??
@cosmicbandido8186
@cosmicbandido8186 3 года назад
As far as I understand you check with i = i-1 the same element again against the next possible element on its left side until the while condition is False.
@polymorph5067
@polymorph5067 3 года назад
4:34 This code is not bad but I think there's no reason to create new variables here. Just have a look at my code below: def ins_sort(arr:list) -> list: for i in range(1, len(arr)): for j in range(0, len(arr[:i])): if arr[i] < arr[j]: arr[i], arr[j] = arr[j], arr[i] return arr I use slicing of a list instead of creating new variables
@kenadams9024
@kenadams9024 3 года назад
I think this one takes more time
@JT2751257
@JT2751257 3 года назад
Wrote this before looking at your solution :) list_nos = [10,16,8,12,15,6,3,9,5,100] def insertion_sort(list_nos): list_new=[] for i in range(len(list_nos)): list_new.append(list_nos[i]) while i>0: if list_new[i]
@destined0645
@destined0645 2 года назад
why are u swappig it in that way ,i thought it was meant to be the reversed way of yours? n
@genericstospecifics1856
@genericstospecifics1856 4 года назад
why are we using i = i-1 ??
@anshulvairagade1604
@anshulvairagade1604 4 года назад
We have to compare the current element with the left element down the list
@atod2572
@atod2572 4 года назад
Even I didn't get it. Coz we are already comparing elements to left in the while loop itself so why i=i-1 again?
@anshulvairagade1604
@anshulvairagade1604 4 года назад
@@atod2572 we are using i=i-1 coz we are not just using the left element, I'd the left element is not larger it will check the left of left till zeroth index Hope you got it now. Don't check for just i=1 Check for say i=4 or 5 Then you will get it. Website: visualgo.com And check for insert sort
@atod2572
@atod2572 4 года назад
@@anshulvairagade1604 thanks 👍🏻
@abdullahwasim7284
@abdullahwasim7284 3 года назад
he looks exactly like luke from percy jackson
@LownerCZ
@LownerCZ 4 года назад
Shorter code(Not faster though): def insertion_sort(lst): for i in range(1, len(lst)): while i > 0 and lst[i] < lst[i-1]: lst[i], lst[i - 1] = lst[i - 1], lst[i] i -= 1 return lst
@vidyaselvakumar7044
@vidyaselvakumar7044 4 года назад
Is this code correct?(it's too easy n small. i have exams today and can't afford a mistake) . No offense.thanks.
@r.a.a.6472
@r.a.a.6472 7 месяцев назад
Why do you always look like if you were drowning in the sea and subtly emerged to take air
@amanahmed6057
@amanahmed6057 4 года назад
anyone is forcing u to SPEAK
@CodeWithDerrick
@CodeWithDerrick 4 года назад
SPEAK master anyone has instructed long ago
@amanahmed6057
@amanahmed6057 4 года назад
@@CodeWithDerrick SIR PLZ BE ENERGETIC AND PLZ ADD SOUND BEHIND YOUR VOICE
@genericstospecifics1856
@genericstospecifics1856 4 года назад
@@amanahmed6057 Let him teach the way he does without music, most of us here are not to spend some good time listening to music but to learn concepts that are skipped or explained casually by professors at our universities.
@msms3260
@msms3260 3 года назад
@Aman Ahmed. No music needed thank you. Calm soft voice is good for such topics.
Далее
마시멜로우로 체감되는 요즘 물가
00:20
Просмотров 18 млн
Learn Insertion Sort in 7 minutes 🧩
7:05
Просмотров 189 тыс.
Python: QuickSort algorithm explained
8:33
Просмотров 112 тыс.
If __name__ == "__main__" for Python Developers
8:47
Просмотров 386 тыс.
Insertion sort in 2 minutes
2:19
Просмотров 1,2 млн
Собери ПК и Получи 10,000₽
1:00
Просмотров 2,5 млн