All language subtitles for 3 - Insertion Sort English

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt-PT Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:08,790 --> 00:00:15,290 ‫This algorithm lesson we're going to discuss the insertion sort out with them the insertion sort. 2 00:00:15,320 --> 00:00:18,900 ‫Is one of the most basic sorting algorithms out there. 3 00:00:19,020 --> 00:00:25,590 ‫And it's nice because it's incredibly easy to understand it's also pretty easy to implement when it 4 00:00:25,590 --> 00:00:27,440 ‫comes to sorting algorithms. 5 00:00:27,720 --> 00:00:29,780 ‫It does have some weaknesses. 6 00:00:29,790 --> 00:00:36,840 ‫If you have a dataset you're trying to sort that is a has a lot of integers or a lot of different items 7 00:00:36,840 --> 00:00:40,490 ‫and an insertion sort is definitely not the best one to go with. 8 00:00:40,650 --> 00:00:44,840 ‫However if you're looking for a smaller number of items it works very well. 9 00:00:44,880 --> 00:00:49,440 ‫It's also very for if you're just trying to get to learn and sorting and algorithms. 10 00:00:49,440 --> 00:00:53,740 ‫So we're going to start with this array and it has six elements. 11 00:00:53,750 --> 00:00:58,320 ‫5:47 twelve hundred thirty three and 21. 12 00:00:58,410 --> 00:01:06,630 ‫And so what we're going to do is go through each one and then get through the stages of what it takes 13 00:01:06,630 --> 00:01:10,220 ‫to sort this using the insertion sort algorithm. 14 00:01:10,350 --> 00:01:13,300 ‫So what you do is you start off on the left hand side. 15 00:01:13,500 --> 00:01:22,280 ‫And so we're going to look at the integer 5 and what the first step is comparing five and 47. 16 00:01:22,500 --> 00:01:28,670 ‫We see that 500 and 47 are already in sorted order so that's good. 17 00:01:28,780 --> 00:01:32,570 ‫That means that we don't have to do anything with these. 18 00:01:32,730 --> 00:01:38,430 ‫So are the first set of the iteration actually remains the same. 19 00:01:38,430 --> 00:01:46,950 ‫Then we go onto the third item which is 12 when we do a comparison and we say is 47 less than or equal 20 00:01:46,950 --> 00:01:52,470 ‫to 12 or you can say is 12 less than or greater than 47. 21 00:01:52,620 --> 00:01:54,250 ‫And we know 12 is less than. 22 00:01:54,270 --> 00:01:58,230 ‫And so what we're going to do is take 12 and move it right here. 23 00:01:58,230 --> 00:02:10,540 ‫So the next version of the store would be four five 12 forty seven hundred thirty three and 21. 24 00:02:10,870 --> 00:02:16,390 ‫So we now are sorted for the first three items. 25 00:02:16,390 --> 00:02:22,650 ‫Next thing we do is we look at the fourth item and we compare it to 47. 26 00:02:22,690 --> 00:02:25,410 ‫We realize 100 is greater than 47. 27 00:02:25,570 --> 00:02:29,190 ‫And so we don't have to do anything at all. 28 00:02:29,200 --> 00:02:31,950 ‫So the first four items are sorted. 29 00:02:32,050 --> 00:02:39,050 ‫Next we look at 33 in and we see is 33 less than 100. 30 00:02:39,070 --> 00:02:46,280 ‫We know it is so we say OK what is 33 less than 47. 31 00:02:46,430 --> 00:02:47,320 ‫Yes it does. 32 00:02:47,480 --> 00:02:51,140 ‫So we know 33 has to go here. 33 00:02:51,140 --> 00:02:56,600 ‫There's actually one other step in here I shouldn't let go. 34 00:02:56,600 --> 00:02:58,790 ‫We actually have to compare 33 to 12. 35 00:02:58,790 --> 00:03:00,670 ‫So that would be the real step. 36 00:03:00,710 --> 00:03:03,520 ‫You know just using common sense you can see where it would go. 37 00:03:03,520 --> 00:03:08,150 ‫But in the real life algorithm it what you would do is you take 33. 38 00:03:08,150 --> 00:03:15,770 ‫Compare it with each item to the left until it was greater than one of the items then you would just 39 00:03:15,770 --> 00:03:16,780 ‫keep on moving. 40 00:03:16,790 --> 00:03:28,390 ‫So the next version would be five twelve thirty three forty seven and 100 and for the last word and 41 00:03:28,390 --> 00:03:36,760 ‫then 21 for the last version we know we have five 12:33 47 and 100 are all sorted. 42 00:03:36,920 --> 00:03:41,770 ‫So for the last one we take 21 and we compare it to 100. 43 00:03:42,050 --> 00:03:45,180 ‫We know it's less than 100 compared to 47. 44 00:03:45,200 --> 00:03:47,150 ‫We know it's less than 47. 45 00:03:47,170 --> 00:03:52,440 ‫We compared to 33 it's still less than 33 we compared to 12. 46 00:03:52,460 --> 00:03:54,080 ‫We see it's more than 12. 47 00:03:54,260 --> 00:03:56,190 ‫So we know it goes right here. 48 00:03:56,210 --> 00:04:08,490 ‫And so for the last iteration we know it's five 12 21 33 47 and 100. 49 00:04:08,720 --> 00:04:12,680 ‫And so there you go you have a fully sorted list. 50 00:04:12,770 --> 00:04:22,940 ‫Now when you're dealing with six items this is absolutely fantastic algorithm to use when you get to 51 00:04:23,000 --> 00:04:27,310 ‫you know larger items you know over a thousand items or anything like that. 52 00:04:27,410 --> 00:04:29,300 ‫Then this is going to be really good. 53 00:04:29,300 --> 00:04:36,980 ‫This is essentially brute force type algorithm and so you're looking at every element and then you're 54 00:04:36,980 --> 00:04:39,110 ‫moving that element based on its balance. 55 00:04:39,140 --> 00:04:47,110 ‫We're going to get into some other algorithms such as mergesort and quicksort which are a lot more intelligent. 56 00:04:47,330 --> 00:04:54,050 ‫They have some pretty powerful mechanisms that make them perform much better and operate much faster 57 00:04:54,320 --> 00:04:56,340 ‫exponentially faster actually. 58 00:04:56,360 --> 00:05:02,900 ‫And so if you want to know the time complexity which if you're taking this for an algorithm class or 59 00:05:02,990 --> 00:05:08,660 ‫something like that and you definitely want to know the time complexity this actually had three different 60 00:05:08,660 --> 00:05:09,700 ‫complexities. 61 00:05:09,740 --> 00:05:15,120 ‫We have our best case. 62 00:05:15,340 --> 00:05:20,790 ‫We have our average and we have our worst 63 00:05:23,870 --> 00:05:31,820 ‫it's really good to know all three because with all three you're going to get different values. 64 00:05:31,820 --> 00:05:41,150 ‫Best case is actually going to the order of an average and worst are going to be the same. 65 00:05:41,150 --> 00:05:50,510 ‫They're going to be order in squared and order of squared. 66 00:05:50,800 --> 00:05:59,440 ‫Now if you watched my video talking about growth of functions you know that anything on this side of 67 00:05:59,440 --> 00:06:08,080 ‫the world is really a first especially for sorting is not a great way to go because these numbers get 68 00:06:08,080 --> 00:06:17,140 ‫very fast very quickly and you'll end up having some very slow poor performing type of programs so we 69 00:06:17,140 --> 00:06:19,930 ‫want to stay away from this for large items. 70 00:06:19,940 --> 00:06:29,230 ‫However the interesting thing is this best case this best case is actually better than some of the most 71 00:06:29,230 --> 00:06:32,150 ‫powerful algorithms out there. 72 00:06:32,380 --> 00:06:38,590 ‫Some things that are used for just gigantic applications this little insertion sort of rhythm actually 73 00:06:38,590 --> 00:06:42,260 ‫works better than those in certain cases. 74 00:06:42,280 --> 00:06:48,790 ‫And if you want to know that case it's actually important to think about it because if you really understand 75 00:06:48,790 --> 00:06:51,120 ‫now over them it'll make perfect sense. 76 00:06:51,730 --> 00:07:00,820 ‫If you notice how many times we had to iterate here we create a new list each time we went through but 77 00:07:00,820 --> 00:07:08,950 ‫we only have to do that when the item the next item down the list was less than the item to the left 78 00:07:09,100 --> 00:07:11,370 ‫when that wasn't the case. 79 00:07:11,420 --> 00:07:13,830 ‫Were able just to move right on to the next item. 80 00:07:13,840 --> 00:07:19,840 ‫So take for example five 2:47 we saw that 5 was already less than forty seven. 81 00:07:19,840 --> 00:07:21,130 ‫We didn't have to do anything. 82 00:07:21,220 --> 00:07:23,740 ‫We just continued right on to 12. 83 00:07:23,740 --> 00:07:37,170 ‫Now imagine if our list started out as 5 12 21 33 47 and 100. 84 00:07:37,180 --> 00:07:43,920 ‫In other words if we got a list that was already sorted it was already in sorted order. 85 00:07:43,960 --> 00:07:53,210 ‫That's when we would get this best case and the order of n just means that the speed or the time complexity. 86 00:07:53,380 --> 00:08:00,630 ‫All it takes is the amount of time it takes to get through the total number of items in the list. 87 00:08:00,700 --> 00:08:03,140 ‫And so that's incredibly fast. 88 00:08:03,160 --> 00:08:11,710 ‫The some of the other fast algorithms typically have a time complexity in law again which has a speed 89 00:08:11,710 --> 00:08:24,280 ‫that is actually rate right in this area in between these two type of complexities and the reason for 90 00:08:24,280 --> 00:08:32,710 ‫this is because the insertion sort is so basic it doesn't have a lot of different things like It doesn't 91 00:08:34,450 --> 00:08:40,870 ‫have a lot of big mathematical functions it doesn't do divide and conquer it's actually pretty basic 92 00:08:40,870 --> 00:08:42,670 ‫in how you implement it. 93 00:08:42,700 --> 00:08:53,100 ‫And so if you have a list of items that you know is either in order or it's very close to being in order. 94 00:08:53,130 --> 00:09:02,860 ‫A great example I heard is say that you are making a baking application and that 90 plus percent of 95 00:09:03,280 --> 00:09:09,740 ‫the items that you're getting for check numbers for the most part those are going to be either in order 96 00:09:09,760 --> 00:09:16,210 ‫as your application receives them or it's going to be very close to being in order insertion sort would 97 00:09:16,210 --> 00:09:22,510 ‫actually be a pretty good algorithm for that because when it takes items that are in order even a large 98 00:09:22,720 --> 00:09:31,240 ‫number of them it does very well with that because it only has to do it's more time consuming kind of 99 00:09:31,390 --> 00:09:34,580 ‫functions when it's having. 100 00:09:34,930 --> 00:09:38,180 ‫You know when it's actually moving the items in the array. 101 00:09:38,380 --> 00:09:45,700 ‫And so for Best case you can get to this if you know that you're dealing with data that is very close 102 00:09:45,700 --> 00:09:52,480 ‫to being sorted and you just kind of want to polish up the array to make sure it's in perfect sorted 103 00:09:52,480 --> 00:09:52,880 ‫order. 104 00:09:52,930 --> 00:09:54,160 ‫That's when you can use that. 105 00:09:54,190 --> 00:09:57,250 ‫Or if you're dealing with a very small number of items. 106 00:09:57,490 --> 00:10:07,560 ‫Worst case right over here the very worst case you could ever get on this was if the items were in reverse 107 00:10:07,580 --> 00:10:12,580 ‫sorted order and if you want to know the reason for that. 108 00:10:12,850 --> 00:10:21,330 ‫I mean just give us a little bit of room up here and just clean this up. 109 00:10:21,350 --> 00:10:29,080 ‫The reason for that is because if you're trying to get these all in the correct sorted order Michael 110 00:10:29,140 --> 00:10:31,630 ‫and I'll keep that right here. 111 00:10:31,820 --> 00:10:37,160 ‫You look at each item and you compare it with the item on the left. 112 00:10:37,160 --> 00:10:52,670 ‫Now what would happen if we had this in reverse sorted order 3 21 12 and 5 we would literally have to 113 00:10:52,700 --> 00:11:05,960 ‫create a new line for every single version so we have to create one that was forty seven hundred thirty 114 00:11:05,960 --> 00:11:15,470 ‫three 21 12 and 5 and then the next one we would have to do the exact same thing over and over and over 115 00:11:15,470 --> 00:11:16,360 ‫and over again. 116 00:11:16,520 --> 00:11:24,170 ‫And so you would have to go through the entire list and you'd have to iterate through every item where 117 00:11:24,170 --> 00:11:29,970 ‫you're essentially taking this hundred moving through each item until it's at the end. 118 00:11:30,080 --> 00:11:34,370 ‫Then you take the 47 and you do you'd be doing the exact same process. 119 00:11:34,370 --> 00:11:40,940 ‫So if you have something in reverse sorted order it or something in that side of the world insertion 120 00:11:40,940 --> 00:11:44,010 ‫sort is an absolutely horrible algorithm to use. 121 00:11:44,010 --> 00:11:49,730 ‫So you have to make sure that you do know the type of data that you're dealing with that's important 122 00:11:50,600 --> 00:11:52,270 ‫in any type of application. 123 00:11:52,280 --> 00:11:57,680 ‫But especially if you're working on sorting knowing your dataset is very important so please let me 124 00:11:57,680 --> 00:12:00,550 ‫know if you have any questions at all about insertion sort. 125 00:12:00,590 --> 00:12:02,070 ‫And I'll see in the next video. 13700

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.