All language subtitles for Data Structures and Algorithms in Python for Beginners - 2022 _ Great Learning - English

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi Download
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,080 --> 00:00:04,400 data structures and algorithms in python 2 00:00:02,560 --> 00:00:06,799 is a starter friendly course where 3 00:00:04,400 --> 00:00:08,880 learners can begin from very basic 4 00:00:06,799 --> 00:00:12,000 concepts like introduction to data 5 00:00:08,880 --> 00:00:15,440 structures types of data structures like 6 00:00:12,000 --> 00:00:18,240 array stack queue and linked list with 7 00:00:15,440 --> 00:00:20,880 introduction examples and implementation 8 00:00:18,240 --> 00:00:23,199 using python programming language 9 00:00:20,880 --> 00:00:25,920 proceeding with concepts you can also 10 00:00:23,199 --> 00:00:28,800 learn regarding tree data structures 11 00:00:25,920 --> 00:00:32,000 like binary tree binary search tree 12 00:00:28,800 --> 00:00:34,640 creation traversal and implementation 13 00:00:32,000 --> 00:00:35,920 followed by graph and hash tables are 14 00:00:34,640 --> 00:00:38,239 also taught 15 00:00:35,920 --> 00:00:40,719 coming to the algorithmic part we start 16 00:00:38,239 --> 00:00:43,600 with finding time and space complexity 17 00:00:40,719 --> 00:00:46,399 of algorithms searching and sorting 18 00:00:43,600 --> 00:00:49,200 concepts like binary search linear 19 00:00:46,399 --> 00:00:51,440 search incision sort and quick sort with 20 00:00:49,200 --> 00:00:53,840 implementation using python programming 21 00:00:51,440 --> 00:00:56,079 language are also included 22 00:00:53,840 --> 00:00:57,760 towards the end of this course you will 23 00:00:56,079 --> 00:01:00,000 learn and implement different 24 00:00:57,760 --> 00:01:02,399 programming approaches like divide and 25 00:01:00,000 --> 00:01:04,799 conquer greedy method and dynamic 26 00:01:02,399 --> 00:01:07,360 programming with examples like merge 27 00:01:04,799 --> 00:01:10,080 sort minimum spanning tree algorithm and 28 00:01:07,360 --> 00:01:13,600 towers of hanoi concepts using python 29 00:01:10,080 --> 00:01:15,360 programming language so why wait let's 30 00:01:13,600 --> 00:01:19,810 start learning data structures and 31 00:01:15,360 --> 00:01:23,779 algorithms in python on great learning 32 00:01:19,810 --> 00:01:23,779 [Music] 33 00:01:23,920 --> 00:01:27,600 if you haven't subscribed for our 34 00:01:25,520 --> 00:01:29,520 channel yet i would request you to hit 35 00:01:27,600 --> 00:01:32,400 the subscribe button and turn on 36 00:01:29,520 --> 00:01:35,119 notification bell so that you don't miss 37 00:01:32,400 --> 00:01:37,439 any new updates or video releases from 38 00:01:35,119 --> 00:01:40,320 great learning if you enjoy this video 39 00:01:37,439 --> 00:01:42,560 show us some love and do like this video 40 00:01:40,320 --> 00:01:44,560 knowledge increases by sharing so make 41 00:01:42,560 --> 00:01:46,880 sure you share this video with your 42 00:01:44,560 --> 00:01:49,600 friends and colleague make sure you 43 00:01:46,880 --> 00:01:52,320 comment on this video any queries or 44 00:01:49,600 --> 00:01:55,040 suggestions i'll be more than happy to 45 00:01:52,320 --> 00:01:56,799 respond to all of them hello everyone 46 00:01:55,040 --> 00:01:59,200 welcome to this video where we'll be 47 00:01:56,799 --> 00:02:02,560 learning regarding data structures and 48 00:01:59,200 --> 00:02:05,040 algorithms in python so python is a very 49 00:02:02,560 --> 00:02:07,439 well known programming language where 50 00:02:05,040 --> 00:02:10,399 we'll be using that in various domains 51 00:02:07,439 --> 00:02:12,560 so nowadays we are using python in data 52 00:02:10,399 --> 00:02:15,440 science in order to visualize data and 53 00:02:12,560 --> 00:02:17,920 analyze them and also various others 54 00:02:15,440 --> 00:02:21,360 naming artificial intelligence machine 55 00:02:17,920 --> 00:02:25,040 learning and web development also it is 56 00:02:21,360 --> 00:02:27,520 used as server sides crypto so knowing 57 00:02:25,040 --> 00:02:28,879 all about python so we have to know what 58 00:02:27,520 --> 00:02:30,959 are the different data structures and 59 00:02:28,879 --> 00:02:35,280 what are the algorithms which we 60 00:02:30,959 --> 00:02:37,840 can analyze and build using python so 61 00:02:35,280 --> 00:02:41,200 the agenda for this particular video 62 00:02:37,840 --> 00:02:44,000 would be will be first learning what is 63 00:02:41,200 --> 00:02:47,680 a data structure so introduction 64 00:02:44,000 --> 00:02:49,920 incomplete for data structure in python 65 00:02:47,680 --> 00:02:51,360 is given so followed by what are the 66 00:02:49,920 --> 00:02:54,000 inbuilt data structures which is 67 00:02:51,360 --> 00:02:55,840 available in python so python is a 68 00:02:54,000 --> 00:02:58,800 very good interactive programming 69 00:02:55,840 --> 00:02:59,599 language which also has its inbuilt and 70 00:02:58,800 --> 00:03:01,840 own 71 00:02:59,599 --> 00:03:04,560 kind of data structures in it so we'll 72 00:03:01,840 --> 00:03:07,519 be learning that with a tad bit of 73 00:03:04,560 --> 00:03:10,080 implementation and with simple examples 74 00:03:07,519 --> 00:03:13,360 so followed by will be starting 75 00:03:10,080 --> 00:03:15,599 data structures first then algorithms so 76 00:03:13,360 --> 00:03:18,239 arrays will be the first element which 77 00:03:15,599 --> 00:03:20,080 will be learning and then followed by 78 00:03:18,239 --> 00:03:22,159 stack and 79 00:03:20,080 --> 00:03:25,040 queue and again will be learning 80 00:03:22,159 --> 00:03:27,920 regarding linked list followed by trees 81 00:03:25,040 --> 00:03:31,680 and graphs so binary tree binary search 82 00:03:27,920 --> 00:03:35,440 tree graph hashing and later part would 83 00:03:31,680 --> 00:03:38,560 be algorithm analysis right so we'll be 84 00:03:35,440 --> 00:03:41,360 starting with linear and binary search 85 00:03:38,560 --> 00:03:44,000 and then we'll be learning sorting what 86 00:03:41,360 --> 00:03:48,000 are the different shots we can do and we 87 00:03:44,000 --> 00:03:49,120 also have a simple examples for all 88 00:03:48,000 --> 00:03:50,959 these 89 00:03:49,120 --> 00:03:53,920 topics which we are covering in this 90 00:03:50,959 --> 00:03:56,879 video and we also show implementation 91 00:03:53,920 --> 00:03:59,120 how can you put up into an ide with 92 00:03:56,879 --> 00:04:02,000 respect to python either it can be 93 00:03:59,120 --> 00:04:04,560 pycharm or it can be google collab or 94 00:04:02,000 --> 00:04:06,720 various other ids available for python 95 00:04:04,560 --> 00:04:09,439 so we'll be showing accordingly how can 96 00:04:06,720 --> 00:04:12,239 you implement these concepts in python 97 00:04:09,439 --> 00:04:13,200 so at last you have divide and conquer 98 00:04:12,239 --> 00:04:15,920 approach 99 00:04:13,200 --> 00:04:18,560 to programming right how do you divide 100 00:04:15,920 --> 00:04:20,639 and conquer so it is one kind of 101 00:04:18,560 --> 00:04:23,919 algorithm where you can just 102 00:04:20,639 --> 00:04:26,240 know how does it work and then it goes 103 00:04:23,919 --> 00:04:29,040 next with greedy approach and then 104 00:04:26,240 --> 00:04:31,840 ending with dynamic programming so all 105 00:04:29,040 --> 00:04:34,560 these topics are covered in this video 106 00:04:31,840 --> 00:04:36,400 in order to give you a basic and wide 107 00:04:34,560 --> 00:04:37,440 knowledge about the data structures and 108 00:04:36,400 --> 00:04:41,120 algorithms 109 00:04:37,440 --> 00:04:44,080 in python programming language so let's 110 00:04:41,120 --> 00:04:46,960 start this video with the first topic 111 00:04:44,080 --> 00:04:48,639 that is introduction to data structure 112 00:04:46,960 --> 00:04:50,400 the first thing that you really have to 113 00:04:48,639 --> 00:04:52,479 think about whenever the term data 114 00:04:50,400 --> 00:04:54,639 structure comes into your head is what 115 00:04:52,479 --> 00:04:56,639 is a data structure because it is a term 116 00:04:54,639 --> 00:04:58,240 that gets thrown around the entire world 117 00:04:56,639 --> 00:05:00,080 of programming and if you're a 118 00:04:58,240 --> 00:05:01,680 non-technical person who's ever gone for 119 00:05:00,080 --> 00:05:03,759 a technical interview either for a 120 00:05:01,680 --> 00:05:05,840 programming job or something along the 121 00:05:03,759 --> 00:05:08,720 lines of that data structures are 122 00:05:05,840 --> 00:05:10,560 extremely important for interviews and 123 00:05:08,720 --> 00:05:12,880 why is that right let's answer these 124 00:05:10,560 --> 00:05:15,120 questions first of all what is a data 125 00:05:12,880 --> 00:05:17,600 structure a data structure is basically 126 00:05:15,120 --> 00:05:19,759 a way of how we can store data in a 127 00:05:17,600 --> 00:05:22,240 structured manner in fact the answer 128 00:05:19,759 --> 00:05:24,800 lies in the name itself right so data 129 00:05:22,240 --> 00:05:26,800 structures not only provide a very good 130 00:05:24,800 --> 00:05:28,880 and an efficient way to actually store 131 00:05:26,800 --> 00:05:31,199 your data but also whenever it is 132 00:05:28,880 --> 00:05:33,199 required right that is when it shows its 133 00:05:31,199 --> 00:05:35,759 power that is when it brings out all the 134 00:05:33,199 --> 00:05:37,600 efficiency that i just spoke about now 135 00:05:35,759 --> 00:05:39,360 and then when you think about it having 136 00:05:37,600 --> 00:05:41,039 a structured way of approaching things 137 00:05:39,360 --> 00:05:42,800 not only in the world of programming but 138 00:05:41,039 --> 00:05:45,039 works everywhere right think about 139 00:05:42,800 --> 00:05:47,440 organizing your books in a bookshelf or 140 00:05:45,039 --> 00:05:49,120 maybe a kitchen cabinet right but then 141 00:05:47,440 --> 00:05:50,639 when you when you again think about data 142 00:05:49,120 --> 00:05:52,720 structures as a whole when you come to 143 00:05:50,639 --> 00:05:54,479 programming languages there's varieties 144 00:05:52,720 --> 00:05:56,880 of data structures right 145 00:05:54,479 --> 00:05:58,479 in your book rack you'll only have books 146 00:05:56,880 --> 00:06:00,400 there's a good chance in your kitchen 147 00:05:58,479 --> 00:06:02,479 cabinet you might have only stuff that's 148 00:06:00,400 --> 00:06:03,680 required for your kitchen right but in a 149 00:06:02,479 --> 00:06:05,680 data structure when you're thinking 150 00:06:03,680 --> 00:06:07,680 about a programming language here things 151 00:06:05,680 --> 00:06:09,440 will slightly change and there is a good 152 00:06:07,680 --> 00:06:11,759 chance that there can be a lot more 153 00:06:09,440 --> 00:06:13,680 things at play right think about not 154 00:06:11,759 --> 00:06:16,639 only how to store data but how to 155 00:06:13,680 --> 00:06:18,880 organize them how to access them and how 156 00:06:16,639 --> 00:06:21,360 efficient is this entire process of 157 00:06:18,880 --> 00:06:25,039 storing organizing and accessing going 158 00:06:21,360 --> 00:06:27,840 to be right this is what makes up a data 159 00:06:25,039 --> 00:06:29,360 structure to give you an example just 160 00:06:27,840 --> 00:06:31,440 like the book example that i just told 161 00:06:29,360 --> 00:06:32,960 you about think about organizing fruits 162 00:06:31,440 --> 00:06:35,840 in a basket right because this is 163 00:06:32,960 --> 00:06:37,440 exactly what uh happens in our case as 164 00:06:35,840 --> 00:06:39,440 well right look at your screen right now 165 00:06:37,440 --> 00:06:41,120 you'll see a couple of bananas apples 166 00:06:39,440 --> 00:06:43,199 watermelons and mangoes that are 167 00:06:41,120 --> 00:06:45,039 arranged in their own baskets right now 168 00:06:43,199 --> 00:06:47,600 if everything was jumbled if i asked you 169 00:06:45,039 --> 00:06:49,199 to put it back like that you can do it 170 00:06:47,600 --> 00:06:51,280 right exactly 171 00:06:49,199 --> 00:06:53,039 now in the case of a data structure in 172 00:06:51,280 --> 00:06:54,880 python what happens is the data that it 173 00:06:53,039 --> 00:06:56,960 sees is usually numbers or character 174 00:06:54,880 --> 00:06:59,280 data right now if it's an integer it can 175 00:06:56,960 --> 00:07:01,039 be one two three four five if it's a 176 00:06:59,280 --> 00:07:03,840 floating point number it can be three 177 00:07:01,039 --> 00:07:06,720 point one four four point nine six point 178 00:07:03,840 --> 00:07:08,639 nine five point eight anything like that 179 00:07:06,720 --> 00:07:11,039 right and then of course if you have any 180 00:07:08,639 --> 00:07:13,680 sort of string data for example hi my 181 00:07:11,039 --> 00:07:16,160 name is anirudh rao right 182 00:07:13,680 --> 00:07:18,639 so uh just similar to organizing these 183 00:07:16,160 --> 00:07:21,280 there also we each have baskets where if 184 00:07:18,639 --> 00:07:23,039 python sees a data which happens to be 185 00:07:21,280 --> 00:07:25,360 either integer floating or anything like 186 00:07:23,039 --> 00:07:28,479 that it will automatically try to put it 187 00:07:25,360 --> 00:07:30,560 in these baskets that we have now to 188 00:07:28,479 --> 00:07:32,639 dive right into the details of it to 189 00:07:30,560 --> 00:07:34,240 understand what exactly is it that we're 190 00:07:32,639 --> 00:07:37,280 doing with respect to the data 191 00:07:34,240 --> 00:07:38,880 structures that are present uh in python 192 00:07:37,280 --> 00:07:40,319 and many other programming languages 193 00:07:38,880 --> 00:07:42,319 ladies and gentlemen you have to think 194 00:07:40,319 --> 00:07:44,080 about two ways of how data structures 195 00:07:42,319 --> 00:07:46,319 are meant to be used and of course the 196 00:07:44,080 --> 00:07:47,759 purpose of it first of all you have 197 00:07:46,319 --> 00:07:49,840 built-in data structures and 198 00:07:47,759 --> 00:07:51,520 user-defined data structures see enough 199 00:07:49,840 --> 00:07:53,759 built-in data structures as the name 200 00:07:51,520 --> 00:07:56,080 itself suggests right it comes built in 201 00:07:53,759 --> 00:07:58,000 with the programming languages it talks 202 00:07:56,080 --> 00:08:00,639 about something fundamental it talks 203 00:07:58,000 --> 00:08:03,120 about a scaffolding structure or it 204 00:08:00,639 --> 00:08:05,840 talks about an already ready syntax that 205 00:08:03,120 --> 00:08:07,759 you guys have to use to just ensure that 206 00:08:05,840 --> 00:08:09,199 you know your storage options are being 207 00:08:07,759 --> 00:08:11,520 met and of course whatever it is that 208 00:08:09,199 --> 00:08:12,960 you want to use the storage for you have 209 00:08:11,520 --> 00:08:14,639 time to concentrate on those 210 00:08:12,960 --> 00:08:17,199 applications rather than breaking your 211 00:08:14,639 --> 00:08:19,199 head on how to store the data right so 212 00:08:17,199 --> 00:08:20,639 that's built in data structures uh user 213 00:08:19,199 --> 00:08:22,879 defined data structures are some of 214 00:08:20,639 --> 00:08:24,800 these data structures which we require 215 00:08:22,879 --> 00:08:26,879 in a case where we have 216 00:08:24,800 --> 00:08:28,879 not just a lot of data but sometimes you 217 00:08:26,879 --> 00:08:30,720 have a solution which will require you 218 00:08:28,879 --> 00:08:32,959 to take a non-linear approach of how you 219 00:08:30,720 --> 00:08:34,959 should not only store data but process 220 00:08:32,959 --> 00:08:37,279 them and derive insights out of them in 221 00:08:34,959 --> 00:08:39,919 those cases right user-defined data 222 00:08:37,279 --> 00:08:41,440 structures are very much useful now if 223 00:08:39,919 --> 00:08:42,800 you take a look at all the built-in data 224 00:08:41,440 --> 00:08:45,040 structures and the user-defined data 225 00:08:42,800 --> 00:08:46,240 structures especially we have in python 226 00:08:45,040 --> 00:08:47,920 you'll see that the built-in data 227 00:08:46,240 --> 00:08:49,600 structures of course apart from the in 228 00:08:47,920 --> 00:08:52,880 float string and all those you'll see 229 00:08:49,600 --> 00:08:54,320 list dictionary tuple and set and then 230 00:08:52,880 --> 00:08:55,839 take a look at user-defined data 231 00:08:54,320 --> 00:08:57,440 structures use user-defined data 232 00:08:55,839 --> 00:08:59,680 structures i've given you four on the 233 00:08:57,440 --> 00:09:01,519 screen right now but you can have any 234 00:08:59,680 --> 00:09:03,040 number of user-defined data structures 235 00:09:01,519 --> 00:09:05,040 for that programming language right for 236 00:09:03,040 --> 00:09:07,680 example python itself has a lot more 237 00:09:05,040 --> 00:09:09,680 than four but these four happen to be so 238 00:09:07,680 --> 00:09:12,399 so popular that you guys have to know 239 00:09:09,680 --> 00:09:14,160 about them so all right guys now we will 240 00:09:12,399 --> 00:09:16,800 take a look at all the built-in data 241 00:09:14,160 --> 00:09:19,200 structures that we just uh discussed 242 00:09:16,800 --> 00:09:21,120 right now whenever the talk is about 243 00:09:19,200 --> 00:09:23,279 building data structures the first 244 00:09:21,120 --> 00:09:26,080 question you really must ask yourself is 245 00:09:23,279 --> 00:09:27,600 what is a built-in data structure or why 246 00:09:26,080 --> 00:09:28,800 is it even called a built-in data 247 00:09:27,600 --> 00:09:31,680 structure right now a built-in data 248 00:09:28,800 --> 00:09:33,839 structure basically is present natively 249 00:09:31,680 --> 00:09:35,120 in the python library as well right so 250 00:09:33,839 --> 00:09:36,480 when you take a look at all these 251 00:09:35,120 --> 00:09:37,680 various uh 252 00:09:36,480 --> 00:09:40,240 you know built-in structures that we're 253 00:09:37,680 --> 00:09:42,399 discussing be it lists be tuple set 254 00:09:40,240 --> 00:09:44,720 dictionary whatever it is uh all of 255 00:09:42,399 --> 00:09:46,080 these are present directly uh for usage 256 00:09:44,720 --> 00:09:48,399 right you really do not have to install 257 00:09:46,080 --> 00:09:50,080 anything on top of python or you know 258 00:09:48,399 --> 00:09:52,240 work with any sort of dependencies to 259 00:09:50,080 --> 00:09:54,000 work with these as well right fantastic 260 00:09:52,240 --> 00:09:56,080 now the first data structure that you 261 00:09:54,000 --> 00:09:56,959 see on your screen is lists so what are 262 00:09:56,080 --> 00:09:58,880 lessons 263 00:09:56,959 --> 00:10:00,000 list as the name itself suggests you 264 00:09:58,880 --> 00:10:01,600 know whenever you're trying to make a 265 00:10:00,000 --> 00:10:03,600 list of things what are you trying to do 266 00:10:01,600 --> 00:10:05,519 you are sequentially trying to organize 267 00:10:03,600 --> 00:10:06,959 something in an orderly fashion or 268 00:10:05,519 --> 00:10:08,720 you're trying to arrange something in a 269 00:10:06,959 --> 00:10:10,079 fashion where the next time you take a 270 00:10:08,720 --> 00:10:12,240 look at the list you understand what's 271 00:10:10,079 --> 00:10:14,560 going on similarly right it's not the 272 00:10:12,240 --> 00:10:16,880 exact analogy but similarly here as well 273 00:10:14,560 --> 00:10:19,200 with respect to list we have a storage 274 00:10:16,880 --> 00:10:21,600 in the form of an array-like structure 275 00:10:19,200 --> 00:10:23,600 right uh with respect to list in python 276 00:10:21,600 --> 00:10:25,600 the advantage here is that every single 277 00:10:23,600 --> 00:10:27,600 element in the list right all of these 278 00:10:25,600 --> 00:10:29,600 positions are indexed so if i wanted the 279 00:10:27,600 --> 00:10:31,440 first element the second element the 280 00:10:29,600 --> 00:10:33,440 last element or any element that is 281 00:10:31,440 --> 00:10:35,680 present in an entire list i can actually 282 00:10:33,440 --> 00:10:37,600 use its index position it's addressing 283 00:10:35,680 --> 00:10:39,040 and eventually start picking that up 284 00:10:37,600 --> 00:10:40,640 right in fact there's an example on your 285 00:10:39,040 --> 00:10:43,279 screen right now we have a sample list 286 00:10:40,640 --> 00:10:44,800 here which contains of uh you know all 287 00:10:43,279 --> 00:10:46,320 the types of data that we can have right 288 00:10:44,800 --> 00:10:48,240 we can have integers we can have 289 00:10:46,320 --> 00:10:50,800 floating point numbers and look at this 290 00:10:48,240 --> 00:10:52,560 ladies and gentlemen we can also have a 291 00:10:50,800 --> 00:10:54,240 string data type right and of course all 292 00:10:52,560 --> 00:10:56,399 of these are completely 293 00:10:54,240 --> 00:10:57,760 valid and all of these are output as an 294 00:10:56,399 --> 00:10:59,600 actual list as well so this is something 295 00:10:57,760 --> 00:11:00,399 you really have to know in terms of 296 00:10:59,600 --> 00:11:01,600 lists 297 00:11:00,399 --> 00:11:03,920 the next thing that we're going to take 298 00:11:01,600 --> 00:11:05,600 a look at and discuss is dictionaries 299 00:11:03,920 --> 00:11:07,200 now think about a dictionary at your 300 00:11:05,600 --> 00:11:08,800 home right before even understanding 301 00:11:07,200 --> 00:11:10,399 what's present on the screen if you take 302 00:11:08,800 --> 00:11:12,800 a look at a dictionary if i have to 303 00:11:10,399 --> 00:11:14,720 search for a word in that dictionary 304 00:11:12,800 --> 00:11:16,160 how do i go about moving around the 305 00:11:14,720 --> 00:11:18,160 dictionary right if my word usually 306 00:11:16,160 --> 00:11:20,079 starts from s i go to that index 307 00:11:18,160 --> 00:11:21,760 position i open all the pages that start 308 00:11:20,079 --> 00:11:23,760 from s and then i alphabetically start 309 00:11:21,760 --> 00:11:25,519 looking for the word right 310 00:11:23,760 --> 00:11:27,680 the working of dictionary in python is 311 00:11:25,519 --> 00:11:29,440 also similar where we take the concept 312 00:11:27,680 --> 00:11:31,600 from the actual example that i just told 313 00:11:29,440 --> 00:11:33,200 you right so we have something called as 314 00:11:31,600 --> 00:11:35,360 key items and we have something called 315 00:11:33,200 --> 00:11:37,680 as the values that correspond to these 316 00:11:35,360 --> 00:11:40,240 key items right uh think about uh 317 00:11:37,680 --> 00:11:42,240 structured data right maybe your name uh 318 00:11:40,240 --> 00:11:43,760 your phone number and your con and your 319 00:11:42,240 --> 00:11:45,120 email address and your aadhar card 320 00:11:43,760 --> 00:11:46,800 number let's just say now if you have to 321 00:11:45,120 --> 00:11:49,360 list all of these for your entire family 322 00:11:46,800 --> 00:11:51,200 members uh you will have it in an order 323 00:11:49,360 --> 00:11:52,720 or even if it's unordered uh you will 324 00:11:51,200 --> 00:11:54,560 have it let's just say there are four 325 00:11:52,720 --> 00:11:56,240 people there right so you can pick up 326 00:11:54,560 --> 00:11:58,000 each one of the four as and when you 327 00:11:56,240 --> 00:11:59,279 require right similarly here in 328 00:11:58,000 --> 00:12:01,440 dictionaries we have something called as 329 00:11:59,279 --> 00:12:03,120 a key and a key is basically associated 330 00:12:01,440 --> 00:12:05,120 with the value so if you have to access 331 00:12:03,120 --> 00:12:06,560 the value you can just look for the key 332 00:12:05,120 --> 00:12:08,800 and eventually the key is hooked up to 333 00:12:06,560 --> 00:12:10,720 the value where you can find out uh the 334 00:12:08,800 --> 00:12:13,120 data that is stored uh in the place 335 00:12:10,720 --> 00:12:14,959 that's mapped for that key right uh so 336 00:12:13,120 --> 00:12:16,800 if you're wondering about these pairings 337 00:12:14,959 --> 00:12:18,639 how many pairings can we have or can 338 00:12:16,800 --> 00:12:20,480 they be accessed or can they be added as 339 00:12:18,639 --> 00:12:22,160 and when required of course with respect 340 00:12:20,480 --> 00:12:25,680 to dictionaries all these key value 341 00:12:22,160 --> 00:12:27,839 pairs can be added accessed modified and 342 00:12:25,680 --> 00:12:29,600 removed as in when you choose that's a 343 00:12:27,839 --> 00:12:31,200 huge advantage here right now take a 344 00:12:29,600 --> 00:12:32,480 look at the example on your screen now 345 00:12:31,200 --> 00:12:34,480 in fact here we have a dictionary which 346 00:12:32,480 --> 00:12:36,720 contains of two items when i say two 347 00:12:34,480 --> 00:12:38,800 items i'm meaning two key value pairs 348 00:12:36,720 --> 00:12:41,040 the first uh the first item that we're 349 00:12:38,800 --> 00:12:42,880 talking about is basically an apple but 350 00:12:41,040 --> 00:12:44,639 the key associated with respect to apple 351 00:12:42,880 --> 00:12:46,160 is first the second item that we're 352 00:12:44,639 --> 00:12:48,320 talking about is ball but the key 353 00:12:46,160 --> 00:12:50,320 associated with ball is in second right 354 00:12:48,320 --> 00:12:52,959 that's the output that you can see so i 355 00:12:50,320 --> 00:12:54,560 hope you're clear with dictionaries the 356 00:12:52,959 --> 00:12:56,240 next interesting data structure that 357 00:12:54,560 --> 00:12:58,000 we're going to take a look at in python 358 00:12:56,240 --> 00:13:00,240 has to be double now ladies and 359 00:12:58,000 --> 00:13:02,880 gentlemen tuple is literally the most 360 00:13:00,240 --> 00:13:04,800 used data structure we have in python 361 00:13:02,880 --> 00:13:05,920 now that we just took a look at list one 362 00:13:04,800 --> 00:13:08,160 thing you really have to understand 363 00:13:05,920 --> 00:13:10,639 about tuple is that the working is very 364 00:13:08,160 --> 00:13:12,480 very similar to what the lists provide 365 00:13:10,639 --> 00:13:14,000 us but then but then ladies and 366 00:13:12,480 --> 00:13:16,480 gentlemen this is a very important point 367 00:13:14,000 --> 00:13:18,480 but here in terms of a tuple you really 368 00:13:16,480 --> 00:13:19,839 cannot change the value of that 369 00:13:18,480 --> 00:13:23,120 particular tuple where it is in 370 00:13:19,839 --> 00:13:25,120 execution or when it is underway so 371 00:13:23,120 --> 00:13:27,040 if i have a tuple called a sample tuple 372 00:13:25,120 --> 00:13:28,880 one two three and then if i want to 373 00:13:27,040 --> 00:13:30,399 change it while it's executing or if i 374 00:13:28,880 --> 00:13:32,720 want to change it and uh you know just 375 00:13:30,399 --> 00:13:35,760 add in more details or remove elements 376 00:13:32,720 --> 00:13:38,079 as such that is not possible why because 377 00:13:35,760 --> 00:13:41,120 tuple is an immutable data type when i 378 00:13:38,079 --> 00:13:43,519 say immutable basically it means uh that 379 00:13:41,120 --> 00:13:45,040 you cannot add any more data into that 380 00:13:43,519 --> 00:13:46,720 particular topic so if i have to create 381 00:13:45,040 --> 00:13:48,480 a tuple called one two three four 382 00:13:46,720 --> 00:13:50,480 instead of adding it to the sample tuple 383 00:13:48,480 --> 00:13:52,639 here i'll create another tuple and add 384 00:13:50,480 --> 00:13:55,279 that particular value and discard the 385 00:13:52,639 --> 00:13:57,760 one that i have at hand right so the 386 00:13:55,279 --> 00:14:00,079 working is very similar uh to that of 387 00:13:57,760 --> 00:14:02,079 lists and again lists use square back 388 00:14:00,079 --> 00:14:04,480 brackets for all the people who want a 389 00:14:02,079 --> 00:14:05,519 shortcut to remember lists sets and 390 00:14:04,480 --> 00:14:08,560 dictionaries right when you guys are 391 00:14:05,519 --> 00:14:10,800 beginning a list uses a square bracket a 392 00:14:08,560 --> 00:14:13,279 dictionary uses a flower bracket and a 393 00:14:10,800 --> 00:14:15,040 tuple uses a round bracket right so i 394 00:14:13,279 --> 00:14:17,440 hope you're clear with these three data 395 00:14:15,040 --> 00:14:19,519 structures now let's take a look at the 396 00:14:17,440 --> 00:14:22,320 fourth one which again uses flower 397 00:14:19,519 --> 00:14:24,800 brackets and this one is called as a set 398 00:14:22,320 --> 00:14:26,399 set uh the name itself should definitely 399 00:14:24,800 --> 00:14:27,920 take you back to your school days where 400 00:14:26,399 --> 00:14:29,040 you might have learned about these 401 00:14:27,920 --> 00:14:31,279 concepts of 402 00:14:29,040 --> 00:14:33,519 sets and how you should have an ordered 403 00:14:31,279 --> 00:14:36,079 collection of unique elements present 404 00:14:33,519 --> 00:14:37,839 right so if i have a set uh you know one 405 00:14:36,079 --> 00:14:40,480 two two three three four four five five 406 00:14:37,839 --> 00:14:42,720 six six the entire output of it will 407 00:14:40,480 --> 00:14:44,720 actually be one two three four five six 408 00:14:42,720 --> 00:14:47,360 right that's how a set works in a set 409 00:14:44,720 --> 00:14:50,079 you cannot have repeated elements every 410 00:14:47,360 --> 00:14:52,560 single element that is present in a set 411 00:14:50,079 --> 00:14:53,839 should be 100 unique to each other right 412 00:14:52,560 --> 00:14:55,839 in fact look at the example on your 413 00:14:53,839 --> 00:14:57,920 screen right now first set is the name 414 00:14:55,839 --> 00:14:59,760 of our set of course and again similar 415 00:14:57,920 --> 00:15:01,440 to dictionaries here we use flower 416 00:14:59,760 --> 00:15:03,600 braces as well but the difference here 417 00:15:01,440 --> 00:15:05,120 is that we do not have key value pairs 418 00:15:03,600 --> 00:15:07,120 if you had the same flower bracket 419 00:15:05,120 --> 00:15:08,320 structure where you had a key value pair 420 00:15:07,120 --> 00:15:09,920 immediately that would become a 421 00:15:08,320 --> 00:15:11,440 dictionary but since we don't have that 422 00:15:09,920 --> 00:15:12,880 and we have individual elements you can 423 00:15:11,440 --> 00:15:15,360 take a look at it and you can think of 424 00:15:12,880 --> 00:15:16,639 it as a set right in fact the example 425 00:15:15,360 --> 00:15:18,959 that i just spoke to you about is on the 426 00:15:16,639 --> 00:15:21,199 screen one two three four four and five 427 00:15:18,959 --> 00:15:23,040 so four is repeated right uh but in the 428 00:15:21,199 --> 00:15:25,920 output you can see that four is present 429 00:15:23,040 --> 00:15:28,160 only once so no matter how many repeated 430 00:15:25,920 --> 00:15:29,839 elements or you have in your set once 431 00:15:28,160 --> 00:15:31,680 you try to perform operations on it or 432 00:15:29,839 --> 00:15:33,440 once you try to actually store it in the 433 00:15:31,680 --> 00:15:35,759 form of a set and try to access the 434 00:15:33,440 --> 00:15:37,519 output of it you will only have access 435 00:15:35,759 --> 00:15:40,160 to the unique elements that are present 436 00:15:37,519 --> 00:15:42,639 in that particular set right fantastic 437 00:15:40,160 --> 00:15:44,240 now this was about the built-in data 438 00:15:42,639 --> 00:15:45,920 structures we checked out list we 439 00:15:44,240 --> 00:15:48,639 checked out the dictionaries we checked 440 00:15:45,920 --> 00:15:51,040 out tuples and we also checked out sets 441 00:15:48,639 --> 00:15:53,199 right remember the brackets remember 442 00:15:51,040 --> 00:15:54,880 what's immutable and understand that for 443 00:15:53,199 --> 00:15:56,560 all of these data types you do not have 444 00:15:54,880 --> 00:15:58,000 to install any sort of dependency to 445 00:15:56,560 --> 00:15:59,920 work with right 446 00:15:58,000 --> 00:16:01,360 now let's take a look at user defined 447 00:15:59,920 --> 00:16:03,519 data structures 448 00:16:01,360 --> 00:16:05,120 when the name itself suggests user 449 00:16:03,519 --> 00:16:06,959 defined data structures what are we 450 00:16:05,120 --> 00:16:08,800 trying to do here right 451 00:16:06,959 --> 00:16:09,920 for example let's talk about these uh 452 00:16:08,800 --> 00:16:12,240 the data structure that you see on your 453 00:16:09,920 --> 00:16:14,000 screen right now stack right think about 454 00:16:12,240 --> 00:16:16,000 a stack of books again a stack of books 455 00:16:14,000 --> 00:16:18,320 is shown on your screen right there how 456 00:16:16,000 --> 00:16:19,680 would you uh arrange arrange a couple of 457 00:16:18,320 --> 00:16:21,040 books let's just say you have 10 books 458 00:16:19,680 --> 00:16:22,560 how would you arrange it in the form of 459 00:16:21,040 --> 00:16:25,759 a stack if you just have to put it on 460 00:16:22,560 --> 00:16:27,680 one top of the other right i'll be there 461 00:16:25,759 --> 00:16:29,600 so how would you arrange books in a 462 00:16:27,680 --> 00:16:33,040 stack right you would obviously take it 463 00:16:29,600 --> 00:16:35,040 and put it one on top of the other now 464 00:16:33,040 --> 00:16:36,480 this actually has a principle of 465 00:16:35,040 --> 00:16:38,720 approach and how you are actually 466 00:16:36,480 --> 00:16:40,880 accessing and removing uh elements or in 467 00:16:38,720 --> 00:16:42,800 this case books right if you've kept all 468 00:16:40,880 --> 00:16:44,800 those books on the ground or maybe on 469 00:16:42,800 --> 00:16:46,880 your bed or maybe on your table there's 470 00:16:44,800 --> 00:16:48,480 only one way that you can have complete 471 00:16:46,880 --> 00:16:50,480 access to the book that is the top book 472 00:16:48,480 --> 00:16:51,920 right you can you have one two books and 473 00:16:50,480 --> 00:16:54,320 you can access the top one and you can 474 00:16:51,920 --> 00:16:56,480 start picking up the book from the top 475 00:16:54,320 --> 00:16:59,120 to the bottom right this concept is 476 00:16:56,480 --> 00:17:00,399 called as last in first out or in short 477 00:16:59,120 --> 00:17:02,639 you might have already heard about it 478 00:17:00,399 --> 00:17:04,880 it's called as le4 right 479 00:17:02,639 --> 00:17:06,880 so if i have a stack of books or if i 480 00:17:04,880 --> 00:17:09,199 have a stack of elements in python what 481 00:17:06,880 --> 00:17:11,039 can i do with it well you can actually 482 00:17:09,199 --> 00:17:13,839 add any sort of data you require to the 483 00:17:11,039 --> 00:17:16,400 stack remove any sort of data you can 484 00:17:13,839 --> 00:17:17,919 access it change it modify it work with 485 00:17:16,400 --> 00:17:20,000 when you are adding an element into a 486 00:17:17,919 --> 00:17:22,079 stack the operation is called as pushing 487 00:17:20,000 --> 00:17:24,000 and when you're removing a data element 488 00:17:22,079 --> 00:17:26,160 from a stack or in fact even a queue as 489 00:17:24,000 --> 00:17:28,240 well it's called as popping the 490 00:17:26,160 --> 00:17:29,600 operation that goes into it right but 491 00:17:28,240 --> 00:17:31,600 one thing ladies and gentlemen you have 492 00:17:29,600 --> 00:17:33,760 to think about a stack is that once 493 00:17:31,600 --> 00:17:35,760 you've stacked it on the ground on your 494 00:17:33,760 --> 00:17:37,840 bed uh you know beat on the table or 495 00:17:35,760 --> 00:17:39,679 wherever it is you cannot start pulling 496 00:17:37,840 --> 00:17:41,600 out books from the last right you have 497 00:17:39,679 --> 00:17:44,000 to hold everything else and manually 498 00:17:41,600 --> 00:17:45,760 yank it out forcefully so this concept 499 00:17:44,000 --> 00:17:47,760 does not apply here in python so the 500 00:17:45,760 --> 00:17:49,360 data access if you if you want to remove 501 00:17:47,760 --> 00:17:50,799 the third book on your screen if you 502 00:17:49,360 --> 00:17:52,080 have to remove this book on your screen 503 00:17:50,799 --> 00:17:54,080 you basically have to remove the first 504 00:17:52,080 --> 00:17:56,080 one remove the second one remove the 505 00:17:54,080 --> 00:17:57,919 third book and then put these back into 506 00:17:56,080 --> 00:18:00,080 its place right so it's not like you'll 507 00:17:57,919 --> 00:18:01,679 just divide it into two things pull that 508 00:18:00,080 --> 00:18:03,600 book out and put it back again like how 509 00:18:01,679 --> 00:18:06,400 you would probably see in a cartoon or 510 00:18:03,600 --> 00:18:09,280 so that's not how it works if you want a 511 00:18:06,400 --> 00:18:11,200 concept that works in that way the next 512 00:18:09,280 --> 00:18:12,960 uh user-defined data structure will be 513 00:18:11,200 --> 00:18:15,200 pleasing to you guys because this is a 514 00:18:12,960 --> 00:18:18,160 queue a cue data structure is something 515 00:18:15,200 --> 00:18:19,440 that uh literally every one of you guys 516 00:18:18,160 --> 00:18:21,679 watching this particular video in fact 517 00:18:19,440 --> 00:18:24,000 all of us when i say including me have 518 00:18:21,679 --> 00:18:25,440 seen and either been a part of a queue 519 00:18:24,000 --> 00:18:27,919 or of course you know we know how it 520 00:18:25,440 --> 00:18:29,919 works right so cues are again used in 521 00:18:27,919 --> 00:18:31,679 real life a lot the principle here is 522 00:18:29,919 --> 00:18:34,160 slightly different from the stack here 523 00:18:31,679 --> 00:18:36,480 we use this concept called as fifo first 524 00:18:34,160 --> 00:18:38,400 in first out right that's how an actual 525 00:18:36,480 --> 00:18:40,400 queue works whoever comes first gets 526 00:18:38,400 --> 00:18:42,480 priority they get served and then they 527 00:18:40,400 --> 00:18:44,799 go out and the next person walks in 528 00:18:42,480 --> 00:18:46,240 right uh you can see the ah you can see 529 00:18:44,799 --> 00:18:48,000 the effect on the image that's shown on 530 00:18:46,240 --> 00:18:50,240 your screen right now now whenever 531 00:18:48,000 --> 00:18:51,840 you're taking a look at cues though uh 532 00:18:50,240 --> 00:18:53,919 maybe let's just say the fourth person 533 00:18:51,840 --> 00:18:55,440 in this queue forgot her car keys or 534 00:18:53,919 --> 00:18:57,120 forgot her mobile for another anything 535 00:18:55,440 --> 00:18:59,600 can she just walk out of the queue from 536 00:18:57,120 --> 00:19:01,520 the back as well is it possible well of 537 00:18:59,600 --> 00:19:03,520 course it is possible right uh the 538 00:19:01,520 --> 00:19:04,559 advantage of queue compared to stack is 539 00:19:03,520 --> 00:19:06,880 that here 540 00:19:04,559 --> 00:19:09,440 operations can be performed both from 541 00:19:06,880 --> 00:19:10,960 the front and the back when i say front 542 00:19:09,440 --> 00:19:13,120 and the back what i'm trying to mean is 543 00:19:10,960 --> 00:19:14,799 the head and the tail right for example 544 00:19:13,120 --> 00:19:16,880 the head of the queue is the first 545 00:19:14,799 --> 00:19:19,039 person who was uh who was there to be 546 00:19:16,880 --> 00:19:20,960 attended to right the tail of the queue 547 00:19:19,039 --> 00:19:23,200 will involve what it will involve the 548 00:19:20,960 --> 00:19:25,039 last person in that queue so basically 549 00:19:23,200 --> 00:19:27,120 including all of these things operations 550 00:19:25,039 --> 00:19:28,720 can be performed either from the top or 551 00:19:27,120 --> 00:19:30,400 you can actually take the tail section 552 00:19:28,720 --> 00:19:33,039 and you can start either removing data 553 00:19:30,400 --> 00:19:35,600 or adding data from there as well so 554 00:19:33,039 --> 00:19:36,480 this is how a general queue works and 555 00:19:35,600 --> 00:19:38,559 when you're thinking about 556 00:19:36,480 --> 00:19:40,720 implementation in python as well 557 00:19:38,559 --> 00:19:42,480 remember one thing people first and 558 00:19:40,720 --> 00:19:44,080 first out so depending on your object 559 00:19:42,480 --> 00:19:46,480 axis depending on what's actually 560 00:19:44,080 --> 00:19:48,960 required you will be either thinking of 561 00:19:46,480 --> 00:19:51,280 using stacks or queues in most of these 562 00:19:48,960 --> 00:19:53,039 situations right now that we're clear 563 00:19:51,280 --> 00:19:54,480 with stacks and queues uh i'm gonna take 564 00:19:53,039 --> 00:19:56,400 a look at another interesting data 565 00:19:54,480 --> 00:19:58,799 structure here another user defined data 566 00:19:56,400 --> 00:20:00,240 structure here it's called as trees 567 00:19:58,799 --> 00:20:02,080 right now 568 00:20:00,240 --> 00:20:04,000 when we've been talking about these user 569 00:20:02,080 --> 00:20:05,520 defined data structures what we're 570 00:20:04,000 --> 00:20:08,400 trying to do is we're trying to use the 571 00:20:05,520 --> 00:20:10,880 syntax and the logic of python to build 572 00:20:08,400 --> 00:20:12,240 ourselves our own data structure that is 573 00:20:10,880 --> 00:20:14,240 the reason why all of these data 574 00:20:12,240 --> 00:20:15,919 structures are called user-defined data 575 00:20:14,240 --> 00:20:18,720 structures i am sure you guys figured 576 00:20:15,919 --> 00:20:20,000 that out by now right fantastic right so 577 00:20:18,720 --> 00:20:22,080 when we're talking about a tree data 578 00:20:20,000 --> 00:20:24,159 structure again think of an actual tree 579 00:20:22,080 --> 00:20:25,919 right in an actual tree you find root 580 00:20:24,159 --> 00:20:27,840 you find the leaves and of course you 581 00:20:25,919 --> 00:20:29,360 find uh you know all the branches and 582 00:20:27,840 --> 00:20:30,640 all of that now try to see if you can 583 00:20:29,360 --> 00:20:33,200 take a look at the image on your left 584 00:20:30,640 --> 00:20:36,640 and find any of those right let me help 585 00:20:33,200 --> 00:20:38,640 you out this one uh the node this is a 586 00:20:36,640 --> 00:20:40,640 top node uh basically from this 587 00:20:38,640 --> 00:20:42,159 particular node we have all the other 588 00:20:40,640 --> 00:20:43,679 nodes that are coming out right so this 589 00:20:42,159 --> 00:20:46,080 can be considered as a root this can be 590 00:20:43,679 --> 00:20:48,240 considered as the primary and from that 591 00:20:46,080 --> 00:20:50,480 whenever you see every other node into 592 00:20:48,240 --> 00:20:52,159 it there is a concept of uh 593 00:20:50,480 --> 00:20:55,200 you know the parent relationships that 594 00:20:52,159 --> 00:20:57,440 we build here for example uh 2 and 3 the 595 00:20:55,200 --> 00:21:00,320 nodes 2 and 3 have their parent as 1 596 00:20:57,440 --> 00:21:02,559 because from 1 you got 2 and 3 right 597 00:21:00,320 --> 00:21:04,640 similarly for two now four and five 598 00:21:02,559 --> 00:21:05,520 become the children of two similarly for 599 00:21:04,640 --> 00:21:07,840 five 600 00:21:05,520 --> 00:21:10,159 similarly for five a six seven and eight 601 00:21:07,840 --> 00:21:12,240 become the children of five getting it 602 00:21:10,159 --> 00:21:13,840 right now one important thing that you 603 00:21:12,240 --> 00:21:15,679 really have to understand is that when 604 00:21:13,840 --> 00:21:17,600 you're working from the root node all 605 00:21:15,679 --> 00:21:19,200 the way to the leaf nodes now what i 606 00:21:17,600 --> 00:21:21,520 mean by leaf nodes is basically all 607 00:21:19,200 --> 00:21:23,360 these nodes which do not have another 608 00:21:21,520 --> 00:21:24,320 connector node let's take a look at what 609 00:21:23,360 --> 00:21:26,240 they are 610 00:21:24,320 --> 00:21:28,960 are we having any other nodes connected 611 00:21:26,240 --> 00:21:31,200 to 3 no so that's a leaf node similarly 612 00:21:28,960 --> 00:21:33,440 for four similarly for six similarly for 613 00:21:31,200 --> 00:21:35,200 seven and eight right so most of the 614 00:21:33,440 --> 00:21:37,200 results of these computation that we 615 00:21:35,200 --> 00:21:40,080 perform when using a tree data structure 616 00:21:37,200 --> 00:21:41,760 usually lies in these leaf nodes aspect 617 00:21:40,080 --> 00:21:44,159 of it right they are the last nodes but 618 00:21:41,760 --> 00:21:45,840 hey in our case they are some really 619 00:21:44,159 --> 00:21:48,480 important nodes that we are going to 620 00:21:45,840 --> 00:21:50,320 have to use right so breaking up a 621 00:21:48,480 --> 00:21:52,320 problem into a solution where you 622 00:21:50,320 --> 00:21:54,880 require a data structure which needs to 623 00:21:52,320 --> 00:21:57,120 be broken down in a compounded form 624 00:21:54,880 --> 00:21:58,400 right so if you have if you have a very 625 00:21:57,120 --> 00:22:00,960 complex data set and you're gonna break 626 00:21:58,400 --> 00:22:02,400 it into modular chunks uh and use it as 627 00:22:00,960 --> 00:22:04,080 a solution eventually one of those 628 00:22:02,400 --> 00:22:06,159 modules will give you a solution the 629 00:22:04,080 --> 00:22:08,799 tree data structure is exactly what you 630 00:22:06,159 --> 00:22:09,840 guys should use right fantastic the next 631 00:22:08,799 --> 00:22:12,159 data structure that we're going to take 632 00:22:09,840 --> 00:22:14,320 a look at is the graph data structure 633 00:22:12,159 --> 00:22:15,760 now again we've been using graphs and 634 00:22:14,320 --> 00:22:17,760 i'm sure you guys have seen graphs you 635 00:22:15,760 --> 00:22:19,600 have worked on graphs beat school be it 636 00:22:17,760 --> 00:22:21,679 college or you guys might have seen the 637 00:22:19,600 --> 00:22:24,240 rise in the coronavirus spread that's a 638 00:22:21,679 --> 00:22:25,760 graph as well right so 639 00:22:24,240 --> 00:22:28,080 we're talking about graph as a data 640 00:22:25,760 --> 00:22:30,480 visualization entity there but in terms 641 00:22:28,080 --> 00:22:32,159 of python what are we doing well here 642 00:22:30,480 --> 00:22:34,400 graph is a data structure which 643 00:22:32,159 --> 00:22:36,400 basically has a structure which is very 644 00:22:34,400 --> 00:22:38,240 similar to a tree but then the working 645 00:22:36,400 --> 00:22:40,159 of a graph is very different because 646 00:22:38,240 --> 00:22:41,840 this sometimes is a closed loop 647 00:22:40,159 --> 00:22:43,600 structure which has a collection of 648 00:22:41,840 --> 00:22:45,039 nodes and edges now you already know 649 00:22:43,600 --> 00:22:46,320 what are the nodes right so what are the 650 00:22:45,039 --> 00:22:50,000 nodes that you see on your screen right 651 00:22:46,320 --> 00:22:51,679 now 0 1 2 3 and 4. what are the edges 652 00:22:50,000 --> 00:22:54,559 that you see on your screen right now 653 00:22:51,679 --> 00:22:56,640 well in between 0 and 1 there is a an 654 00:22:54,559 --> 00:22:59,360 edge between 0 and 4 there is an edge 655 00:22:56,640 --> 00:23:01,280 between 4 and 1 there is an edge right 656 00:22:59,360 --> 00:23:03,200 now even though i directly do not have 657 00:23:01,280 --> 00:23:04,799 an edge from 4 to 2 658 00:23:03,200 --> 00:23:06,320 we go through 3 and we still have an 659 00:23:04,799 --> 00:23:08,400 edge there right 660 00:23:06,320 --> 00:23:10,080 this is the concept of nodes uh you know 661 00:23:08,400 --> 00:23:11,440 nodes are also called as vertices in 662 00:23:10,080 --> 00:23:13,120 case that's what you might have learned 663 00:23:11,440 --> 00:23:15,280 in your school of college or in fact 664 00:23:13,120 --> 00:23:18,960 edges are also called as arcs right so 665 00:23:15,280 --> 00:23:20,720 regarding vertices arcs nodes edges you 666 00:23:18,960 --> 00:23:22,880 have to understand and you have to make 667 00:23:20,720 --> 00:23:25,440 sure that you do not get confused in 668 00:23:22,880 --> 00:23:27,360 these concepts so as i told you a graph 669 00:23:25,440 --> 00:23:30,640 is a closed connection of all of these 670 00:23:27,360 --> 00:23:32,880 uh nodes and edges so can i have an a 671 00:23:30,640 --> 00:23:35,039 can have n number of nodes here if i'm 672 00:23:32,880 --> 00:23:37,919 talking about a graph data structure in 673 00:23:35,039 --> 00:23:40,640 python it has to consist of a finite set 674 00:23:37,919 --> 00:23:43,039 of nodes and edges only then it is 675 00:23:40,640 --> 00:23:44,960 called as a graph data structure right 676 00:23:43,039 --> 00:23:47,279 now think about a fantastic example of 677 00:23:44,960 --> 00:23:49,279 where this exact data structure is used 678 00:23:47,279 --> 00:23:51,279 in fact it's facebook facebook is a 679 00:23:49,279 --> 00:23:53,919 fantastic social media that all of us if 680 00:23:51,279 --> 00:23:55,919 not most of us know and use how do you 681 00:23:53,919 --> 00:23:57,679 think each of the individual people 682 00:23:55,919 --> 00:23:59,760 present in the facebook's network get 683 00:23:57,679 --> 00:24:01,919 connected to others right when there is 684 00:23:59,760 --> 00:24:03,919 a connection from one person to another 685 00:24:01,919 --> 00:24:05,840 think of it as an edge but then these 686 00:24:03,919 --> 00:24:07,520 two people sitting in correlation with 687 00:24:05,840 --> 00:24:09,360 what we're discussing right now are the 688 00:24:07,520 --> 00:24:10,880 nodes right so you have nodes you have 689 00:24:09,360 --> 00:24:12,720 edges and now think about all the 690 00:24:10,880 --> 00:24:14,960 billions of people that are on facebook 691 00:24:12,720 --> 00:24:17,279 right it just becomes an entirely uh 692 00:24:14,960 --> 00:24:19,760 different uh network it comes it becomes 693 00:24:17,279 --> 00:24:21,440 a very super uh complex intertwined web 694 00:24:19,760 --> 00:24:24,320 but then i had to really simplify it for 695 00:24:21,440 --> 00:24:26,000 you to talk to you about graph in uh in 696 00:24:24,320 --> 00:24:28,240 terms of facebook right but yes this is 697 00:24:26,000 --> 00:24:30,000 used in facebook and if you break it 698 00:24:28,240 --> 00:24:31,919 down to its most simplest levels you 699 00:24:30,000 --> 00:24:34,480 definitely will see this data structure 700 00:24:31,919 --> 00:24:37,120 used there and in thousands of other 701 00:24:34,480 --> 00:24:38,960 places i'm sure right guys now i have 702 00:24:37,120 --> 00:24:40,799 one more bonus data structure that i 703 00:24:38,960 --> 00:24:42,880 want to talk to you about this is again 704 00:24:40,799 --> 00:24:44,400 the favorite of a lot of interviewers 705 00:24:42,880 --> 00:24:46,240 out there so if you're a non-technical 706 00:24:44,400 --> 00:24:48,320 person who's had a chance to attend a 707 00:24:46,240 --> 00:24:50,320 technical interview there is a very good 708 00:24:48,320 --> 00:24:51,600 chance you'll be quizzed on first of all 709 00:24:50,320 --> 00:24:53,200 everything that we have learned until 710 00:24:51,600 --> 00:24:54,400 now secondly there's a good chance 711 00:24:53,200 --> 00:24:56,400 there's a question that's going to come 712 00:24:54,400 --> 00:24:58,080 up if you're looking for a python if 713 00:24:56,400 --> 00:25:01,200 you're looking for a carrier in python c 714 00:24:58,080 --> 00:25:02,240 c plus plus c sharp or java 99 of the 715 00:25:01,200 --> 00:25:04,320 time they're gonna ask you a question 716 00:25:02,240 --> 00:25:05,919 saying what are linked lists 717 00:25:04,320 --> 00:25:08,320 all right so to answer that question 718 00:25:05,919 --> 00:25:10,640 link list is another user defined data 719 00:25:08,320 --> 00:25:12,799 structure that we have uh here in python 720 00:25:10,640 --> 00:25:15,120 of course we do not have native support 721 00:25:12,799 --> 00:25:16,960 for a link list in python as in we just 722 00:25:15,120 --> 00:25:18,960 saw how we can work with list how we can 723 00:25:16,960 --> 00:25:21,679 work with sets and all of those right we 724 00:25:18,960 --> 00:25:24,080 have a valid syntax to use that but to 725 00:25:21,679 --> 00:25:25,760 implement linked list in terms of python 726 00:25:24,080 --> 00:25:27,440 you actually do not have it in the 727 00:25:25,760 --> 00:25:29,440 standard library so you have to use 728 00:25:27,440 --> 00:25:31,600 functions you have to use other lists 729 00:25:29,440 --> 00:25:33,840 and you have to use manual logic to 730 00:25:31,600 --> 00:25:35,919 actually implement a linked list first 731 00:25:33,840 --> 00:25:38,000 of all what is a linked list well a 732 00:25:35,919 --> 00:25:40,400 linked list is basically a collection of 733 00:25:38,000 --> 00:25:42,480 elements tied up in a chain structure 734 00:25:40,400 --> 00:25:44,400 right it's basically linked again look 735 00:25:42,480 --> 00:25:46,400 the answer lies in the name of the data 736 00:25:44,400 --> 00:25:48,400 structure itself a linked list is 737 00:25:46,400 --> 00:25:50,159 basically a sequence of elements that 738 00:25:48,400 --> 00:25:52,320 are connected to each other in a way 739 00:25:50,159 --> 00:25:54,640 where you can move around and access one 740 00:25:52,320 --> 00:25:56,880 or the other element as and when 741 00:25:54,640 --> 00:25:58,480 required now if i have three elements 742 00:25:56,880 --> 00:26:01,200 that you can see on your screen right 5 743 00:25:58,480 --> 00:26:03,520 10 and 20 how how do i even know that 744 00:26:01,200 --> 00:26:06,159 the location that holds the value 5 is 745 00:26:03,520 --> 00:26:08,400 able to talk to the value 10 right this 746 00:26:06,159 --> 00:26:10,320 is where a simple concept of pointers 747 00:26:08,400 --> 00:26:13,360 are used i am sure you guys know what 748 00:26:10,320 --> 00:26:15,600 pointers are right pointers are nothing 749 00:26:13,360 --> 00:26:17,679 but the simple addressing system that we 750 00:26:15,600 --> 00:26:19,679 use uh the address that we attach to a 751 00:26:17,679 --> 00:26:22,000 variable so that even though we do not 752 00:26:19,679 --> 00:26:23,600 know the value or where the value lies 753 00:26:22,000 --> 00:26:25,760 as soon as we have the address in our 754 00:26:23,600 --> 00:26:27,120 hand the second part of what i just said 755 00:26:25,760 --> 00:26:28,960 will negate itself we will have an 756 00:26:27,120 --> 00:26:32,320 address we know where that particular 757 00:26:28,960 --> 00:26:34,400 element 5 10 or 20 exists right so uh if 758 00:26:32,320 --> 00:26:36,159 you just take this uh particular entity 759 00:26:34,400 --> 00:26:38,159 here five and there will be another 760 00:26:36,159 --> 00:26:40,080 criteria here called as next so next is 761 00:26:38,159 --> 00:26:42,240 basically the name of the pointer you 762 00:26:40,080 --> 00:26:44,720 would point that to where the data 763 00:26:42,240 --> 00:26:46,400 element of 10 exists that is how the 764 00:26:44,720 --> 00:26:47,840 linked list works again there's multiple 765 00:26:46,400 --> 00:26:50,240 types of linked list there's a single 766 00:26:47,840 --> 00:26:51,760 linked list there's doubly linked list 767 00:26:50,240 --> 00:26:53,600 you know there's a circular linked list 768 00:26:51,760 --> 00:26:55,919 so there's many many types and it can 769 00:26:53,600 --> 00:26:58,320 get really complex but one thing that 770 00:26:55,919 --> 00:27:00,799 we've seen a lot is that uh in terms of 771 00:26:58,320 --> 00:27:02,400 python linked lists are not super 772 00:27:00,799 --> 00:27:04,240 popular because you again have a lot of 773 00:27:02,400 --> 00:27:06,000 other data structures uh that you can 774 00:27:04,240 --> 00:27:08,400 try to use and you can try to have you 775 00:27:06,000 --> 00:27:10,400 can build the same application uh using 776 00:27:08,400 --> 00:27:11,840 something else uh where uh you know a 777 00:27:10,400 --> 00:27:13,520 link list might have been a good fit 778 00:27:11,840 --> 00:27:15,760 right so implementation has to be done 779 00:27:13,520 --> 00:27:17,440 manually but then even though it has to 780 00:27:15,760 --> 00:27:19,279 be done manually even though it's not in 781 00:27:17,440 --> 00:27:21,360 a standard library this is a super 782 00:27:19,279 --> 00:27:24,480 important data structure that again uh 783 00:27:21,360 --> 00:27:26,159 is used asked and worked with by all uh 784 00:27:24,480 --> 00:27:28,480 the professionals out there so make sure 785 00:27:26,159 --> 00:27:30,159 you guys know your linked lists right 786 00:27:28,480 --> 00:27:32,640 now let's go ahead and work with some 787 00:27:30,159 --> 00:27:34,000 non-primitive data structures in python 788 00:27:32,640 --> 00:27:36,080 so the main non-primitive data 789 00:27:34,000 --> 00:27:38,640 structures in python are tuple list 790 00:27:36,080 --> 00:27:40,960 dictionary and set so when it comes to 791 00:27:38,640 --> 00:27:44,240 all of these data structures so you can 792 00:27:40,960 --> 00:27:46,720 store multiple elements inside one data 793 00:27:44,240 --> 00:27:49,039 structure so till now we work with only 794 00:27:46,720 --> 00:27:51,840 variables and when it came to variables 795 00:27:49,039 --> 00:27:53,840 we could store only one value in one 796 00:27:51,840 --> 00:27:55,679 variable now let's say if you go to a 797 00:27:53,840 --> 00:27:57,600 music concert and you'd have to store 798 00:27:55,679 --> 00:28:00,240 the names of all the people attending 799 00:27:57,600 --> 00:28:02,880 the music concert and the strength of 800 00:28:00,240 --> 00:28:05,120 the audience would be around 10 000. so 801 00:28:02,880 --> 00:28:07,520 to store the names you would require 10 802 00:28:05,120 --> 00:28:09,120 000 variables now that's a lot of 803 00:28:07,520 --> 00:28:11,679 variables and you'd have to manually 804 00:28:09,120 --> 00:28:13,840 store all of these names inside 10 000 805 00:28:11,679 --> 00:28:15,919 variables so that's a humongous task and 806 00:28:13,840 --> 00:28:17,600 that's a tiresome task so this is where 807 00:28:15,919 --> 00:28:19,360 you would need these data structures 808 00:28:17,600 --> 00:28:21,760 where you can store multiple elements 809 00:28:19,360 --> 00:28:22,799 inside one single data structure so 810 00:28:21,760 --> 00:28:25,279 we'll start with the first data 811 00:28:22,799 --> 00:28:27,120 structure which is basically tuple so 812 00:28:25,279 --> 00:28:29,279 when it comes to tuple it is an ordered 813 00:28:27,120 --> 00:28:31,760 collection of elements enclosed within 814 00:28:29,279 --> 00:28:33,760 round braces so we have curious jason 815 00:28:31,760 --> 00:28:36,320 over here and he'll be coming in between 816 00:28:33,760 --> 00:28:39,279 to give us some pointers so curious 817 00:28:36,320 --> 00:28:41,200 jason tells us that tuples are immutable 818 00:28:39,279 --> 00:28:43,600 now what does he mean when he states 819 00:28:41,200 --> 00:28:46,240 that tuples are immutable so basically 820 00:28:43,600 --> 00:28:48,480 when you go ahead and create a tuple so 821 00:28:46,240 --> 00:28:52,399 let's say you add five elements inside 822 00:28:48,480 --> 00:28:54,799 one tuple now you can't change or modify 823 00:28:52,399 --> 00:28:56,559 the tuple which you have created so that 824 00:28:54,799 --> 00:28:58,640 would basically mean that let's say if 825 00:28:56,559 --> 00:29:00,480 you have five elements then you can't 826 00:28:58,640 --> 00:29:02,799 change the value of the second element 827 00:29:00,480 --> 00:29:04,559 or the third element or you can't add 828 00:29:02,799 --> 00:29:06,559 another element inside the tuple which 829 00:29:04,559 --> 00:29:08,799 you have already created so this is 830 00:29:06,559 --> 00:29:10,799 basically the immutability nature of 831 00:29:08,799 --> 00:29:13,440 tuples and this is how you can create a 832 00:29:10,799 --> 00:29:14,720 tuple so you will use these round braces 833 00:29:13,440 --> 00:29:17,039 over here and inside these you will 834 00:29:14,720 --> 00:29:18,559 given the values right so as you see 835 00:29:17,039 --> 00:29:20,399 tuple is actually your heterogeneous 836 00:29:18,559 --> 00:29:22,240 data structure so over here you are 837 00:29:20,399 --> 00:29:24,880 storing a numerical value a character 838 00:29:22,240 --> 00:29:26,240 value and a boolean value right so let's 839 00:29:24,880 --> 00:29:28,399 go to jupyter notebook and work with 840 00:29:26,240 --> 00:29:31,120 tuples 841 00:29:28,399 --> 00:29:33,760 so let me create a tuple 842 00:29:31,120 --> 00:29:37,559 so i'll name the tuple as up1 843 00:29:33,760 --> 00:29:37,559 and i'll given the values 844 00:29:48,960 --> 00:29:52,960 right so this is our tuple top1 845 00:29:51,679 --> 00:29:55,039 now let's see how can we access 846 00:29:52,960 --> 00:29:57,120 individual elements from this tuple so 847 00:29:55,039 --> 00:29:59,520 accessing elements from tuple is the 848 00:29:57,120 --> 00:30:00,640 same as axing elements from a string so 849 00:29:59,520 --> 00:30:02,960 all you do is given the name of the 850 00:30:00,640 --> 00:30:04,640 tuple which is top1 you're given this 851 00:30:02,960 --> 00:30:06,159 square braces and then if you want to 852 00:30:04,640 --> 00:30:08,480 extract the first element which is 853 00:30:06,159 --> 00:30:10,720 basically present at index 0 854 00:30:08,480 --> 00:30:12,559 you will type in 0 over here 855 00:30:10,720 --> 00:30:14,399 and you have extracted the first element 856 00:30:12,559 --> 00:30:15,919 which is 1. similarly if you want to 857 00:30:14,399 --> 00:30:17,520 extract the last element you have to 858 00:30:15,919 --> 00:30:19,039 type in -1 859 00:30:17,520 --> 00:30:21,760 and you have extracted the last element 860 00:30:19,039 --> 00:30:23,679 which is 23 and if you want to extract a 861 00:30:21,760 --> 00:30:26,720 sequence of elements so let's say i want 862 00:30:23,679 --> 00:30:29,440 a true and b so this would be from index 863 00:30:26,720 --> 00:30:33,039 number one going on till index number 864 00:30:29,440 --> 00:30:33,919 four because we'd want one two and three 865 00:30:33,039 --> 00:30:35,840 so 866 00:30:33,919 --> 00:30:40,159 i'll type in 867 00:30:35,840 --> 00:30:41,360 1 colon 4 right so i've extracted a true 868 00:30:40,159 --> 00:30:42,960 and b 869 00:30:41,360 --> 00:30:45,600 right so this is how you can extract 870 00:30:42,960 --> 00:30:48,159 individual elements from a tuple now as 871 00:30:45,600 --> 00:30:50,240 i've told you a tuple is immutable so 872 00:30:48,159 --> 00:30:52,640 let me go ahead and try to change the 873 00:30:50,240 --> 00:30:55,279 value which is present inside a tuple so 874 00:30:52,640 --> 00:30:58,159 i'll type in tup1 and let me change the 875 00:30:55,279 --> 00:31:03,600 value which is presented zero with index 876 00:30:58,159 --> 00:31:05,600 and i'll change it to let's say 100 877 00:31:03,600 --> 00:31:07,760 and we get an error over here so the 878 00:31:05,600 --> 00:31:10,000 error is tuple object does not support 879 00:31:07,760 --> 00:31:11,919 item assignment and that is because 880 00:31:10,000 --> 00:31:14,799 tuples are immutable 881 00:31:11,919 --> 00:31:17,440 now let me try to add a new value inside 882 00:31:14,799 --> 00:31:19,120 this so we've got one two three four 883 00:31:17,440 --> 00:31:21,519 five so that would mean this is index 884 00:31:19,120 --> 00:31:24,000 number four so let me try to add 885 00:31:21,519 --> 00:31:26,000 something at index number five 886 00:31:24,000 --> 00:31:28,960 i'll type in 887 00:31:26,000 --> 00:31:28,960 harry potter 888 00:31:29,679 --> 00:31:33,279 again we get the same error so tuple 889 00:31:31,600 --> 00:31:35,360 object does not support item assignment 890 00:31:33,279 --> 00:31:36,720 because of this immutable 891 00:31:35,360 --> 00:31:39,279 now we'll head on to the next data 892 00:31:36,720 --> 00:31:41,039 structure which is a list so lists and 893 00:31:39,279 --> 00:31:43,120 tuples are quite similar the only 894 00:31:41,039 --> 00:31:45,679 difference is less immutable while 895 00:31:43,120 --> 00:31:48,399 tuples are immutable now what do i mean 896 00:31:45,679 --> 00:31:50,799 when i say lists are mutable so this 897 00:31:48,399 --> 00:31:52,799 means that lists can be changed once 898 00:31:50,799 --> 00:31:54,960 they are created so there are five 899 00:31:52,799 --> 00:31:57,519 elements in a list so the second element 900 00:31:54,960 --> 00:31:58,720 or the third element can be modified and 901 00:31:57,519 --> 00:32:00,799 again since i've said that there are 902 00:31:58,720 --> 00:32:03,360 five elements so another five elements 903 00:32:00,799 --> 00:32:05,440 can be added inside this list so this is 904 00:32:03,360 --> 00:32:07,200 the basic difference between lists and 905 00:32:05,440 --> 00:32:09,039 tuples and this is how you can create a 906 00:32:07,200 --> 00:32:10,559 list so you'll basically given square 907 00:32:09,039 --> 00:32:12,960 braces and you'll given all of the 908 00:32:10,559 --> 00:32:14,399 values inside the square braces so let's 909 00:32:12,960 --> 00:32:16,960 head on to jupiter notebook and work 910 00:32:14,399 --> 00:32:16,960 with lists 911 00:32:18,320 --> 00:32:24,679 so i'll type in l1 and i'll given some 912 00:32:21,039 --> 00:32:24,679 values over here 913 00:32:33,039 --> 00:32:37,200 and then i'll print it out 914 00:32:35,200 --> 00:32:39,200 all right so this is our list over here 915 00:32:37,200 --> 00:32:40,799 and accessing elements from the list is 916 00:32:39,200 --> 00:32:42,799 same as tuple 917 00:32:40,799 --> 00:32:45,120 so if i want to access the first element 918 00:32:42,799 --> 00:32:47,279 i'll type in l1 i'll given square braces 919 00:32:45,120 --> 00:32:49,519 and then i'll given zero so i have 920 00:32:47,279 --> 00:32:50,960 extracted the first element now let's go 921 00:32:49,519 --> 00:32:53,200 ahead and access our sequence of 922 00:32:50,960 --> 00:32:55,600 elements so i want to access these three 923 00:32:53,200 --> 00:32:57,519 elements over here true two and nb so 924 00:32:55,600 --> 00:32:59,760 this is index number two and then going 925 00:32:57,519 --> 00:33:04,080 on till index number five 926 00:32:59,760 --> 00:33:06,240 so l one and two colon five 927 00:33:04,080 --> 00:33:07,840 all right so true two and b so we have 928 00:33:06,240 --> 00:33:10,159 successfully extracted these three 929 00:33:07,840 --> 00:33:12,080 sequence of elements now let me go ahead 930 00:33:10,159 --> 00:33:15,600 and actually change the values inside 931 00:33:12,080 --> 00:33:19,679 this list so i'll change this value 1 to 932 00:33:15,600 --> 00:33:23,919 100 so i'll type in l1 0 and i'll assign 933 00:33:19,679 --> 00:33:25,919 the value of 100 now let me print out l1 934 00:33:23,919 --> 00:33:27,919 so as you see initially the value of 935 00:33:25,919 --> 00:33:30,320 this element was one and then we have 936 00:33:27,919 --> 00:33:32,000 changed the value to 100. now let's say 937 00:33:30,320 --> 00:33:34,679 i want to add a couple more elements 938 00:33:32,000 --> 00:33:36,640 inside this list so i'll type in 939 00:33:34,679 --> 00:33:38,559 l1.append and 940 00:33:36,640 --> 00:33:41,279 i'll add an element and name it to be 941 00:33:38,559 --> 00:33:44,159 let's say sword 942 00:33:41,279 --> 00:33:45,919 and then let me print out l1 943 00:33:44,159 --> 00:33:46,880 right so i've successfully added a new 944 00:33:45,919 --> 00:33:49,039 element 945 00:33:46,880 --> 00:33:51,440 so now i can actually add a list inside 946 00:33:49,039 --> 00:33:53,360 a list so let me again use the append 947 00:33:51,440 --> 00:33:55,679 function over here 948 00:33:53,360 --> 00:33:57,200 and then i'll add a list inside this so 949 00:33:55,679 --> 00:33:59,840 i'll type in 950 00:33:57,200 --> 00:34:01,360 one two three four and five 951 00:33:59,840 --> 00:34:03,519 so let me 952 00:34:01,360 --> 00:34:05,440 again look at l1 953 00:34:03,519 --> 00:34:07,279 right so i successfully added this new 954 00:34:05,440 --> 00:34:09,040 list inside this list 955 00:34:07,279 --> 00:34:10,800 now we can also go ahead and remove 956 00:34:09,040 --> 00:34:13,040 these elements so to remove the last 957 00:34:10,800 --> 00:34:16,560 element we've got the pop function so 958 00:34:13,040 --> 00:34:19,119 i'll type in l1.pop 959 00:34:16,560 --> 00:34:21,280 so as we see this last element has been 960 00:34:19,119 --> 00:34:23,839 popped out now again let me type in 961 00:34:21,280 --> 00:34:25,919 l1.pop and let's see what will be popped 962 00:34:23,839 --> 00:34:28,000 out right so this time this element 963 00:34:25,919 --> 00:34:29,839 sword has been popped out 964 00:34:28,000 --> 00:34:31,839 now let me have a look at the modified 965 00:34:29,839 --> 00:34:34,720 list right so we are only left with 966 00:34:31,839 --> 00:34:36,879 these values because word and this list 967 00:34:34,720 --> 00:34:39,040 has been popped out so this is how we 968 00:34:36,879 --> 00:34:40,639 can work with lists 969 00:34:39,040 --> 00:34:42,480 now let's head on to the next data 970 00:34:40,639 --> 00:34:44,159 structure which is a dictionary so 971 00:34:42,480 --> 00:34:46,240 dictionary is actually quite different 972 00:34:44,159 --> 00:34:48,879 than a list and a tuple because it 973 00:34:46,240 --> 00:34:50,879 consists of key value pairs and these 974 00:34:48,879 --> 00:34:53,200 are actually unordered collection of key 975 00:34:50,879 --> 00:34:55,520 value pairs which are enclosed within 976 00:34:53,200 --> 00:34:57,680 curly braces and again dictionaries are 977 00:34:55,520 --> 00:34:59,680 mutable so this is how you can create a 978 00:34:57,680 --> 00:35:01,440 dictionary so you will put in curly 979 00:34:59,680 --> 00:35:03,359 braces over here so first you'll put in 980 00:35:01,440 --> 00:35:05,040 the key so over here the key is apple 981 00:35:03,359 --> 00:35:07,440 and then you'll give it a colon and then 982 00:35:05,040 --> 00:35:10,079 you'll given the value so for the key 983 00:35:07,440 --> 00:35:12,079 apple the value is 10 and then you're 984 00:35:10,079 --> 00:35:14,000 given the next key value pair so after 985 00:35:12,079 --> 00:35:16,800 come up the next key value pair is 986 00:35:14,000 --> 00:35:18,640 orange 20. so orange is the key and 20 987 00:35:16,800 --> 00:35:21,200 is the value so let's go ahead and 988 00:35:18,640 --> 00:35:22,640 create our own dictionary 989 00:35:21,200 --> 00:35:25,040 so let me create the dictionary over 990 00:35:22,640 --> 00:35:27,359 here i'll type in d1 and then i'll give 991 00:35:25,040 --> 00:35:29,280 them these curly braces over here 992 00:35:27,359 --> 00:35:30,720 so i'll just stop given some names of 993 00:35:29,280 --> 00:35:33,599 fruits 994 00:35:30,720 --> 00:35:34,960 so let's say the first key is mango and 995 00:35:33,599 --> 00:35:39,359 the price of the fruit would be the 996 00:35:34,960 --> 00:35:41,839 value so mango costs 45 bucks 997 00:35:39,359 --> 00:35:45,920 and then we've got apple 998 00:35:41,839 --> 00:35:47,680 so let's say apples cost us 30 999 00:35:45,920 --> 00:35:51,920 and then we've got 1000 00:35:47,680 --> 00:35:54,480 orange and then orange costs around 77. 1001 00:35:51,920 --> 00:35:59,119 after that we've got guava 1002 00:35:54,480 --> 00:36:01,680 and then this would cost around 125 1003 00:35:59,119 --> 00:36:03,760 now let me print it out 1004 00:36:01,680 --> 00:36:06,640 so this is our dictionary 1005 00:36:03,760 --> 00:36:09,359 now what we have is key value pairs so 1006 00:36:06,640 --> 00:36:11,520 if i want to access only the keys then 1007 00:36:09,359 --> 00:36:14,400 we have the keys function so i'll type 1008 00:36:11,520 --> 00:36:16,079 in d1 dot keys so i'll given the name of 1009 00:36:14,400 --> 00:36:17,839 the dictionary and then i'll follow it 1010 00:36:16,079 --> 00:36:20,000 up with the keys function 1011 00:36:17,839 --> 00:36:22,320 diva.keys right so i have extracted all 1012 00:36:20,000 --> 00:36:24,480 of the keys which are mango apple orange 1013 00:36:22,320 --> 00:36:26,320 and guava similarly if i want to extract 1014 00:36:24,480 --> 00:36:28,640 all of the values which are basically 1015 00:36:26,320 --> 00:36:31,599 the price of all of these fruits i'll 1016 00:36:28,640 --> 00:36:34,000 type in d1 dot values 1017 00:36:31,599 --> 00:36:35,680 and i have extracted the prices of all 1018 00:36:34,000 --> 00:36:37,839 of these fruits 1019 00:36:35,680 --> 00:36:40,079 now let's go ahead and modify some of 1020 00:36:37,839 --> 00:36:42,000 these elements so i want to change the 1021 00:36:40,079 --> 00:36:44,800 cost of mango 1022 00:36:42,000 --> 00:36:46,640 so i'll type in d1 and then i'll given 1023 00:36:44,800 --> 00:36:50,079 the name of key 1024 00:36:46,640 --> 00:36:52,560 so the name of the key is mango and the 1025 00:36:50,079 --> 00:36:54,960 cost would be 100 1026 00:36:52,560 --> 00:36:57,200 and then i'll type in d1 1027 00:36:54,960 --> 00:36:59,839 so initially the cost of mango was 45 1028 00:36:57,200 --> 00:37:01,440 and then i've altered it to be 100 so 1029 00:36:59,839 --> 00:37:03,040 this is how we can work with 1030 00:37:01,440 --> 00:37:04,400 dictionaries 1031 00:37:03,040 --> 00:37:06,960 and then we've got the final data 1032 00:37:04,400 --> 00:37:09,200 structure which is a set so a set is an 1033 00:37:06,960 --> 00:37:12,720 unordered and unindexed collection of 1034 00:37:09,200 --> 00:37:15,200 elements enclosed within curly braces so 1035 00:37:12,720 --> 00:37:17,920 what do i mean by unindexed so when i 1036 00:37:15,200 --> 00:37:20,640 say unindexed this basically means that 1037 00:37:17,920 --> 00:37:22,640 sets don't have any index to it so we 1038 00:37:20,640 --> 00:37:24,720 can't really access the elements with 1039 00:37:22,640 --> 00:37:27,119 respect to their indexing and also 1040 00:37:24,720 --> 00:37:29,359 another thing to be noted is sets do not 1041 00:37:27,119 --> 00:37:32,000 allow duplicate values so let's say if 1042 00:37:29,359 --> 00:37:34,079 you add a value 10 then you cannot add 1043 00:37:32,000 --> 00:37:36,480 the same value again so it will take the 1044 00:37:34,079 --> 00:37:38,960 value only once and this is how you can 1045 00:37:36,480 --> 00:37:40,560 create a set so you'll have curly braces 1046 00:37:38,960 --> 00:37:43,040 over here and then you'll given all of 1047 00:37:40,560 --> 00:37:45,359 the elements inside it so let me create 1048 00:37:43,040 --> 00:37:46,960 my final data structure over here 1049 00:37:45,359 --> 00:37:48,079 so i'll type in 1050 00:37:46,960 --> 00:37:50,320 one 1051 00:37:48,079 --> 00:37:53,359 a and then i'll given a floating point 1052 00:37:50,320 --> 00:37:55,119 value let's say 3.78 1053 00:37:53,359 --> 00:37:57,119 let me print out the set 1054 00:37:55,119 --> 00:38:00,079 so this is our set over here now what 1055 00:37:57,119 --> 00:38:02,720 i'll do is i'll copy this and let me try 1056 00:38:00,079 --> 00:38:04,480 to add some duplicate values 1057 00:38:02,720 --> 00:38:05,359 so i'll add 1058 00:38:04,480 --> 00:38:07,280 one 1059 00:38:05,359 --> 00:38:08,960 all of these times and then i'll also 1060 00:38:07,280 --> 00:38:11,839 add a 1061 00:38:08,960 --> 00:38:14,240 now let me print out s1 and then see 1062 00:38:11,839 --> 00:38:15,920 what value do i get 1063 00:38:14,240 --> 00:38:18,079 so we see that duplicates are not 1064 00:38:15,920 --> 00:38:19,839 allowed one even though i have given 1065 00:38:18,079 --> 00:38:22,000 around five or six times it has been 1066 00:38:19,839 --> 00:38:24,480 taken only once and a half given two 1067 00:38:22,000 --> 00:38:26,560 times but then again it comes only once 1068 00:38:24,480 --> 00:38:29,280 so obviously set does not allow any 1069 00:38:26,560 --> 00:38:31,920 duplicates inside it now let me go ahead 1070 00:38:29,280 --> 00:38:36,240 and add some new values inside the set 1071 00:38:31,920 --> 00:38:39,359 so i'll type in s1 dot add 1072 00:38:36,240 --> 00:38:39,359 hello world 1073 00:38:40,640 --> 00:38:45,440 right so i've added this new element 1074 00:38:43,760 --> 00:38:47,760 and if i want to add more than one 1075 00:38:45,440 --> 00:38:50,000 element at a single time then we've got 1076 00:38:47,760 --> 00:38:51,520 this update method so i'll type in 1077 00:38:50,000 --> 00:38:53,760 s1.update 1078 00:38:51,520 --> 00:38:55,040 and i'll give it a list of values so 1079 00:38:53,760 --> 00:38:56,560 i'll type in 1080 00:38:55,040 --> 00:39:00,079 let's see 1081 00:38:56,560 --> 00:39:02,800 sparta and then i'll type in 123. 1082 00:39:00,079 --> 00:39:05,359 then i'll given let's say 3 plus 1083 00:39:02,800 --> 00:39:05,359 9j 1084 00:39:05,520 --> 00:39:09,200 then let me print out s1 1085 00:39:07,680 --> 00:39:11,440 all right so all of these elements have 1086 00:39:09,200 --> 00:39:13,920 been added again you see that the order 1087 00:39:11,440 --> 00:39:16,480 of these elements is not preserved it is 1088 00:39:13,920 --> 00:39:19,280 random right so 3 plus 9 j comes over 1089 00:39:16,480 --> 00:39:21,599 here and then we've got 1 123 right so 1090 00:39:19,280 --> 00:39:23,359 the random is not at all preserved all 1091 00:39:21,599 --> 00:39:24,640 right so we are done with the basic data 1092 00:39:23,359 --> 00:39:28,079 structures in python which were 1093 00:39:24,640 --> 00:39:30,800 basically tuple list dictionary and set 1094 00:39:28,079 --> 00:39:33,760 now let's talk about our first linear 1095 00:39:30,800 --> 00:39:35,920 data structure that is a 1096 00:39:33,760 --> 00:39:37,920 so what is an array it is a linear data 1097 00:39:35,920 --> 00:39:40,320 structure that means elements will be 1098 00:39:37,920 --> 00:39:43,280 stored in a linear fashion 1099 00:39:40,320 --> 00:39:45,920 right linear fashion now if you talk 1100 00:39:43,280 --> 00:39:47,839 about any let's take an example now 1101 00:39:45,920 --> 00:39:50,160 let's consider that this is how you 1102 00:39:47,839 --> 00:39:52,320 represent an array in the form of a row 1103 00:39:50,160 --> 00:39:54,800 right and let's suppose it contains 1104 00:39:52,320 --> 00:39:57,119 elements one two three and four right 1105 00:39:54,800 --> 00:39:58,960 now with every memory location there 1106 00:39:57,119 --> 00:40:00,720 will be some address right so let's 1107 00:39:58,960 --> 00:40:02,880 suppose these are the four elements 1108 00:40:00,720 --> 00:40:04,480 right one two three and four 1109 00:40:02,880 --> 00:40:06,880 and these are some addresses let's 1110 00:40:04,480 --> 00:40:10,000 suppose this is 100 and this is 104 this 1111 00:40:06,880 --> 00:40:11,680 is 108 and this is one one two now if 1112 00:40:10,000 --> 00:40:13,920 you talk about memory obviously these 1113 00:40:11,680 --> 00:40:15,920 addresses will be hexadecimal and when 1114 00:40:13,920 --> 00:40:17,599 you talk about this this particular 1115 00:40:15,920 --> 00:40:21,200 array it will be somewhere in the memory 1116 00:40:17,599 --> 00:40:23,760 with four uh or you can say four bytes 1117 00:40:21,200 --> 00:40:26,480 of memory for each 1118 00:40:23,760 --> 00:40:28,720 integer now if i take a if i consider 1119 00:40:26,480 --> 00:40:31,359 this integer and let's suppose integer 1120 00:40:28,720 --> 00:40:33,760 takes four bytes now these four bytes 1121 00:40:31,359 --> 00:40:35,520 are available for each integer now this 1122 00:40:33,760 --> 00:40:37,359 is an integer right now it takes four 1123 00:40:35,520 --> 00:40:40,640 bytes now the second address will start 1124 00:40:37,359 --> 00:40:42,400 from 104 right because now again this 1125 00:40:40,640 --> 00:40:44,160 will take four bytes then one way again 1126 00:40:42,400 --> 00:40:46,319 it will take four bytes then one 1127 00:40:44,160 --> 00:40:49,119 one one two right so in memory it will 1128 00:40:46,319 --> 00:40:50,720 be somewhere around but the thing that 1129 00:40:49,119 --> 00:40:52,400 obviously you might be thinking okay sir 1130 00:40:50,720 --> 00:40:55,119 let's suppose this is our memory 1131 00:40:52,400 --> 00:40:56,640 and now if we have four and four bytes 1132 00:40:55,119 --> 00:40:58,560 that means eight bytes here and eight 1133 00:40:56,640 --> 00:41:00,160 bytes here but they are available in 1134 00:40:58,560 --> 00:41:02,240 chunks right this is one chunk and this 1135 00:41:00,160 --> 00:41:05,280 is second chunk and rest of the memory 1136 00:41:02,240 --> 00:41:07,760 is occupied can we store this array in 1137 00:41:05,280 --> 00:41:09,760 your memory in the memory no because it 1138 00:41:07,760 --> 00:41:12,000 needs contiguous memory allocation that 1139 00:41:09,760 --> 00:41:14,400 means when this is the scenario where in 1140 00:41:12,000 --> 00:41:17,680 you have memory or locations or memory 1141 00:41:14,400 --> 00:41:19,520 locations available in a one big chunk 1142 00:41:17,680 --> 00:41:21,359 right that means if you talk about this 1143 00:41:19,520 --> 00:41:23,440 array it requires four into four that is 1144 00:41:21,359 --> 00:41:25,520 16 bytes are available but and they are 1145 00:41:23,440 --> 00:41:27,359 available in a in a continuous memory 1146 00:41:25,520 --> 00:41:28,000 fashion right or if they are available 1147 00:41:27,359 --> 00:41:30,160 in 1148 00:41:28,000 --> 00:41:33,280 in such a way that it is a one single 1149 00:41:30,160 --> 00:41:36,160 chunk of 16 bytes okay so then only you 1150 00:41:33,280 --> 00:41:38,400 can store the elements at that location 1151 00:41:36,160 --> 00:41:41,760 now obviously for simplicity i'm taking 1152 00:41:38,400 --> 00:41:44,079 this addresses as a num integer number 1153 00:41:41,760 --> 00:41:46,560 but in reality those are hexadecimal 1154 00:41:44,079 --> 00:41:48,960 numbers okay so it is easier for me okay 1155 00:41:46,560 --> 00:41:50,480 so now one more thing is that 1156 00:41:48,960 --> 00:41:52,079 the elements are stored in a linear 1157 00:41:50,480 --> 00:41:54,880 fashion right but 1158 00:41:52,079 --> 00:41:57,200 can we access elements randomly yes with 1159 00:41:54,880 --> 00:41:58,960 the help of indexes so if you talk about 1160 00:41:57,200 --> 00:42:01,520 this array right 1161 00:41:58,960 --> 00:42:03,040 one two three and four obviously this 1162 00:42:01,520 --> 00:42:06,480 there will be a name associated with 1163 00:42:03,040 --> 00:42:07,839 this array right now we have index 0 1 2 1164 00:42:06,480 --> 00:42:11,119 3. 1165 00:42:07,839 --> 00:42:14,079 now why indexing starts with 0 or why 1166 00:42:11,119 --> 00:42:16,079 there is a 0 and indexing always starts 1167 00:42:14,079 --> 00:42:18,400 with 0. now the question is that right 1168 00:42:16,079 --> 00:42:19,680 now let's try to demystify this fact 1169 00:42:18,400 --> 00:42:22,480 that why 1170 00:42:19,680 --> 00:42:25,520 indexing starts from 0 why not it starts 1171 00:42:22,480 --> 00:42:27,440 from 1. now if you remember 1172 00:42:25,520 --> 00:42:29,359 right i told you that there will be a 1173 00:42:27,440 --> 00:42:32,720 name associated with this array that is 1174 00:42:29,359 --> 00:42:34,640 arr now this arr is nothing but name of 1175 00:42:32,720 --> 00:42:36,079 the array and name of the array 1176 00:42:34,640 --> 00:42:37,760 represents 1177 00:42:36,079 --> 00:42:41,200 right it represents 1178 00:42:37,760 --> 00:42:41,200 its base address 1179 00:42:41,680 --> 00:42:46,480 right now the base address of this was 1180 00:42:43,920 --> 00:42:49,760 earlier we spoke about it so it is 100 1181 00:42:46,480 --> 00:42:53,760 this is 104 this is 108 and this is 1 1 1182 00:42:49,760 --> 00:42:56,400 2 right so now this is 100 now 1183 00:42:53,760 --> 00:42:58,000 let's talk about how you access 1184 00:42:56,400 --> 00:42:59,280 we will talk about in the coming slides 1185 00:42:58,000 --> 00:43:01,200 we will see how to declare and 1186 00:42:59,280 --> 00:43:03,119 initialize our array but let's suppose 1187 00:43:01,200 --> 00:43:05,839 if we talk about how to access this we 1188 00:43:03,119 --> 00:43:08,640 use array and then the subscript and 1189 00:43:05,839 --> 00:43:10,800 then the index okay the index is one now 1190 00:43:08,640 --> 00:43:12,960 i told you name of this array represents 1191 00:43:10,800 --> 00:43:15,760 the base address so base address is 100 1192 00:43:12,960 --> 00:43:18,240 now plus 1 now this one represents 4 1193 00:43:15,760 --> 00:43:20,800 bytes okay so the 4 bytes 1194 00:43:18,240 --> 00:43:23,920 then what internally happens it will be 1195 00:43:20,800 --> 00:43:26,800 it boils down to 100 plus 4 that means 1196 00:43:23,920 --> 00:43:28,960 104. now 104 is not the first location 1197 00:43:26,800 --> 00:43:31,440 it is the second location okay now 1198 00:43:28,960 --> 00:43:32,960 similarly if you talk about accessing 1199 00:43:31,440 --> 00:43:35,599 the second element or third element in 1200 00:43:32,960 --> 00:43:39,280 the array it boils down to what area of 1201 00:43:35,599 --> 00:43:43,040 2 which is nothing but 100 plus 2 now 1202 00:43:39,280 --> 00:43:44,319 this 2 is nothing but 8 right 100 108 so 1203 00:43:43,040 --> 00:43:46,800 you will be 1204 00:43:44,319 --> 00:43:49,520 accessing the third element in the array 1205 00:43:46,800 --> 00:43:50,640 now how can you access the first element 1206 00:43:49,520 --> 00:43:53,680 so 1207 00:43:50,640 --> 00:43:56,560 here are 0 now it boils down around 100 1208 00:43:53,680 --> 00:43:59,680 plus 0 because there are no bytes right 1209 00:43:56,560 --> 00:44:02,319 so it boils down to this that array 1210 00:43:59,680 --> 00:44:05,119 indexing starts from 0 and now you know 1211 00:44:02,319 --> 00:44:07,440 why and this is how you can access 1212 00:44:05,119 --> 00:44:10,480 elements randomly so with the help of 1213 00:44:07,440 --> 00:44:13,359 these indexes okay so now you might be 1214 00:44:10,480 --> 00:44:15,680 thinking okay now we have an array 1215 00:44:13,359 --> 00:44:17,680 can we store different elements right 1216 00:44:15,680 --> 00:44:19,839 can we store let's suppose that it can 1217 00:44:17,680 --> 00:44:22,160 be stored in this will sort integer then 1218 00:44:19,839 --> 00:44:24,800 we will store a floating point number 1219 00:44:22,160 --> 00:44:26,880 then we will can we store a character no 1220 00:44:24,800 --> 00:44:29,200 if you talk about any particular area 1221 00:44:26,880 --> 00:44:31,839 let's talk about this array now the data 1222 00:44:29,200 --> 00:44:34,160 type or the type of data that you can 1223 00:44:31,839 --> 00:44:37,440 store in this area will be homogeneous 1224 00:44:34,160 --> 00:44:40,720 that means you can only store similar 1225 00:44:37,440 --> 00:44:40,720 elements okay 1226 00:44:40,960 --> 00:44:47,359 so these are some facts and this is how 1227 00:44:45,200 --> 00:44:49,440 array works and what are the addresses 1228 00:44:47,359 --> 00:44:51,760 what are the indexes can you store 1229 00:44:49,440 --> 00:44:54,400 different elements no you can only store 1230 00:44:51,760 --> 00:44:56,880 similar elements in the 1231 00:44:54,400 --> 00:44:59,200 now let's talk about the applications of 1232 00:44:56,880 --> 00:45:01,760 array now you might be thinking sir why 1233 00:44:59,200 --> 00:45:03,520 do we need this array what is the uh 1234 00:45:01,760 --> 00:45:05,359 what is the reason that we are using 1235 00:45:03,520 --> 00:45:08,079 this array so basically when you talk 1236 00:45:05,359 --> 00:45:10,240 about arrays now obviously when you have 1237 00:45:08,079 --> 00:45:12,960 a scenario wherein you want to store 1238 00:45:10,240 --> 00:45:15,599 your elements in a linear fashion right 1239 00:45:12,960 --> 00:45:18,160 and that too you want to store them in a 1240 00:45:15,599 --> 00:45:20,640 contiguous memory locations right so 1241 00:45:18,160 --> 00:45:22,960 that you can use your cpu or you can use 1242 00:45:20,640 --> 00:45:24,800 your memory efficiently right 1243 00:45:22,960 --> 00:45:26,960 not the cpu you can use your memory and 1244 00:45:24,800 --> 00:45:29,280 you can utilize your memory to the 1245 00:45:26,960 --> 00:45:31,440 maximum right so you want to utilize 1246 00:45:29,280 --> 00:45:33,839 your memory efficiently at that time you 1247 00:45:31,440 --> 00:45:35,599 can use this but obviously it will have 1248 00:45:33,839 --> 00:45:37,280 some drawbacks right it will have some 1249 00:45:35,599 --> 00:45:39,040 drawbacks that is why we have different 1250 00:45:37,280 --> 00:45:41,119 different data structures right so if 1251 00:45:39,040 --> 00:45:43,920 you want to store your data in a linear 1252 00:45:41,119 --> 00:45:46,640 fashion you can use arrays okay now it 1253 00:45:43,920 --> 00:45:48,960 is also suitable for 1254 00:45:46,640 --> 00:45:51,760 for the scenarios wherein 1255 00:45:48,960 --> 00:45:53,839 you require frequent searching right if 1256 00:45:51,760 --> 00:45:56,000 you want to search an element in an area 1257 00:45:53,839 --> 00:45:58,400 you can directly go and access these 1258 00:45:56,000 --> 00:45:59,920 indexes one by one right so in a linear 1259 00:45:58,400 --> 00:46:01,359 fashion you will access okay is this the 1260 00:45:59,920 --> 00:46:03,040 element that you're looking for no it's 1261 00:46:01,359 --> 00:46:04,400 20th element that you're looking for no 1262 00:46:03,040 --> 00:46:06,240 is 30 the element that you're looking 1263 00:46:04,400 --> 00:46:08,720 for no as 40 the element that you're 1264 00:46:06,240 --> 00:46:10,800 looking for yes one by one you can 1265 00:46:08,720 --> 00:46:13,040 access all those elements and 1266 00:46:10,800 --> 00:46:16,240 try to search for the element that you 1267 00:46:13,040 --> 00:46:18,960 are looking for okay so it is suitable 1268 00:46:16,240 --> 00:46:21,359 for applications which require frequent 1269 00:46:18,960 --> 00:46:22,880 searching now let's talk about one 1270 00:46:21,359 --> 00:46:25,280 dimension 1271 00:46:22,880 --> 00:46:27,839 so if you talk about 1d array it is it 1272 00:46:25,280 --> 00:46:30,560 can be related to a rule like we saw in 1273 00:46:27,839 --> 00:46:32,800 the example right so that is what is a 1274 00:46:30,560 --> 00:46:35,040 one dimensional array it is represented 1275 00:46:32,800 --> 00:46:40,000 in the form of row and we have addresses 1276 00:46:35,040 --> 00:46:42,880 like 104 100 104 108 112 and 116 and 1277 00:46:40,000 --> 00:46:44,480 indexing will be obviously 0 1 2 3 4 and 1278 00:46:42,880 --> 00:46:46,480 then there will be a name associated 1279 00:46:44,480 --> 00:46:48,079 with this array which is arr and then 1280 00:46:46,480 --> 00:46:50,480 you can store the elements in this array 1281 00:46:48,079 --> 00:46:52,640 let's suppose here the it is an integer 1282 00:46:50,480 --> 00:46:53,839 array okay so you can store only integer 1283 00:46:52,640 --> 00:46:54,640 elements 1284 00:46:53,839 --> 00:46:56,880 and 1285 00:46:54,640 --> 00:46:58,480 the size of this array is five and you 1286 00:46:56,880 --> 00:47:01,119 have stored the elements one two three 1287 00:46:58,480 --> 00:47:03,599 four five so now it can be related to a 1288 00:47:01,119 --> 00:47:06,000 row where in elements are stored one 1289 00:47:03,599 --> 00:47:07,520 after the other like you see until you 1290 00:47:06,000 --> 00:47:09,839 have one then you have two then you have 1291 00:47:07,520 --> 00:47:12,400 three and there's those or all of these 1292 00:47:09,839 --> 00:47:13,760 numbers are in a contiguous memory are 1293 00:47:12,400 --> 00:47:16,400 available in a contiguous memory 1294 00:47:13,760 --> 00:47:19,920 location okay now when you talk about 1d 1295 00:47:16,400 --> 00:47:22,000 array only one index used right when you 1296 00:47:19,920 --> 00:47:23,920 try to declare and initialize your area 1297 00:47:22,000 --> 00:47:25,839 at that time you will use one subscript 1298 00:47:23,920 --> 00:47:27,920 okay so how you can use that let's 1299 00:47:25,839 --> 00:47:29,920 suppose if i if i talk about this 1300 00:47:27,920 --> 00:47:33,440 special array that i that i have defined 1301 00:47:29,920 --> 00:47:36,640 here here right you will define it a r 1302 00:47:33,440 --> 00:47:38,400 and then the sub or the index is and the 1303 00:47:36,640 --> 00:47:41,040 number of elements that are present here 1304 00:47:38,400 --> 00:47:43,920 which is five so only one subscript will 1305 00:47:41,040 --> 00:47:47,119 be there or one index will be used okay 1306 00:47:43,920 --> 00:47:49,599 so this is how you uh declare your array 1307 00:47:47,119 --> 00:47:51,680 so now let's talk about the declaration 1308 00:47:49,599 --> 00:47:53,200 and initialization of this area so 1309 00:47:51,680 --> 00:47:54,720 obviously when you talk about the array 1310 00:47:53,200 --> 00:47:57,040 there will be a name associated with the 1311 00:47:54,720 --> 00:47:58,800 array and then the data type are you 1312 00:47:57,040 --> 00:48:01,760 going to store integer values in that 1313 00:47:58,800 --> 00:48:03,760 area then the one uh that one subscript 1314 00:48:01,760 --> 00:48:06,480 or one index that we use and then 1315 00:48:03,760 --> 00:48:10,319 definitely the size of the edit so this 1316 00:48:06,480 --> 00:48:11,760 is how you declare your 1d array now how 1317 00:48:10,319 --> 00:48:13,599 can you initialize it there are 1318 00:48:11,760 --> 00:48:15,440 different ways you can initialize it 1319 00:48:13,599 --> 00:48:17,520 obviously here you are declaring it then 1320 00:48:15,440 --> 00:48:19,839 you might use a for loop to initialize 1321 00:48:17,520 --> 00:48:23,200 all the elements or you can declare or 1322 00:48:19,839 --> 00:48:25,440 initialize your array at once so how do 1323 00:48:23,200 --> 00:48:26,800 you do that so you will write 1324 00:48:25,440 --> 00:48:27,599 and let's suppose this is the integer 1325 00:48:26,800 --> 00:48:29,839 array 1326 00:48:27,599 --> 00:48:31,440 and you don't have to specify the size 1327 00:48:29,839 --> 00:48:33,040 you can directly write the elements 1328 00:48:31,440 --> 00:48:35,760 right and those elements let's suppose 1329 00:48:33,040 --> 00:48:37,520 those are one two three four and five in 1330 00:48:35,760 --> 00:48:40,559 this case when you're declaring and 1331 00:48:37,520 --> 00:48:43,359 initializing your array at once at that 1332 00:48:40,559 --> 00:48:45,280 time this size becomes optional you 1333 00:48:43,359 --> 00:48:47,520 don't have to specify explicitly the 1334 00:48:45,280 --> 00:48:50,079 size of the array but obviously the size 1335 00:48:47,520 --> 00:48:52,160 of this area will be five okay now since 1336 00:48:50,079 --> 00:48:55,520 you are declaring and initializing it at 1337 00:48:52,160 --> 00:48:57,680 once so this is optional but in the case 1338 00:48:55,520 --> 00:49:00,400 where wherein you are not initializing 1339 00:48:57,680 --> 00:49:02,800 it at that time the size becomes 1340 00:49:00,400 --> 00:49:05,680 very important and you have to mention 1341 00:49:02,800 --> 00:49:08,319 this size explicitly okay 1342 00:49:05,680 --> 00:49:10,559 now let's talk about two dimensional 1343 00:49:08,319 --> 00:49:13,520 so also it is known as 2d array so it 1344 00:49:10,559 --> 00:49:16,160 can be related to a table like this or 1345 00:49:13,520 --> 00:49:19,760 you can also say a matrix wherein you 1346 00:49:16,160 --> 00:49:22,319 have rows and columns right now in this 1347 00:49:19,760 --> 00:49:24,640 elements are stored one after the other 1348 00:49:22,319 --> 00:49:27,040 in such a way that you can think of it 1349 00:49:24,640 --> 00:49:29,280 as a 1d array now this is what when the 1350 00:49:27,040 --> 00:49:31,839 array right as we have already seen it 1351 00:49:29,280 --> 00:49:34,960 right and inside this one nd array you 1352 00:49:31,839 --> 00:49:37,839 have another 1d array right 1353 00:49:34,960 --> 00:49:39,680 now this is known as 2d array so now how 1354 00:49:37,839 --> 00:49:41,760 it works right 1355 00:49:39,680 --> 00:49:44,160 let's suppose you have numbers over here 1356 00:49:41,760 --> 00:49:45,920 and you have four numbers one two three 1357 00:49:44,160 --> 00:49:47,040 four then you have 1358 00:49:45,920 --> 00:49:50,559 five 1359 00:49:47,040 --> 00:49:53,200 six seven eight and you have nine 1360 00:49:50,559 --> 00:49:56,480 ten and eleven and twelve so this is the 1361 00:49:53,200 --> 00:49:59,280 2d area of having oh and this will be 1362 00:49:56,480 --> 00:50:01,680 similar to what of having 1363 00:49:59,280 --> 00:50:03,440 three rows 1364 00:50:01,680 --> 00:50:05,839 right you have you will have three rows 1365 00:50:03,440 --> 00:50:09,040 not four rows you will have three rows 1366 00:50:05,839 --> 00:50:10,240 and in that in those three rows right 1367 00:50:09,040 --> 00:50:11,440 you will have 1368 00:50:10,240 --> 00:50:13,280 what 1369 00:50:11,440 --> 00:50:15,119 four columns 1370 00:50:13,280 --> 00:50:17,200 one two and 1371 00:50:15,119 --> 00:50:21,359 so numbers will be like this one two 1372 00:50:17,200 --> 00:50:24,880 three four five six seven eight nine ten 1373 00:50:21,359 --> 00:50:26,880 eleven and twelve okay done so now 1374 00:50:24,880 --> 00:50:29,280 obviously this will have let's suppose 1375 00:50:26,880 --> 00:50:31,599 this is a zero based indexing and this 1376 00:50:29,280 --> 00:50:33,760 will have zero index here one index here 1377 00:50:31,599 --> 00:50:35,760 and one now internally what is happening 1378 00:50:33,760 --> 00:50:39,520 it will be a zero zero and it will be a 1379 00:50:35,760 --> 00:50:46,160 zero one and zero two zero three then 1 1380 00:50:39,520 --> 00:50:48,319 0 1 1 1 2 1 3 then 2 0 2 1 2 2 and 2 3 1381 00:50:46,160 --> 00:50:50,640 indexing right so similar to that we 1382 00:50:48,319 --> 00:50:52,800 will have this index would be 0 0 this 1383 00:50:50,640 --> 00:50:54,559 would be like this and this will be like 1384 00:50:52,800 --> 00:50:56,480 this so 1385 00:50:54,559 --> 00:50:58,559 if you want to access the element that 1386 00:50:56,480 --> 00:51:00,960 is present at this location what you 1387 00:50:58,559 --> 00:51:03,760 will do you will run two for loops right 1388 00:51:00,960 --> 00:51:05,839 one uh will be starting with from here 1389 00:51:03,760 --> 00:51:09,680 let's suppose one will start from i 1390 00:51:05,839 --> 00:51:12,079 equal to zero to the length of this 1391 00:51:09,680 --> 00:51:15,280 outer array that is 1392 00:51:12,079 --> 00:51:16,880 3 less than 3 right so 1393 00:51:15,280 --> 00:51:19,520 and the another 1394 00:51:16,880 --> 00:51:22,319 from 0 to the number of 1395 00:51:19,520 --> 00:51:25,040 rows that are there okay 1396 00:51:22,319 --> 00:51:26,640 so this one will be the outer loop 1397 00:51:25,040 --> 00:51:28,640 this one will be the outer loop and this 1398 00:51:26,640 --> 00:51:30,880 one will be the inner loop that means 1399 00:51:28,640 --> 00:51:33,040 the number of columns that are there so 1400 00:51:30,880 --> 00:51:35,599 this is for row and this will be for 1401 00:51:33,040 --> 00:51:37,359 column that will start from 0 to 1402 00:51:35,599 --> 00:51:38,559 less than four okay 1403 00:51:37,359 --> 00:51:40,559 so 1404 00:51:38,559 --> 00:51:42,640 this is how you iterate and it will be 1405 00:51:40,559 --> 00:51:44,319 similar to this and now what about the 1406 00:51:42,640 --> 00:51:46,319 addressing right what about the 1407 00:51:44,319 --> 00:51:48,400 addresses that will be there so this 1408 00:51:46,319 --> 00:51:51,440 will be let's suppose if this is 100 1409 00:51:48,400 --> 00:51:54,319 this will be 104 this will be 108 this 1410 00:51:51,440 --> 00:51:57,920 will be 1 1 2 this will be 1 1 6 because 1411 00:51:54,319 --> 00:52:00,400 internally it is treated as if they are 1412 00:51:57,920 --> 00:52:02,640 again in a contiguous memory location 1413 00:52:00,400 --> 00:52:04,800 but this time around you have a 1d array 1414 00:52:02,640 --> 00:52:08,000 and inside that 1d array you have 1415 00:52:04,800 --> 00:52:10,559 another one dna so for declaring it 1416 00:52:08,000 --> 00:52:12,319 you will use two subscripts right so it 1417 00:52:10,559 --> 00:52:14,480 will be something the name of the array 1418 00:52:12,319 --> 00:52:16,160 then two subscripts and now this will 1419 00:52:14,480 --> 00:52:17,920 represent the number of rows and this 1420 00:52:16,160 --> 00:52:19,760 will represent the number of columns so 1421 00:52:17,920 --> 00:52:22,160 in this case the number of rows will be 1422 00:52:19,760 --> 00:52:24,559 three and in this uh and the number of 1423 00:52:22,160 --> 00:52:26,000 columns is four right so this is how you 1424 00:52:24,559 --> 00:52:28,400 declare your 1425 00:52:26,000 --> 00:52:30,480 what a two d array so dimension depends 1426 00:52:28,400 --> 00:52:32,880 upon the number of subscripts you are 1427 00:52:30,480 --> 00:52:34,960 using so this time around we are using 1428 00:52:32,880 --> 00:52:36,960 two subscripts now let's suppose you are 1429 00:52:34,960 --> 00:52:39,119 using three subscripts right 1430 00:52:36,960 --> 00:52:41,760 so similar to like this three 1431 00:52:39,119 --> 00:52:43,839 three and three okay so this time around 1432 00:52:41,760 --> 00:52:46,319 this is the 3d array and similarly you 1433 00:52:43,839 --> 00:52:48,400 can have multi-dimensional array right 1434 00:52:46,319 --> 00:52:50,319 and you just need to keep on adding the 1435 00:52:48,400 --> 00:52:52,079 subscripts that's it okay 1436 00:52:50,319 --> 00:52:54,240 now we should learn regarding array 1437 00:52:52,079 --> 00:52:55,920 implementation right we are solving 1438 00:52:54,240 --> 00:52:58,000 three different problem statements here 1439 00:52:55,920 --> 00:53:00,240 the first one is we are creating one 1440 00:52:58,000 --> 00:53:02,319 dimensional array it is very simple and 1441 00:53:00,240 --> 00:53:04,559 people can understand it in very easy 1442 00:53:02,319 --> 00:53:07,359 way the second one we are concentrating 1443 00:53:04,559 --> 00:53:09,599 on creating two dimensional array that 1444 00:53:07,359 --> 00:53:12,400 means usually we use it for matrix which 1445 00:53:09,599 --> 00:53:15,760 includes rows and columns also we call 1446 00:53:12,400 --> 00:53:17,040 it as m and n or m cross n all these 1447 00:53:15,760 --> 00:53:19,280 three are the names which you can give 1448 00:53:17,040 --> 00:53:21,200 it for two dimensional array and also 1449 00:53:19,280 --> 00:53:23,520 two dimensional array is used for 1450 00:53:21,200 --> 00:53:26,640 different purposes at the last we are 1451 00:53:23,520 --> 00:53:29,200 trying to sort search and insert delete 1452 00:53:26,640 --> 00:53:31,359 the elements inside an array only which 1453 00:53:29,200 --> 00:53:33,599 is having integers so these are the 1454 00:53:31,359 --> 00:53:35,920 problem statements we are solving for 1455 00:53:33,599 --> 00:53:37,839 arrays in python so let's quickly hop 1456 00:53:35,920 --> 00:53:40,559 into the id and check out the first 1457 00:53:37,839 --> 00:53:42,400 problem statement that is how to create 1458 00:53:40,559 --> 00:53:45,200 one dimensional array and insert 1459 00:53:42,400 --> 00:53:47,280 elements inside that also put up the 1460 00:53:45,200 --> 00:53:50,400 output whatever the input is given by 1461 00:53:47,280 --> 00:53:51,599 the user on the screen so let's hop into 1462 00:53:50,400 --> 00:53:54,640 the ide now 1463 00:53:51,599 --> 00:53:58,240 here i'm using google collab in order to 1464 00:53:54,640 --> 00:54:00,720 put up the first program right so 1465 00:53:58,240 --> 00:54:02,880 we'll rename this i'm 1466 00:54:00,720 --> 00:54:03,680 naming this as 1467 00:54:02,880 --> 00:54:06,160 one 1468 00:54:03,680 --> 00:54:07,440 dimensional array 1469 00:54:06,160 --> 00:54:10,480 so 1470 00:54:07,440 --> 00:54:13,599 now we will come to this ide and we'll 1471 00:54:10,480 --> 00:54:15,920 type one dimensional array example where 1472 00:54:13,599 --> 00:54:17,680 you are including array size and you are 1473 00:54:15,920 --> 00:54:20,240 asking the user what are the different 1474 00:54:17,680 --> 00:54:22,160 inputs and then we are presenting the 1475 00:54:20,240 --> 00:54:24,480 same inputs received by the user on the 1476 00:54:22,160 --> 00:54:26,720 output screen so to quickly save the 1477 00:54:24,480 --> 00:54:28,800 time i'm just putting up the 1478 00:54:26,720 --> 00:54:32,079 code now 1479 00:54:28,800 --> 00:54:33,839 so this is the python code where we'll 1480 00:54:32,079 --> 00:54:36,400 be using for one dimensional array i'll 1481 00:54:33,839 --> 00:54:39,040 explain what is happening here the first 1482 00:54:36,400 --> 00:54:41,359 thing is we are asking how many elements 1483 00:54:39,040 --> 00:54:43,920 to store inside the array for example it 1484 00:54:41,359 --> 00:54:46,240 might be 5 6 10 so whatever the integer 1485 00:54:43,920 --> 00:54:49,119 number is the whole number we can give 1486 00:54:46,240 --> 00:54:51,200 it right so again we are asking 1487 00:54:49,119 --> 00:54:53,680 assigning a variable for input so 1488 00:54:51,200 --> 00:54:56,000 whatever the input is given by the user 1489 00:54:53,680 --> 00:54:58,880 will be assigned to the variable called 1490 00:54:56,000 --> 00:55:01,280 num right then we are assigning an empty 1491 00:54:58,880 --> 00:55:04,000 array why because whatever the size has 1492 00:55:01,280 --> 00:55:06,240 been defined by the user is put up here 1493 00:55:04,000 --> 00:55:08,000 so if it is five it can take only five 1494 00:55:06,240 --> 00:55:10,000 elements if it is six it can take only 1495 00:55:08,000 --> 00:55:11,920 six elements that's how it goes and 1496 00:55:10,000 --> 00:55:14,799 immediately we'll ask to enter the 1497 00:55:11,920 --> 00:55:16,799 elements inside the array then we'll be 1498 00:55:14,799 --> 00:55:18,559 pushing through a for loop and we'll be 1499 00:55:16,799 --> 00:55:21,839 using one 1500 00:55:18,559 --> 00:55:24,319 important piece of code here that is arr 1501 00:55:21,839 --> 00:55:26,559 dot append append in the sense will be 1502 00:55:24,319 --> 00:55:28,319 assigning the elements one after the 1503 00:55:26,559 --> 00:55:30,079 other at the back of the array we are 1504 00:55:28,319 --> 00:55:32,720 not putting up the elements which is 1505 00:55:30,079 --> 00:55:35,200 inserted by the user in middle or in the 1506 00:55:32,720 --> 00:55:37,119 front or somewhere right so append will 1507 00:55:35,200 --> 00:55:40,000 always ensure the elements which is 1508 00:55:37,119 --> 00:55:42,559 given by the user is put up at the back 1509 00:55:40,000 --> 00:55:44,799 of the array one after the other right 1510 00:55:42,559 --> 00:55:47,280 so next will display whatever the array 1511 00:55:44,799 --> 00:55:48,960 elements are so the array elements are 1512 00:55:47,280 --> 00:55:51,520 again you have to push through for loop 1513 00:55:48,960 --> 00:55:53,680 because it has to uh just print the 1514 00:55:51,520 --> 00:55:55,839 elements one after the other so let's 1515 00:55:53,680 --> 00:55:59,359 quickly run this program and check out 1516 00:55:55,839 --> 00:55:59,359 how does this output look 1517 00:56:00,720 --> 00:56:05,280 right so it is asking how many elements 1518 00:56:03,599 --> 00:56:09,040 do you want to insert into array so i'm 1519 00:56:05,280 --> 00:56:10,640 just putting up three as of now so enter 1520 00:56:09,040 --> 00:56:12,559 then it will ask you for the first 1521 00:56:10,640 --> 00:56:14,720 number i'm putting up four 1522 00:56:12,559 --> 00:56:17,119 and then 1523 00:56:14,720 --> 00:56:21,200 the second number that is 5 1524 00:56:17,119 --> 00:56:24,480 then let us i'm giving 7 okay 1525 00:56:21,200 --> 00:56:26,799 so it will display 4 5 7. also you can 1526 00:56:24,480 --> 00:56:27,839 modify this outputs by giving commas by 1527 00:56:26,799 --> 00:56:30,160 giving 1528 00:56:27,839 --> 00:56:32,400 spaces between those if not it can 1529 00:56:30,160 --> 00:56:34,640 generally display this before five and 1530 00:56:32,400 --> 00:56:36,880 seven so this is about one dimensional 1531 00:56:34,640 --> 00:56:39,440 array in python let's see the second 1532 00:56:36,880 --> 00:56:41,440 problem statement in arrays for python 1533 00:56:39,440 --> 00:56:43,760 right so we are going to create two 1534 00:56:41,440 --> 00:56:46,720 dimensional integer array where you can 1535 00:56:43,760 --> 00:56:48,319 insert row number and column number and 1536 00:56:46,720 --> 00:56:50,799 it will fill up the 1537 00:56:48,319 --> 00:56:52,880 elements inside the array accordingly so 1538 00:56:50,799 --> 00:56:55,040 let's quickly switch on to the ide 1539 00:56:52,880 --> 00:56:58,880 that's google collab and check out how 1540 00:56:55,040 --> 00:57:01,520 does 2d array work in python so here i 1541 00:56:58,880 --> 00:57:03,680 have named this particular file as 1542 00:57:01,520 --> 00:57:05,760 2d array and you can name it whatever 1543 00:57:03,680 --> 00:57:07,920 you want and i'm 1544 00:57:05,760 --> 00:57:10,400 putting up the code here so explaining 1545 00:57:07,920 --> 00:57:13,280 the code for you that we have 1546 00:57:10,400 --> 00:57:15,359 asked for row numbers so how much the 1547 00:57:13,280 --> 00:57:17,520 row should be in your matrix i'll take 1548 00:57:15,359 --> 00:57:20,720 it as matrix only because usually rows 1549 00:57:17,520 --> 00:57:22,720 and columns will be using in matrix so 1550 00:57:20,720 --> 00:57:26,319 number of rows should be given by the 1551 00:57:22,720 --> 00:57:29,119 user and we'll store that number in our 1552 00:57:26,319 --> 00:57:31,839 underscore num that is row number again 1553 00:57:29,119 --> 00:57:34,480 we'll ask the user uh input number of 1554 00:57:31,839 --> 00:57:36,799 columns right so whatever the number is 1555 00:57:34,480 --> 00:57:39,440 given integer value whole numbers is 1556 00:57:36,799 --> 00:57:42,400 stored in c num that means column number 1557 00:57:39,440 --> 00:57:44,720 you can accordingly put up the variables 1558 00:57:42,400 --> 00:57:47,040 as per the problem statements so here to 1559 00:57:44,720 --> 00:57:48,000 keep it relatable i have used arnhem and 1560 00:57:47,040 --> 00:57:50,160 cena 1561 00:57:48,000 --> 00:57:53,440 next we are going to 1562 00:57:50,160 --> 00:57:55,680 assign whatever the values we have right 1563 00:57:53,440 --> 00:57:57,599 that is given by the user that is it 1564 00:57:55,680 --> 00:58:00,000 might be a row number or it might be a 1565 00:57:57,599 --> 00:58:01,599 column number we'll assign that with the 1566 00:58:00,000 --> 00:58:03,599 elements so 1567 00:58:01,599 --> 00:58:05,760 to assign will be using for loop because 1568 00:58:03,599 --> 00:58:08,079 one after the other it has to be printed 1569 00:58:05,760 --> 00:58:09,760 right at last we will be printing the 1570 00:58:08,079 --> 00:58:12,319 final 1571 00:58:09,760 --> 00:58:13,760 array and final matrix two dimensional 1572 00:58:12,319 --> 00:58:16,640 array in 1573 00:58:13,760 --> 00:58:18,960 2d array i've just put up a abbreviation 1574 00:58:16,640 --> 00:58:19,920 here so it's understandable for you guys 1575 00:58:18,960 --> 00:58:23,040 so 1576 00:58:19,920 --> 00:58:25,119 t w o two d is dimension underscore 1577 00:58:23,040 --> 00:58:26,640 array arr i'm not put up completely 1578 00:58:25,119 --> 00:58:30,079 array it's just ar 1579 00:58:26,640 --> 00:58:32,799 so this is how we initialize and the 1580 00:58:30,079 --> 00:58:35,359 variables declaration and this is how we 1581 00:58:32,799 --> 00:58:38,000 execute the program let's quickly see 1582 00:58:35,359 --> 00:58:39,920 the output of this so i'm running it is 1583 00:58:38,000 --> 00:58:42,079 asking for the first time that is 1584 00:58:39,920 --> 00:58:45,599 input number of rows so the number of 1585 00:58:42,079 --> 00:58:47,599 rows i'm giving here is uh 2 and again 1586 00:58:45,599 --> 00:58:50,960 i'll enter it will ask for number of 1587 00:58:47,599 --> 00:58:53,280 columns so then it is three here i'm 1588 00:58:50,960 --> 00:58:55,200 entering that and it is giving you two 1589 00:58:53,280 --> 00:58:57,839 rows and three columns also if you want 1590 00:58:55,200 --> 00:58:59,359 you can arrange it as per matrix so one 1591 00:58:57,839 --> 00:59:02,559 after the other but here i'm showing it 1592 00:58:59,359 --> 00:59:04,720 for you guys just with see you can count 1593 00:59:02,559 --> 00:59:07,119 three columns you have and two rows 1594 00:59:04,720 --> 00:59:09,119 right the bracket defines the rows here 1595 00:59:07,119 --> 00:59:11,119 right the first bracket set of brackets 1596 00:59:09,119 --> 00:59:13,920 is for first row and second set of 1597 00:59:11,119 --> 00:59:17,760 brackets is second rows so this is how 1598 00:59:13,920 --> 00:59:19,839 2d array works in python so we are going 1599 00:59:17,760 --> 00:59:22,720 for the third problem statement which we 1600 00:59:19,839 --> 00:59:25,599 are solving for array in python so it 1601 00:59:22,720 --> 00:59:28,960 says implement search sort and delete 1602 00:59:25,599 --> 00:59:31,119 operations on array of integers right so 1603 00:59:28,960 --> 00:59:33,280 i'm breaking these three 1604 00:59:31,119 --> 00:59:35,359 operations that is search sort and 1605 00:59:33,280 --> 00:59:37,680 delete into three different programs to 1606 00:59:35,359 --> 00:59:40,000 make it simpler rather than combining 1607 00:59:37,680 --> 00:59:42,400 everything and making it to one huge 1608 00:59:40,000 --> 00:59:46,079 program so first i'm concentrating on 1609 00:59:42,400 --> 00:59:49,040 deleting elements inside an array of 1610 00:59:46,079 --> 00:59:52,400 integers in python so quickly we shall 1611 00:59:49,040 --> 00:59:54,799 hop into the ide and see the program 1612 00:59:52,400 --> 00:59:56,960 here google collab is ready and the page 1613 00:59:54,799 --> 00:59:59,040 is empty 1614 00:59:56,960 --> 01:00:01,119 i'm just pasting this particular code in 1615 00:59:59,040 --> 01:00:04,319 order to 1616 01:00:01,119 --> 01:00:06,880 use time efficiently so explaining this 1617 01:00:04,319 --> 01:00:09,359 code the first line it says enter the 1618 01:00:06,880 --> 01:00:11,680 size of an array right we are first 1619 01:00:09,359 --> 01:00:14,960 accessing an array size for example it 1620 01:00:11,680 --> 01:00:17,040 might be 10 5 8 as per the user command 1621 01:00:14,960 --> 01:00:19,359 and then we are inserting so many 1622 01:00:17,040 --> 01:00:22,240 elements in into that particular array 1623 01:00:19,359 --> 01:00:24,079 say for example it is 5 right so we are 1624 01:00:22,240 --> 01:00:26,960 inserting 5 different elements which we 1625 01:00:24,079 --> 01:00:30,559 have already seen by now so next it is 1626 01:00:26,960 --> 01:00:33,280 asking which element to delete right so 1627 01:00:30,559 --> 01:00:35,280 we are telling an element an integer to 1628 01:00:33,280 --> 01:00:37,920 delete then it will display the new 1629 01:00:35,280 --> 01:00:38,960 array for you so for loops are there in 1630 01:00:37,920 --> 01:00:42,559 order to 1631 01:00:38,960 --> 01:00:44,720 keep the array in sequence and it might 1632 01:00:42,559 --> 01:00:47,200 be printing or it might be taking input 1633 01:00:44,720 --> 01:00:50,240 from the user both we are using for loop 1634 01:00:47,200 --> 01:00:52,480 only and append is for putting up the 1635 01:00:50,240 --> 01:00:54,400 elements into the back of an array right 1636 01:00:52,480 --> 01:00:56,559 we are not inserting element in middle 1637 01:00:54,400 --> 01:00:59,359 or somewhere in the front abruptly the 1638 01:00:56,559 --> 01:01:01,200 incision should not happen so one after 1639 01:00:59,359 --> 01:01:03,040 the other sequentially in order to 1640 01:01:01,200 --> 01:01:06,319 append in order to insert the elements 1641 01:01:03,040 --> 01:01:07,119 we use ar dot append so let's quickly 1642 01:01:06,319 --> 01:01:09,920 see 1643 01:01:07,119 --> 01:01:12,720 what is the output of it so if you have 1644 01:01:09,920 --> 01:01:14,880 entered any element which is not there 1645 01:01:12,720 --> 01:01:17,200 in this particular array right so it 1646 01:01:14,880 --> 01:01:20,400 will give you element does not exist in 1647 01:01:17,200 --> 01:01:25,200 an array so this is how the program will 1648 01:01:20,400 --> 01:01:26,799 work so let's quickly see the output now 1649 01:01:25,200 --> 01:01:28,400 so it is asking for array size i am 1650 01:01:26,799 --> 01:01:29,680 giving three 1651 01:01:28,400 --> 01:01:32,240 i'll enter 1652 01:01:29,680 --> 01:01:35,040 then entering all the three numbers what 1653 01:01:32,240 --> 01:01:35,040 i want to give 1654 01:01:36,240 --> 01:01:42,640 okay it will ask you which value to be 1655 01:01:38,720 --> 01:01:45,119 deleted right i am giving value five 1656 01:01:42,640 --> 01:01:47,680 so the new array is without five that is 1657 01:01:45,119 --> 01:01:49,839 four and six so this is how it will work 1658 01:01:47,680 --> 01:01:51,760 and immediately i'll 1659 01:01:49,839 --> 01:01:53,839 show you if you give any element which 1660 01:01:51,760 --> 01:01:56,319 is out of the array bound how it will 1661 01:01:53,839 --> 01:01:58,640 give you an error so i am taking three 1662 01:01:56,319 --> 01:01:59,760 elements again 1663 01:01:58,640 --> 01:02:00,960 5 1664 01:01:59,760 --> 01:02:02,240 7 1665 01:02:00,960 --> 01:02:03,680 and 8 1666 01:02:02,240 --> 01:02:05,440 right so 1667 01:02:03,680 --> 01:02:07,200 it will ask you which value to be 1668 01:02:05,440 --> 01:02:09,920 deleted i'll say 1669 01:02:07,200 --> 01:02:12,480 1 1 is not there in the array it is just 1670 01:02:09,920 --> 01:02:15,280 5 7 and 8 right 1671 01:02:12,480 --> 01:02:18,240 so if you put that it will say element 1672 01:02:15,280 --> 01:02:21,039 does not exist in an array right so this 1673 01:02:18,240 --> 01:02:22,079 is how deletion will work in python 1674 01:02:21,039 --> 01:02:24,400 arrays 1675 01:02:22,079 --> 01:02:27,280 after knowing how to delete element in 1676 01:02:24,400 --> 01:02:29,680 an array so we have to see next how do 1677 01:02:27,280 --> 01:02:32,480 you sort elements inside an array in 1678 01:02:29,680 --> 01:02:34,559 python so let's quickly hop into the ide 1679 01:02:32,480 --> 01:02:37,280 and check out how you sort elements 1680 01:02:34,559 --> 01:02:39,440 inside an array right so here we start 1681 01:02:37,280 --> 01:02:41,760 coding 1682 01:02:39,440 --> 01:02:44,160 just putting up the code here so array 1683 01:02:41,760 --> 01:02:48,400 is already defined the elements are 10 1684 01:02:44,160 --> 01:02:50,640 22 38 27 11 so on right so we have 1685 01:02:48,400 --> 01:02:52,480 five elements here to be sorted in 1686 01:02:50,640 --> 01:02:54,480 ascending order and you can also make it 1687 01:02:52,480 --> 01:02:56,960 descending as well i'm showing you for 1688 01:02:54,480 --> 01:02:58,640 ascending order so what is happening 1689 01:02:56,960 --> 01:03:00,960 here i've just put up a comment for 1690 01:02:58,640 --> 01:03:02,960 better understanding displaying elements 1691 01:03:00,960 --> 01:03:05,760 of original array original array in the 1692 01:03:02,960 --> 01:03:08,720 sense whatever it is here is displayed 1693 01:03:05,760 --> 01:03:12,079 first right so next it is sorting by 1694 01:03:08,720 --> 01:03:14,160 using for loop right so every element it 1695 01:03:12,079 --> 01:03:16,319 will chuck and it will try to compare 1696 01:03:14,160 --> 01:03:18,160 with the next element if it is greater 1697 01:03:16,319 --> 01:03:20,160 it will push up 1698 01:03:18,160 --> 01:03:22,799 that particular element to the back and 1699 01:03:20,160 --> 01:03:25,440 whatever it is lesser will come in front 1700 01:03:22,799 --> 01:03:27,520 so this kind of exchange will happen and 1701 01:03:25,440 --> 01:03:29,920 it will sort in ascending order so 1702 01:03:27,520 --> 01:03:32,880 ascending in the sense from smaller to 1703 01:03:29,920 --> 01:03:34,960 higher number so quickly it will display 1704 01:03:32,880 --> 01:03:37,440 after sorting the elements of array 1705 01:03:34,960 --> 01:03:39,760 sorted in ascending order are so and so 1706 01:03:37,440 --> 01:03:43,960 so let's quickly see what is the output 1707 01:03:39,760 --> 01:03:43,960 of this particular code 1708 01:03:44,160 --> 01:03:48,559 right so 1709 01:03:46,160 --> 01:03:53,119 we have original array which we have 1710 01:03:48,559 --> 01:03:54,319 given that is 10 22 38 27 11 and then we 1711 01:03:53,119 --> 01:03:56,799 have 1712 01:03:54,319 --> 01:04:00,880 the sorted array so in ascending order 1713 01:03:56,799 --> 01:04:03,119 it is 10 11 22 27 38 so this is how it 1714 01:04:00,880 --> 01:04:05,680 will sort the major function where it 1715 01:04:03,119 --> 01:04:08,799 will be sorting is we are using this 1716 01:04:05,680 --> 01:04:10,960 particular lines of code which i am just 1717 01:04:08,799 --> 01:04:13,200 highlighting in this particular ide 1718 01:04:10,960 --> 01:04:16,000 where it will compare each and every 1719 01:04:13,200 --> 01:04:18,400 element inside the array 1720 01:04:16,000 --> 01:04:20,640 to the next one if it is greater it will 1721 01:04:18,400 --> 01:04:23,200 push it to back if it is 1722 01:04:20,640 --> 01:04:25,280 lesser than the compar array i mean 1723 01:04:23,200 --> 01:04:27,280 array element it will push it to front 1724 01:04:25,280 --> 01:04:29,599 so this operation will happen in this 1725 01:04:27,280 --> 01:04:31,599 particular lines of code right you can 1726 01:04:29,599 --> 01:04:34,240 also sort by using sort function 1727 01:04:31,599 --> 01:04:37,680 directly as well so this is a simple 1728 01:04:34,240 --> 01:04:41,359 example to know how sorting will happen 1729 01:04:37,680 --> 01:04:44,079 in python now we shall see how do you 1730 01:04:41,359 --> 01:04:46,480 search an element inside an array so 1731 01:04:44,079 --> 01:04:49,440 here i've tried to put up occurrence as 1732 01:04:46,480 --> 01:04:52,720 well so let's quickly search and see 1733 01:04:49,440 --> 01:04:56,160 elements in an array in python id okay 1734 01:04:52,720 --> 01:04:58,799 so this is the code in order to search 1735 01:04:56,160 --> 01:05:01,839 the element also find the occurrence of 1736 01:04:58,799 --> 01:05:04,319 it right so here this is the array set 1737 01:05:01,839 --> 01:05:06,400 so i'm giving number one two three one 1738 01:05:04,319 --> 01:05:07,200 two five so you can see 1739 01:05:06,400 --> 01:05:09,920 one 1740 01:05:07,200 --> 01:05:13,359 2 has been repeated those two integers 1741 01:05:09,920 --> 01:05:15,520 are repeated so first it is showing up 1742 01:05:13,359 --> 01:05:18,079 the created array whatever the array 1743 01:05:15,520 --> 01:05:20,720 which has been given is put up in the 1744 01:05:18,079 --> 01:05:23,680 first place and next what it is doing it 1745 01:05:20,720 --> 01:05:27,039 is trying to find the occurrences of it 1746 01:05:23,680 --> 01:05:30,000 so with the help of index right so the 1747 01:05:27,039 --> 01:05:33,760 element two the number two so where it 1748 01:05:30,000 --> 01:05:35,520 is present and how many times right so 1749 01:05:33,760 --> 01:05:37,440 first where it is present it will show 1750 01:05:35,520 --> 01:05:39,280 that the second time will not be counted 1751 01:05:37,440 --> 01:05:41,119 first occurrence will be counted so 1752 01:05:39,280 --> 01:05:44,240 let's quickly see the output of this 1753 01:05:41,119 --> 01:05:46,640 particular code right so here 1754 01:05:44,240 --> 01:05:49,119 uh the new created array is so whatever 1755 01:05:46,640 --> 01:05:51,599 the given array by the user has been put 1756 01:05:49,119 --> 01:05:54,480 up in the first line and the second line 1757 01:05:51,599 --> 01:05:58,480 it it is saying the first occurrence of 1758 01:05:54,480 --> 01:06:02,079 two at position one why this is zero one 1759 01:05:58,480 --> 01:06:04,880 two three four five right so 1760 01:06:02,079 --> 01:06:08,559 two at the first time is present in the 1761 01:06:04,880 --> 01:06:11,680 index value array 1 right again next it 1762 01:06:08,559 --> 01:06:14,720 is searching for 1 where it is the first 1763 01:06:11,680 --> 01:06:18,160 occurrence of 1 in array is at index 1764 01:06:14,720 --> 01:06:21,200 point 0 right so it is showing the 1765 01:06:18,160 --> 01:06:24,000 output 0. again you have 1 here that is 1766 01:06:21,200 --> 01:06:26,559 0 1 2 3 also you have 1767 01:06:24,000 --> 01:06:29,280 second to in fourth position but still 1768 01:06:26,559 --> 01:06:30,079 wherever it is available at the first is 1769 01:06:29,280 --> 01:06:32,720 being 1770 01:06:30,079 --> 01:06:36,000 demonstrated in this particular program 1771 01:06:32,720 --> 01:06:38,240 right so this is how the occurrence is 1772 01:06:36,000 --> 01:06:40,240 counted also the elements are searched 1773 01:06:38,240 --> 01:06:43,200 in python now let's talk about 1774 01:06:40,240 --> 01:06:45,440 advantages of arrays so obviously when 1775 01:06:43,200 --> 01:06:48,400 you have indexes associated with the 1776 01:06:45,440 --> 01:06:50,720 array right we have indexes so this it 1777 01:06:48,400 --> 01:06:52,480 is easy for us to access any element 1778 01:06:50,720 --> 01:06:54,079 right with the help of this index we 1779 01:06:52,480 --> 01:06:56,880 formed x is the third element you can 1780 01:06:54,079 --> 01:06:59,359 directly go ahead and say ar of 2 in the 1781 01:06:56,880 --> 01:07:01,280 1d array right so we can directly access 1782 01:06:59,359 --> 01:07:03,520 elements with the help of indexing 1783 01:07:01,280 --> 01:07:05,280 similarly it is easy for us to iterate 1784 01:07:03,520 --> 01:07:06,880 through it right with the help of one 1785 01:07:05,280 --> 01:07:09,440 for loop we can iterate through all the 1786 01:07:06,880 --> 01:07:11,440 elements right one by one that is there 1787 01:07:09,440 --> 01:07:13,440 okay that up there in the area and 1788 01:07:11,440 --> 01:07:16,960 similarly if you want to do the sorting 1789 01:07:13,440 --> 01:07:19,119 we can go ahead and easily hydrate 1790 01:07:16,960 --> 01:07:21,119 through one uh these elements one by one 1791 01:07:19,119 --> 01:07:23,119 and look for an element if we are trying 1792 01:07:21,119 --> 01:07:25,039 to search an element in the area let's 1793 01:07:23,119 --> 01:07:26,960 suppose uh 1794 01:07:25,039 --> 01:07:28,400 we are searching for three so one by one 1795 01:07:26,960 --> 01:07:29,839 we will search okay is this element 1796 01:07:28,400 --> 01:07:31,520 three is this element three is this 1797 01:07:29,839 --> 01:07:33,920 element three is this element three is 1798 01:07:31,520 --> 01:07:36,319 this element three right and if this 1799 01:07:33,920 --> 01:07:38,720 element is three we we can easily search 1800 01:07:36,319 --> 01:07:41,200 and also for sorting i let's suppose we 1801 01:07:38,720 --> 01:07:43,440 want to sort this array what would it be 1802 01:07:41,200 --> 01:07:45,680 it will be simply what if you have four 1803 01:07:43,440 --> 01:07:47,920 here three here two here one here and 1804 01:07:45,680 --> 01:07:49,440 let's suppose you have zero here okay so 1805 01:07:47,920 --> 01:07:51,440 now you want to sort this in the 1806 01:07:49,440 --> 01:07:53,359 ascending order so what you will do you 1807 01:07:51,440 --> 01:07:55,440 will use two loops one will 1808 01:07:53,359 --> 01:07:57,839 uh one will focus on this first element 1809 01:07:55,440 --> 01:08:00,160 and then the second uh the second one 1810 01:07:57,839 --> 01:08:02,240 will compare all the elements okay and 1811 01:08:00,160 --> 01:08:05,039 then at the end of this thing you will 1812 01:08:02,240 --> 01:08:08,400 have the largest element at the end of 1813 01:08:05,039 --> 01:08:11,039 the array so sorting hydration searching 1814 01:08:08,400 --> 01:08:12,720 it is easy and airy we just have to i 1815 01:08:11,039 --> 01:08:13,520 trade through all the elements one by 1816 01:08:12,720 --> 01:08:15,599 one 1817 01:08:13,520 --> 01:08:17,199 now it is a replacement of multiple 1818 01:08:15,599 --> 01:08:19,440 variables now what do you mean by this 1819 01:08:17,199 --> 01:08:22,080 thing let's suppose you have an integer 1820 01:08:19,440 --> 01:08:24,640 or let's suppose you want to store uh 1821 01:08:22,080 --> 01:08:26,000 the roll number of 10 students right so 1822 01:08:24,640 --> 01:08:28,560 what you have you would have done 1823 01:08:26,000 --> 01:08:30,319 earlier prior to what when you don't 1824 01:08:28,560 --> 01:08:32,159 know the arrays what you would have done 1825 01:08:30,319 --> 01:08:34,000 you would have said roll number one and 1826 01:08:32,159 --> 01:08:35,679 then it's or you can say 1827 01:08:34,000 --> 01:08:36,799 s1 1828 01:08:35,679 --> 01:08:40,080 s2 1829 01:08:36,799 --> 01:08:42,400 s3 s4 and one by one you can store the 1830 01:08:40,080 --> 01:08:45,600 roll numbers in these 1831 01:08:42,400 --> 01:08:48,000 integer variables right so as soon as 1832 01:08:45,600 --> 01:08:50,159 our students increase now let's suppose 1833 01:08:48,000 --> 01:08:52,239 we are talking about here 10 students 1834 01:08:50,159 --> 01:08:54,239 now as we talk about 100 now what 1835 01:08:52,239 --> 01:08:55,920 happens if we talk about 500 are you 1836 01:08:54,239 --> 01:08:59,120 going to uh 1837 01:08:55,920 --> 01:09:02,000 write 500 variables integer variables s1 1838 01:08:59,120 --> 01:09:04,719 starting from s1 to s500 no it is a very 1839 01:09:02,000 --> 01:09:06,400 inefficient way of doing so right so 1840 01:09:04,719 --> 01:09:08,880 instead what you can do you can create 1841 01:09:06,400 --> 01:09:11,440 an array and you can create an integer 1842 01:09:08,880 --> 01:09:14,239 array and name it student 1843 01:09:11,440 --> 01:09:15,920 and and then you will have the size 1844 01:09:14,239 --> 01:09:17,679 which which obviously represents the 1845 01:09:15,920 --> 01:09:20,000 number of students that are there and in 1846 01:09:17,679 --> 01:09:21,839 this case it will be 500 now if you want 1847 01:09:20,000 --> 01:09:24,400 to change it to tomorrow if you want to 1848 01:09:21,839 --> 01:09:28,080 change it to 600 you can go ahead and 1849 01:09:24,400 --> 01:09:30,239 easily change it to 600 right so 1850 01:09:28,080 --> 01:09:34,080 it is the replacement of multiple 1851 01:09:30,239 --> 01:09:36,239 variables so this is what it means 1852 01:09:34,080 --> 01:09:38,480 now let's clear the screen and now let's 1853 01:09:36,239 --> 01:09:41,040 talk about disadvantages there's one 1854 01:09:38,480 --> 01:09:43,199 disadvantage that can be easily noticed 1855 01:09:41,040 --> 01:09:46,400 is the size now obviously when you're 1856 01:09:43,199 --> 01:09:49,120 talking about 1d array right the size or 1857 01:09:46,400 --> 01:09:52,000 any area right the size is there right 1858 01:09:49,120 --> 01:09:54,000 so you you cannot exceed the size the 1859 01:09:52,000 --> 01:09:56,560 elements cannot exceed this so let's 1860 01:09:54,000 --> 01:09:58,320 suppose you have a the size is five you 1861 01:09:56,560 --> 01:10:01,120 can only store five elements right you 1862 01:09:58,320 --> 01:10:03,600 cannot go more than that or beyond that 1863 01:10:01,120 --> 01:10:05,760 now if you have a size 100 and now 1864 01:10:03,600 --> 01:10:08,159 you're trying to store only two elements 1865 01:10:05,760 --> 01:10:10,080 anyway the 100 memory allocations will 1866 01:10:08,159 --> 01:10:11,920 be there for this array that means you 1867 01:10:10,080 --> 01:10:14,960 are wasting your memory you are not 1868 01:10:11,920 --> 01:10:17,520 utilizing it efficiently okay so this is 1869 01:10:14,960 --> 01:10:19,679 what it means that size is fixed and you 1870 01:10:17,520 --> 01:10:22,560 cannot store more elements and if the 1871 01:10:19,679 --> 01:10:26,400 capacity is more than occupancy most of 1872 01:10:22,560 --> 01:10:28,800 the array gets wasted okay so 1873 01:10:26,400 --> 01:10:31,199 these are two things apart from this you 1874 01:10:28,800 --> 01:10:33,679 need a continuous memory allocation that 1875 01:10:31,199 --> 01:10:36,480 means if chunks of memory are available 1876 01:10:33,679 --> 01:10:38,800 here and there you cannot store an area 1877 01:10:36,480 --> 01:10:40,960 which is let's suppose here you have 16 1878 01:10:38,800 --> 01:10:44,080 bytes and here you have 16 bytes 1879 01:10:40,960 --> 01:10:46,080 only if you have an array which is of 16 1880 01:10:44,080 --> 01:10:48,880 bytes that means if you have an array of 1881 01:10:46,080 --> 01:10:51,440 size 4 that it can be stored here but if 1882 01:10:48,880 --> 01:10:53,360 you have an area of it suppose size 8 1883 01:10:51,440 --> 01:10:55,040 you cannot store four elements here and 1884 01:10:53,360 --> 01:10:57,120 four elements here that will not be 1885 01:10:55,040 --> 01:10:59,199 happening okay that cannot happen rather 1886 01:10:57,120 --> 01:11:01,199 okay because it needs continuous can be 1887 01:10:59,199 --> 01:11:04,080 a location so there is one more 1888 01:11:01,199 --> 01:11:07,120 disadvantage and the last but not the 1889 01:11:04,080 --> 01:11:10,560 least is that insertion and deletion is 1890 01:11:07,120 --> 01:11:12,400 difficult now why do you say that let's 1891 01:11:10,560 --> 01:11:14,159 suppose you have an array and you have 1892 01:11:12,400 --> 01:11:15,920 having in this array one two three four 1893 01:11:14,159 --> 01:11:19,120 now let's suppose you want to insert a 1894 01:11:15,920 --> 01:11:21,600 value zero at this location now what you 1895 01:11:19,120 --> 01:11:23,760 need to do you insert the zero and rest 1896 01:11:21,600 --> 01:11:26,400 of the elements every element will be 1897 01:11:23,760 --> 01:11:28,480 swapped so swapping is required right 1898 01:11:26,400 --> 01:11:30,560 swapping is required plus there should 1899 01:11:28,480 --> 01:11:33,280 be memory available so that you can 1900 01:11:30,560 --> 01:11:35,440 store that element else if there are 1901 01:11:33,280 --> 01:11:38,080 only four elements and now you want to 1902 01:11:35,440 --> 01:11:39,920 store zero and the size is also four and 1903 01:11:38,080 --> 01:11:41,920 that time around what happens you will 1904 01:11:39,920 --> 01:11:43,920 store one and rest of the elements will 1905 01:11:41,920 --> 01:11:47,280 be swapped and you will be losing this 1906 01:11:43,920 --> 01:11:49,040 value so it is very difficult to 1907 01:11:47,280 --> 01:11:50,719 insert the value now same thing will 1908 01:11:49,040 --> 01:11:52,640 happen when you are trying to delete but 1909 01:11:50,719 --> 01:11:55,520 at that time you will not be losing data 1910 01:11:52,640 --> 01:11:57,840 but yes swapping is required right so 1911 01:11:55,520 --> 01:11:59,440 let's suppose you are you want to delete 1912 01:11:57,840 --> 01:12:00,880 this location so what you will do or 1913 01:11:59,440 --> 01:12:02,719 delete this number what you will do you 1914 01:12:00,880 --> 01:12:04,480 will overwrite this with three you will 1915 01:12:02,719 --> 01:12:06,159 overwrite this with four and let's 1916 01:12:04,480 --> 01:12:08,000 suppose if there is six you will write 1917 01:12:06,159 --> 01:12:10,719 this with six so at the end you will 1918 01:12:08,000 --> 01:12:13,280 have one three four six right one three 1919 01:12:10,719 --> 01:12:14,960 four six and one memory location is 1920 01:12:13,280 --> 01:12:16,960 there and it will contain the same 1921 01:12:14,960 --> 01:12:19,199 element that is six so next time around 1922 01:12:16,960 --> 01:12:21,520 you will just override this so again the 1923 01:12:19,199 --> 01:12:23,760 swapping is required so it is very 1924 01:12:21,520 --> 01:12:25,600 difficult to insert and delete an 1925 01:12:23,760 --> 01:12:27,520 element in the area 1926 01:12:25,600 --> 01:12:29,520 so now moving to the agenda we will 1927 01:12:27,520 --> 01:12:31,840 start this course by knowing what is 1928 01:12:29,520 --> 01:12:33,760 stack what are the examples of stack 1929 01:12:31,840 --> 01:12:36,960 then we will see some functions 1930 01:12:33,760 --> 01:12:38,080 associated with the stack such as push 1931 01:12:36,960 --> 01:12:41,199 pop 1932 01:12:38,080 --> 01:12:43,760 top and many more and at last stack 1933 01:12:41,199 --> 01:12:45,520 implementation can be done through list 1934 01:12:43,760 --> 01:12:47,920 through collections 1935 01:12:45,520 --> 01:12:49,440 and through queue we will discuss these 1936 01:12:47,920 --> 01:12:51,440 ways in detail 1937 01:12:49,440 --> 01:12:53,360 now let's see the concept of stack now 1938 01:12:51,440 --> 01:12:55,679 coming to the stack stack is a linear 1939 01:12:53,360 --> 01:12:57,920 data structure which follows last in 1940 01:12:55,679 --> 01:12:59,600 first out order that means the element 1941 01:12:57,920 --> 01:13:00,960 which are inserted at last will be 1942 01:12:59,600 --> 01:13:03,600 removed first 1943 01:13:00,960 --> 01:13:06,159 that is lifo order 1944 01:13:03,600 --> 01:13:08,000 last in first out now 1945 01:13:06,159 --> 01:13:08,719 insertion and removal of the element has 1946 01:13:08,000 --> 01:13:11,440 done 1947 01:13:08,719 --> 01:13:13,600 at one end i will explain you now so 1948 01:13:11,440 --> 01:13:19,280 let's see an example of stack 1949 01:13:13,600 --> 01:13:22,159 so here if i'm having 23 45 67 89 11 1950 01:13:19,280 --> 01:13:24,880 and let's suppose i'm having 50 so these 1951 01:13:22,159 --> 01:13:26,159 are the elements that has to be inserted 1952 01:13:24,880 --> 01:13:28,239 in this stack 1953 01:13:26,159 --> 01:13:30,800 so now what i will do so this is my 1954 01:13:28,239 --> 01:13:32,560 empty stack let's suppose that 1955 01:13:30,800 --> 01:13:35,199 and inside this stack 1956 01:13:32,560 --> 01:13:38,159 i will insert these elements one by one 1957 01:13:35,199 --> 01:13:41,520 so first i will insert 23 1958 01:13:38,159 --> 01:13:44,239 after 23 i will insert 45 1959 01:13:41,520 --> 01:13:45,280 then i will insert 67 1960 01:13:44,239 --> 01:13:47,840 after 1961 01:13:45,280 --> 01:13:49,679 89 1962 01:13:47,840 --> 01:13:53,280 11 1963 01:13:49,679 --> 01:13:55,520 and 15 so this is my stack now as i told 1964 01:13:53,280 --> 01:13:57,920 you the element which is inserted at the 1965 01:13:55,520 --> 01:14:00,560 last will be removed first that means 1966 01:13:57,920 --> 01:14:02,880 last in first out or leaf order so you 1967 01:14:00,560 --> 01:14:05,440 have seen here 15 is the last element 1968 01:14:02,880 --> 01:14:07,520 that has been inserted here so now if 1969 01:14:05,440 --> 01:14:09,040 you want to remove the element then 15 1970 01:14:07,520 --> 01:14:11,520 will be the first element that will be 1971 01:14:09,040 --> 01:14:14,000 removed so for insertion we are using 1972 01:14:11,520 --> 01:14:15,920 push so push was used to insert the 1973 01:14:14,000 --> 01:14:18,080 element and pop will be using to remove 1974 01:14:15,920 --> 01:14:21,360 an element from the stack so 15 is the 1975 01:14:18,080 --> 01:14:24,000 last element that was inserted so now 1976 01:14:21,360 --> 01:14:27,040 i will be using pop to remove this 15 so 1977 01:14:24,000 --> 01:14:29,679 once 15 has been removed then i'm having 1978 01:14:27,040 --> 01:14:31,840 element 23 1979 01:14:29,679 --> 01:14:34,080 45 1980 01:14:31,840 --> 01:14:36,640 67 1981 01:14:34,080 --> 01:14:36,640 89 1982 01:14:36,719 --> 01:14:41,040 and then 11 right so once again if i 1983 01:14:38,960 --> 01:14:43,199 want to remove the element then my 11 1984 01:14:41,040 --> 01:14:45,840 will be removed so once again i will 1985 01:14:43,199 --> 01:14:47,280 write here pop so always remember that 1986 01:14:45,840 --> 01:14:49,360 push operation will be used for the 1987 01:14:47,280 --> 01:14:51,280 insertion and pop operation will be used 1988 01:14:49,360 --> 01:14:55,360 for the removal so whatever the elements 1989 01:14:51,280 --> 01:14:57,760 i was inserting here 23 45 67 89 11 15 i 1990 01:14:55,360 --> 01:15:00,800 was using push operation so if i'm 1991 01:14:57,760 --> 01:15:02,800 writing push 23 then 23 was 1992 01:15:00,800 --> 01:15:04,640 inserted then after that if i'm writing 1993 01:15:02,800 --> 01:15:07,600 push 45 1994 01:15:04,640 --> 01:15:10,640 then 45 was inserted and after that if 1995 01:15:07,600 --> 01:15:12,400 i'm writing push 67 then 67 was inserted 1996 01:15:10,640 --> 01:15:15,520 and in this way i can use the push 1997 01:15:12,400 --> 01:15:17,600 function to insert the element now 1998 01:15:15,520 --> 01:15:19,920 as i written here insertion and removal 1999 01:15:17,600 --> 01:15:21,360 of the element has done at one end why 2000 01:15:19,920 --> 01:15:23,280 if you see 2001 01:15:21,360 --> 01:15:25,360 this was my stack 2002 01:15:23,280 --> 01:15:27,440 right so this is my stack so whatever 2003 01:15:25,360 --> 01:15:30,640 the element i was inserting in an empty 2004 01:15:27,440 --> 01:15:32,800 stack i was inserting it through one end 2005 01:15:30,640 --> 01:15:34,719 right and i was doing insertion through 2006 01:15:32,800 --> 01:15:37,199 push operation now 2007 01:15:34,719 --> 01:15:38,800 if i'm doing the pop operation then also 2008 01:15:37,199 --> 01:15:41,199 i am doing the pop operation through one 2009 01:15:38,800 --> 01:15:43,040 end so that's why you can see that here 2010 01:15:41,199 --> 01:15:44,640 it is written that insertion and removal 2011 01:15:43,040 --> 01:15:46,960 of the element has done at one end so 2012 01:15:44,640 --> 01:15:48,880 this was the basic concept of stack now 2013 01:15:46,960 --> 01:15:51,199 let's see the example of stack so you 2014 01:15:48,880 --> 01:15:53,360 can see here this is my pile of coin 2015 01:15:51,199 --> 01:15:55,520 right so this can be considered as the 2016 01:15:53,360 --> 01:15:58,239 example of a stack why because the last 2017 01:15:55,520 --> 01:16:00,800 coin is removing first here so this 2018 01:15:58,239 --> 01:16:03,040 follows last in first out so i'll remove 2019 01:16:00,800 --> 01:16:05,120 one coin one coin so if i'm reviewing 2020 01:16:03,040 --> 01:16:06,560 step by step that means the last coin 2021 01:16:05,120 --> 01:16:08,320 will remove first and in this way if i 2022 01:16:06,560 --> 01:16:10,000 will follow then you can see that i can 2023 01:16:08,320 --> 01:16:11,920 remove one coin one after other one 2024 01:16:10,000 --> 01:16:14,000 after other and in this way this will be 2025 01:16:11,920 --> 01:16:16,080 the example of stack similarly the same 2026 01:16:14,000 --> 01:16:18,640 example goes for the dvd if i am 2027 01:16:16,080 --> 01:16:20,640 removing one dvd after other then this 2028 01:16:18,640 --> 01:16:22,719 can be example of stack so the dvd which 2029 01:16:20,640 --> 01:16:25,040 was inserted at last will be removed 2030 01:16:22,719 --> 01:16:26,719 first the same goes for the books the 2031 01:16:25,040 --> 01:16:28,880 book which is on the top will remove 2032 01:16:26,719 --> 01:16:30,960 first and after that if i am going one 2033 01:16:28,880 --> 01:16:33,440 by one from the top so you can see that 2034 01:16:30,960 --> 01:16:35,120 the last book that was kept will remove 2035 01:16:33,440 --> 01:16:37,280 first and in this way this can be the 2036 01:16:35,120 --> 01:16:39,360 example of stack so this was the basic 2037 01:16:37,280 --> 01:16:41,520 example of stack now let's see some 2038 01:16:39,360 --> 01:16:43,440 functions associated with stack so we 2039 01:16:41,520 --> 01:16:46,080 are having push function so as i told 2040 01:16:43,440 --> 01:16:48,560 you that if i'm writing here push 23 and 2041 01:16:46,080 --> 01:16:50,800 let's suppose this is my stack 2042 01:16:48,560 --> 01:16:53,120 so this is an empty stack so it will 2043 01:16:50,800 --> 01:16:54,800 insert 23u 2044 01:16:53,120 --> 01:16:57,040 so here you can see that it is used to 2045 01:16:54,800 --> 01:16:57,840 insert the element x at the end of stack 2046 01:16:57,040 --> 01:17:00,080 so 2047 01:16:57,840 --> 01:17:02,480 here instead of x if i'm writing 23 then 2048 01:17:00,080 --> 01:17:04,159 it will insert 23. similarly pop 2049 01:17:02,480 --> 01:17:06,880 function as i told you that power will 2050 01:17:04,159 --> 01:17:08,719 remove the element from the stack 2051 01:17:06,880 --> 01:17:10,800 so it is used to remove the topmost or 2052 01:17:08,719 --> 01:17:13,040 last element of the stack so if there is 2053 01:17:10,800 --> 01:17:16,239 only one element in the stack 23 and if 2054 01:17:13,040 --> 01:17:17,840 i am writing pop then it will remove 23 2055 01:17:16,239 --> 01:17:19,280 right and also 2056 01:17:17,840 --> 01:17:21,679 please remember that it will remove the 2057 01:17:19,280 --> 01:17:23,360 topmost or last element in case of this 2058 01:17:21,679 --> 01:17:25,040 stack we are having only one element so 2059 01:17:23,360 --> 01:17:27,840 this will be the last element so if i am 2060 01:17:25,040 --> 01:17:30,000 writing pop then 23 will be removed but 2061 01:17:27,840 --> 01:17:32,960 what if i am writing here push let's 2062 01:17:30,000 --> 01:17:35,520 suppose that 25 then 25 will be inserted 2063 01:17:32,960 --> 01:17:37,760 here and once again if i'm writing pop 2064 01:17:35,520 --> 01:17:39,600 so this will be the last element so 25 2065 01:17:37,760 --> 01:17:41,280 will be removed 2066 01:17:39,600 --> 01:17:43,920 so this was the basic idea about push 2067 01:17:41,280 --> 01:17:45,920 and pop function now coming to the size 2068 01:17:43,920 --> 01:17:47,840 so size function will give me the size 2069 01:17:45,920 --> 01:17:50,000 or you can say the length of the stack 2070 01:17:47,840 --> 01:17:51,920 next we are having top so it will give 2071 01:17:50,000 --> 01:17:54,480 the reference of the last element 2072 01:17:51,920 --> 01:17:56,080 present in the stack so let's suppose 2073 01:17:54,480 --> 01:17:59,840 that this is my stack 2074 01:17:56,080 --> 01:18:03,679 and i am having 23 25 and let's suppose 2075 01:17:59,840 --> 01:18:05,040 27 so this is my last element here 2076 01:18:03,679 --> 01:18:07,360 so top function will give me the 2077 01:18:05,040 --> 01:18:08,400 reference of this last element 2078 01:18:07,360 --> 01:18:10,400 now 2079 01:18:08,400 --> 01:18:13,440 coming to the empty function so empty 2080 01:18:10,400 --> 01:18:14,320 function returns true for an empty stack 2081 01:18:13,440 --> 01:18:16,159 so 2082 01:18:14,320 --> 01:18:18,239 if this is a stack 2083 01:18:16,159 --> 01:18:21,120 and if this stack is empty then the 2084 01:18:18,239 --> 01:18:22,960 empty function will return us true right 2085 01:18:21,120 --> 01:18:25,120 so this was the basic idea about 2086 01:18:22,960 --> 01:18:27,520 functions in stack and what will be the 2087 01:18:25,120 --> 01:18:29,199 time complexity for each function so 2088 01:18:27,520 --> 01:18:31,280 here the time complexity for each 2089 01:18:29,199 --> 01:18:34,320 functions will be big o of 1 2090 01:18:31,280 --> 01:18:36,159 for push pop size stop and empty so for 2091 01:18:34,320 --> 01:18:38,159 every function time complexity will be 2092 01:18:36,159 --> 01:18:40,080 big o of 1 so this was the basic idea 2093 01:18:38,159 --> 01:18:42,000 about the functions now let's see the 2094 01:18:40,080 --> 01:18:44,000 stack implementation so there are 2095 01:18:42,000 --> 01:18:46,320 several ways to implement stack in 2096 01:18:44,000 --> 01:18:48,640 python we can use list 2097 01:18:46,320 --> 01:18:50,960 we can use collection module from where 2098 01:18:48,640 --> 01:18:52,719 we can provide dq class and we can also 2099 01:18:50,960 --> 01:18:54,320 implement through queue module so these 2100 01:18:52,719 --> 01:18:56,400 are some ways from which we can 2101 01:18:54,320 --> 01:18:58,560 implement stack in python so now let's 2102 01:18:56,400 --> 01:19:00,880 see the implementation using list so in 2103 01:18:58,560 --> 01:19:03,199 implementation using list list in python 2104 01:19:00,880 --> 01:19:05,840 can be used as a stack so we can use 2105 01:19:03,199 --> 01:19:07,920 list as a stack in python so in python 2106 01:19:05,840 --> 01:19:09,679 we are having append and pop function we 2107 01:19:07,920 --> 01:19:11,199 don't have any push function in python 2108 01:19:09,679 --> 01:19:13,040 so if you want to insert the element we 2109 01:19:11,199 --> 01:19:14,719 need some function right so we can use 2110 01:19:13,040 --> 01:19:16,880 the append function which is used to 2111 01:19:14,719 --> 01:19:18,239 insert the element now coming to the pop 2112 01:19:16,880 --> 01:19:21,040 function yeah we are having power 2113 01:19:18,239 --> 01:19:23,840 function in python and pop removes the 2114 01:19:21,040 --> 01:19:25,920 element in the lifo order that means 2115 01:19:23,840 --> 01:19:27,840 last in first out and as we know that 2116 01:19:25,920 --> 01:19:29,920 our stack also follow the leaf order the 2117 01:19:27,840 --> 01:19:32,000 elements which are inserted at last will 2118 01:19:29,920 --> 01:19:34,159 be removed first so these two are the 2119 01:19:32,000 --> 01:19:35,040 functions that we will be using here in 2120 01:19:34,159 --> 01:19:35,840 list 2121 01:19:35,040 --> 01:19:37,920 now 2122 01:19:35,840 --> 01:19:40,239 let's see the logic of this as i told 2123 01:19:37,920 --> 01:19:42,480 you that list in python can be used as a 2124 01:19:40,239 --> 01:19:44,640 stack right so here i am using list as a 2125 01:19:42,480 --> 01:19:46,480 stack so this is my stack variable and 2126 01:19:44,640 --> 01:19:48,400 this is an empty list 2127 01:19:46,480 --> 01:19:50,480 and now as i told you that if you want 2128 01:19:48,400 --> 01:19:53,440 to insert the element then you can use 2129 01:19:50,480 --> 01:19:55,920 append so this was my empty list that is 2130 01:19:53,440 --> 01:19:58,080 stack and as we know that in python list 2131 01:19:55,920 --> 01:20:00,400 is denoted by square brackets so now 2132 01:19:58,080 --> 01:20:01,520 what i will do i will write here stack 2133 01:20:00,400 --> 01:20:03,120 dot append 2134 01:20:01,520 --> 01:20:05,760 [Music] 2135 01:20:03,120 --> 01:20:08,639 and inside this append if i'm writing x 2136 01:20:05,760 --> 01:20:10,639 so x will be inserted in my list 2137 01:20:08,639 --> 01:20:13,280 now now coming to the pop function if 2138 01:20:10,639 --> 01:20:15,360 i'm writing here stack dot pop and if 2139 01:20:13,280 --> 01:20:17,760 i'm writing print and inside that if i'm 2140 01:20:15,360 --> 01:20:19,760 putting it then whatever the element i'm 2141 01:20:17,760 --> 01:20:21,840 having it will remove so let's suppose 2142 01:20:19,760 --> 01:20:24,080 that this is my stack and in this if i'm 2143 01:20:21,840 --> 01:20:26,719 having x element so if i'm writing stack 2144 01:20:24,080 --> 01:20:28,320 dot pop so it will remove this x element 2145 01:20:26,719 --> 01:20:30,400 right because i am having only one 2146 01:20:28,320 --> 01:20:32,000 element here so the last element will be 2147 01:20:30,400 --> 01:20:34,239 removed from the stack so this is the 2148 01:20:32,000 --> 01:20:36,560 basic idea from where stack can be 2149 01:20:34,239 --> 01:20:38,239 implemented using list now let's see the 2150 01:20:36,560 --> 01:20:40,480 practical example 2151 01:20:38,239 --> 01:20:42,639 so now for practical implementation i 2152 01:20:40,480 --> 01:20:44,800 will be using jupyter notebook so i will 2153 01:20:42,639 --> 01:20:46,320 click on here new and then i will go for 2154 01:20:44,800 --> 01:20:49,040 python python3 2155 01:20:46,320 --> 01:20:52,560 and if i'm writing here 2156 01:20:49,040 --> 01:20:52,560 i'll give the name here stack 2157 01:20:53,199 --> 01:20:57,040 and let me comment it down first here i 2158 01:20:55,520 --> 01:20:58,800 will write here 2159 01:20:57,040 --> 01:21:01,800 hashtag and i will write here 2160 01:20:58,800 --> 01:21:01,800 implementation 2161 01:21:02,800 --> 01:21:05,199 using 2162 01:21:05,440 --> 01:21:09,040 list 2163 01:21:06,960 --> 01:21:11,280 so as i told you that stack can be 2164 01:21:09,040 --> 01:21:13,840 implemented using list so i will create 2165 01:21:11,280 --> 01:21:15,760 a stack variable 2166 01:21:13,840 --> 01:21:17,760 and this will contain list this is an 2167 01:21:15,760 --> 01:21:19,120 empty list and after that i will write 2168 01:21:17,760 --> 01:21:21,920 here stack 2169 01:21:19,120 --> 01:21:21,920 dot append 2170 01:21:22,719 --> 01:21:28,080 and 2171 01:21:23,679 --> 01:21:30,159 inside this if i am writing here welcome 2172 01:21:28,080 --> 01:21:32,960 after that once again i'm writing stack 2173 01:21:30,159 --> 01:21:32,960 dot append 2174 01:21:33,520 --> 01:21:37,040 and i will write here now 2 2175 01:21:37,679 --> 01:21:42,280 once again i will write stack dot append 2176 01:21:44,000 --> 01:21:47,040 all right great learning 2177 01:21:49,440 --> 01:21:54,960 so you can see that this is my append 2178 01:21:51,120 --> 01:21:54,960 now if i'm printing my stack 2179 01:21:55,679 --> 01:22:00,080 so i will click on run button so you can 2180 01:21:57,679 --> 01:22:01,120 see that this is my list and earlier my 2181 01:22:00,080 --> 01:22:03,600 list was 2182 01:22:01,120 --> 01:22:05,520 empty but now through append function i 2183 01:22:03,600 --> 01:22:07,920 have inserted welcome to great learning 2184 01:22:05,520 --> 01:22:09,440 so now this is my list now what i will 2185 01:22:07,920 --> 01:22:11,520 do here 2186 01:22:09,440 --> 01:22:13,920 from this stack i want to remove the 2187 01:22:11,520 --> 01:22:15,840 element so for that i will be using pop 2188 01:22:13,920 --> 01:22:16,800 function so i will write here 2189 01:22:15,840 --> 01:22:19,040 stack 2190 01:22:16,800 --> 01:22:21,199 dot pop 2191 01:22:19,040 --> 01:22:24,639 and i will put this stack dot pop inside 2192 01:22:21,199 --> 01:22:26,480 a print function so i'll write print and 2193 01:22:24,639 --> 01:22:28,719 now let me execute this 2194 01:22:26,480 --> 01:22:31,679 so on executing you can see that i am 2195 01:22:28,719 --> 01:22:33,920 getting great learning so that means 2196 01:22:31,679 --> 01:22:36,320 the element which was inserted at the 2197 01:22:33,920 --> 01:22:38,159 last has removed first right and as i 2198 01:22:36,320 --> 01:22:40,800 told you that pop will always follow the 2199 01:22:38,159 --> 01:22:42,639 leaf order last in first out so if i'm 2200 01:22:40,800 --> 01:22:44,639 printing my stack you can see that i am 2201 01:22:42,639 --> 01:22:46,400 getting welcome to because great 2202 01:22:44,639 --> 01:22:48,719 learning has been removed through for 2203 01:22:46,400 --> 01:22:51,040 function if once again i am writing here 2204 01:22:48,719 --> 01:22:52,800 let me copy and paste this 2205 01:22:51,040 --> 01:22:54,000 ctrl c 2206 01:22:52,800 --> 01:22:55,840 ctrl v 2207 01:22:54,000 --> 01:22:58,960 so once again i am performing here stack 2208 01:22:55,840 --> 01:23:00,800 dot pop and if i'm printing stack 2209 01:22:58,960 --> 01:23:02,719 then you can see that i will be getting 2210 01:23:00,800 --> 01:23:05,199 welcome only 2211 01:23:02,719 --> 01:23:07,280 so you can see that welcome i am getting 2212 01:23:05,199 --> 01:23:09,120 and here stack dot pop if i am doing 2213 01:23:07,280 --> 01:23:11,520 then 2 has been removed 2214 01:23:09,120 --> 01:23:13,679 right so clearly we can see that we can 2215 01:23:11,520 --> 01:23:15,760 implement stack using list through 2216 01:23:13,679 --> 01:23:18,239 append and pop function so this was the 2217 01:23:15,760 --> 01:23:20,560 basic idea about stack implementation 2218 01:23:18,239 --> 01:23:22,800 using list so the another way the stack 2219 01:23:20,560 --> 01:23:24,639 can be implementation using dq so we'll 2220 01:23:22,800 --> 01:23:27,360 see the concept of implementation using 2221 01:23:24,639 --> 01:23:28,960 correction dot dq so here stacks in 2222 01:23:27,360 --> 01:23:31,760 python are created by the collection 2223 01:23:28,960 --> 01:23:33,360 module which provides dq class so now 2224 01:23:31,760 --> 01:23:35,520 let's understand this is a collection 2225 01:23:33,360 --> 01:23:36,880 module so in python i will write here 2226 01:23:35,520 --> 01:23:38,159 from 2227 01:23:36,880 --> 01:23:40,639 collections 2228 01:23:38,159 --> 01:23:42,719 so from collections module i will import 2229 01:23:40,639 --> 01:23:44,880 my dq class 2230 01:23:42,719 --> 01:23:47,520 right so i will write from collections 2231 01:23:44,880 --> 01:23:49,840 and then i will write here import 2232 01:23:47,520 --> 01:23:49,840 dq 2233 01:23:50,080 --> 01:23:54,719 so dq here is double ended queue and 2234 01:23:52,639 --> 01:23:57,040 here append and pop operations are 2235 01:23:54,719 --> 01:24:00,400 faster as compared to list why because 2236 01:23:57,040 --> 01:24:02,320 the time complexity of dq is big o of 1 2237 01:24:00,400 --> 01:24:04,639 whereas the time complexity of list is 2238 01:24:02,320 --> 01:24:06,480 big o of n and also in list if you are 2239 01:24:04,639 --> 01:24:08,560 inserting more element then the list 2240 01:24:06,480 --> 01:24:10,719 will grow and it will go out of a block 2241 01:24:08,560 --> 01:24:12,800 of memory so python have to allocate 2242 01:24:10,719 --> 01:24:15,040 some memory so that's why on inserting 2243 01:24:12,800 --> 01:24:17,840 more element in a list the list will 2244 01:24:15,040 --> 01:24:19,760 become slow so that's why we come with 2245 01:24:17,840 --> 01:24:22,320 another way from the collection module 2246 01:24:19,760 --> 01:24:24,480 we import dq and then so i will create 2247 01:24:22,320 --> 01:24:25,600 my stack variable and inside that i will 2248 01:24:24,480 --> 01:24:27,199 assign my 2249 01:24:25,600 --> 01:24:29,360 dq 2250 01:24:27,199 --> 01:24:31,199 right so now i will perform the same 2251 01:24:29,360 --> 01:24:32,800 operation that i was performing in list 2252 01:24:31,199 --> 01:24:34,880 i will write here append 2253 01:24:32,800 --> 01:24:37,120 and pop 2254 01:24:34,880 --> 01:24:39,040 so always remember that dq will be 2255 01:24:37,120 --> 01:24:40,639 preferred more as compared to list 2256 01:24:39,040 --> 01:24:43,199 because the append and pop operations 2257 01:24:40,639 --> 01:24:45,920 are faster here right and rest all the 2258 01:24:43,199 --> 01:24:48,080 concept is same so let me execute it so 2259 01:24:45,920 --> 01:24:49,760 now let's see the implementation with dq 2260 01:24:48,080 --> 01:24:51,760 so let me comment it down here i will 2261 01:24:49,760 --> 01:24:53,360 write here implementation 2262 01:24:51,760 --> 01:24:54,880 using 2263 01:24:53,360 --> 01:24:57,679 dq 2264 01:24:54,880 --> 01:25:00,480 now after this as i told you that if i 2265 01:24:57,679 --> 01:25:02,800 want to implement dq then is the class 2266 01:25:00,480 --> 01:25:04,560 right so i have to import it from the 2267 01:25:02,800 --> 01:25:06,239 collection module so for that i am 2268 01:25:04,560 --> 01:25:08,320 writing here from 2269 01:25:06,239 --> 01:25:10,000 collections 2270 01:25:08,320 --> 01:25:11,679 import 2271 01:25:10,000 --> 01:25:13,360 dequeue 2272 01:25:11,679 --> 01:25:16,560 and i will write stack variable and 2273 01:25:13,360 --> 01:25:19,199 inside this i will assign the dq 2274 01:25:16,560 --> 01:25:20,960 now after this i will write here stack 2275 01:25:19,199 --> 01:25:25,040 dot append 2276 01:25:20,960 --> 01:25:28,560 and let me write the value as xu 2277 01:25:25,040 --> 01:25:28,560 and if i'm printing my stack 2278 01:25:30,400 --> 01:25:35,440 so on execution 2279 01:25:32,800 --> 01:25:37,760 you can see that i'm getting my dq as x 2280 01:25:35,440 --> 01:25:39,520 now let me append some more values so i 2281 01:25:37,760 --> 01:25:42,080 will write here stack 2282 01:25:39,520 --> 01:25:44,320 dot append 2283 01:25:42,080 --> 01:25:44,320 y 2284 01:25:44,400 --> 01:25:48,320 and after that i will write here stack 2285 01:25:46,800 --> 01:25:50,400 dot append 2286 01:25:48,320 --> 01:25:52,080 let's suppose that 2287 01:25:50,400 --> 01:25:54,400 and once again if i'm executing so i 2288 01:25:52,080 --> 01:25:56,639 will write here print 2289 01:25:54,400 --> 01:25:58,400 stack 2290 01:25:56,639 --> 01:26:00,560 and on execution 2291 01:25:58,400 --> 01:26:02,639 you can see that i am getting xyz now 2292 01:26:00,560 --> 01:26:03,600 let's perform pop operation so i'll 2293 01:26:02,639 --> 01:26:04,960 write here 2294 01:26:03,600 --> 01:26:06,840 stack 2295 01:26:04,960 --> 01:26:09,920 dot 2296 01:26:06,840 --> 01:26:13,840 pop and let me 2297 01:26:09,920 --> 01:26:15,520 put inside this into the paint function 2298 01:26:13,840 --> 01:26:17,600 so as we know that if i'm writing here 2299 01:26:15,520 --> 01:26:19,679 stack dot pop so the last element which 2300 01:26:17,600 --> 01:26:21,360 was inserted will remove first so that 2301 01:26:19,679 --> 01:26:23,760 will be removed here 2302 01:26:21,360 --> 01:26:27,760 so you can see that it's z has been 2303 01:26:23,760 --> 01:26:27,760 removed now if i'm printing my stack 2304 01:26:28,560 --> 01:26:33,040 so i'm getting here only x and y so you 2305 01:26:31,120 --> 01:26:35,440 can see that list and dq are the same 2306 01:26:33,040 --> 01:26:38,000 the only difference is that dq is faster 2307 01:26:35,440 --> 01:26:40,800 because the append and operations are 2308 01:26:38,000 --> 01:26:43,199 faster in dq so this was the basic idea 2309 01:26:40,800 --> 01:26:44,960 about the stack implementation using dq 2310 01:26:43,199 --> 01:26:46,960 now let's see the stack implementation 2311 01:26:44,960 --> 01:26:48,960 using queue so here in implementation 2312 01:26:46,960 --> 01:26:51,600 using queue queue module contains the 2313 01:26:48,960 --> 01:26:53,280 lifo q that means last in first out so 2314 01:26:51,600 --> 01:26:55,360 here basically what happens here it 2315 01:26:53,280 --> 01:26:57,120 works same as the stack but it is having 2316 01:26:55,360 --> 01:26:58,639 some additional functions so it is 2317 01:26:57,120 --> 01:27:00,320 having some additional functions and 2318 01:26:58,639 --> 01:27:03,440 works same as a stack 2319 01:27:00,320 --> 01:27:06,080 right now we have seen that in list as 2320 01:27:03,440 --> 01:27:08,239 well as in dq we were using pop as well 2321 01:27:06,080 --> 01:27:10,239 as append operation right but here to 2322 01:27:08,239 --> 01:27:12,000 insert the element we will be using put 2323 01:27:10,239 --> 01:27:14,960 operation so if i am writing here put of 2324 01:27:12,000 --> 01:27:17,120 3 then that means it will insert 3 in my 2325 01:27:14,960 --> 01:27:19,199 stack so similarly if i am writing here 2326 01:27:17,120 --> 01:27:21,679 get function so it will remove the 2327 01:27:19,199 --> 01:27:23,760 element and as i told you that it works 2328 01:27:21,679 --> 01:27:25,600 same as the stack so the last element 2329 01:27:23,760 --> 01:27:27,199 will be removed first here now we are 2330 01:27:25,600 --> 01:27:29,199 having some functions available in the 2331 01:27:27,199 --> 01:27:31,280 queue module so the first function that 2332 01:27:29,199 --> 01:27:32,719 is get so as i already told you in get 2333 01:27:31,280 --> 01:27:35,520 function it is used to remove the 2334 01:27:32,719 --> 01:27:37,040 element now coming to the max size so 2335 01:27:35,520 --> 01:27:38,880 here max size means the number of 2336 01:27:37,040 --> 01:27:39,760 maximum elements that are present in the 2337 01:27:38,880 --> 01:27:41,199 queue 2338 01:27:39,760 --> 01:27:43,679 coming to the next function we are 2339 01:27:41,199 --> 01:27:45,520 having empty function so if a queue is 2340 01:27:43,679 --> 01:27:48,239 empty then it will return true or else 2341 01:27:45,520 --> 01:27:50,800 in other case it will return false 2342 01:27:48,239 --> 01:27:53,199 next full so whenever the queue is full 2343 01:27:50,800 --> 01:27:55,120 it will give us true 2344 01:27:53,199 --> 01:27:56,719 similarly put i have already discussed 2345 01:27:55,120 --> 01:27:58,719 about the put that if you are inserting 2346 01:27:56,719 --> 01:28:01,520 any element so you can write the put and 2347 01:27:58,719 --> 01:28:05,040 suppose if i am inserting here 2 so it 2348 01:28:01,520 --> 01:28:07,120 will insert 2 in a queue now cue size so 2349 01:28:05,040 --> 01:28:08,960 queue size will give me the size of a 2350 01:28:07,120 --> 01:28:11,199 queue so let's suppose that if you are 2351 01:28:08,960 --> 01:28:14,159 having a 3 elements that are inserted in 2352 01:28:11,199 --> 01:28:15,440 queue 3 2 4 so what will the size of the 2353 01:28:14,159 --> 01:28:17,280 queue 2354 01:28:15,440 --> 01:28:20,080 queue size will be three now coming to 2355 01:28:17,280 --> 01:28:21,600 the logic so how can i import lifo cue 2356 01:28:20,080 --> 01:28:24,639 through the queue module so i will write 2357 01:28:21,600 --> 01:28:24,639 here from q 2358 01:28:25,199 --> 01:28:31,440 and then i will write here import 2359 01:28:28,560 --> 01:28:33,199 and i will write here lifo 2360 01:28:31,440 --> 01:28:35,520 and then i will write here q 2361 01:28:33,199 --> 01:28:37,760 after that as i told you that stack can 2362 01:28:35,520 --> 01:28:40,320 be implemented through the queue module 2363 01:28:37,760 --> 01:28:41,120 so i will create a stack variable here 2364 01:28:40,320 --> 01:28:44,159 and 2365 01:28:41,120 --> 01:28:46,960 i will assign here leave for q 2366 01:28:44,159 --> 01:28:50,000 so i write here leave for q 2367 01:28:46,960 --> 01:28:52,159 and now if i'm writing here stack 2368 01:28:50,000 --> 01:28:54,480 dot put 2369 01:28:52,159 --> 01:28:56,000 and if i'm writing 2 so this means i am 2370 01:28:54,480 --> 01:28:59,280 inserting the value 2 in a stack 2371 01:28:56,000 --> 01:29:00,960 similarly if i am writing here stack 2372 01:28:59,280 --> 01:29:03,120 dot get 2373 01:29:00,960 --> 01:29:05,199 so that means i am removing the value 2374 01:29:03,120 --> 01:29:07,040 from the stack so this is the basic idea 2375 01:29:05,199 --> 01:29:09,040 now we will see all these functions in 2376 01:29:07,040 --> 01:29:10,960 the practical coding example so let's 2377 01:29:09,040 --> 01:29:12,880 start with the coding part i will write 2378 01:29:10,960 --> 01:29:13,920 here 2379 01:29:12,880 --> 01:29:15,600 comment 2380 01:29:13,920 --> 01:29:18,880 and inside this comment i will write 2381 01:29:15,600 --> 01:29:18,880 here implementation 2382 01:29:20,480 --> 01:29:23,600 using 2383 01:29:22,639 --> 01:29:26,000 queue 2384 01:29:23,600 --> 01:29:28,000 now after this what i have to do i have 2385 01:29:26,000 --> 01:29:30,159 to import lifo cube from the queue 2386 01:29:28,000 --> 01:29:32,000 module so what i will write here i will 2387 01:29:30,159 --> 01:29:34,000 write from 2388 01:29:32,000 --> 01:29:36,480 q 2389 01:29:34,000 --> 01:29:36,480 import 2390 01:29:36,800 --> 01:29:39,840 before queue 2391 01:29:42,000 --> 01:29:47,920 and i will create a variable stack 2392 01:29:44,719 --> 01:29:50,159 and i will write now leave for q 2393 01:29:47,920 --> 01:29:51,840 so after creating stack variable as i 2394 01:29:50,159 --> 01:29:54,320 told you that if i want to insert the 2395 01:29:51,840 --> 01:29:56,639 element in a queue then i have to use 2396 01:29:54,320 --> 01:29:58,639 the put function right so i will write 2397 01:29:56,639 --> 01:30:01,600 here stack 2398 01:29:58,639 --> 01:30:03,679 dot put and i will insert here let's 2399 01:30:01,600 --> 01:30:05,600 suppose 2 so you have seen that i've 2400 01:30:03,679 --> 01:30:06,960 already inserted a 2 element now let me 2401 01:30:05,600 --> 01:30:09,520 insert some more elements so i will 2402 01:30:06,960 --> 01:30:10,960 write stack dot put and i will insert 2403 01:30:09,520 --> 01:30:13,280 three here 2404 01:30:10,960 --> 01:30:15,360 and after this i will write stack dot 2405 01:30:13,280 --> 01:30:17,520 put 2406 01:30:15,360 --> 01:30:19,440 four so this is all about the put 2407 01:30:17,520 --> 01:30:21,520 function right so we have seen several 2408 01:30:19,440 --> 01:30:23,760 functions in queue so let me write here 2409 01:30:21,520 --> 01:30:26,000 function so here i will write print and 2410 01:30:23,760 --> 01:30:28,880 if i'm writing here stack 2411 01:30:26,000 --> 01:30:28,880 dot cue size 2412 01:30:30,000 --> 01:30:33,280 so as i told you that queue size will 2413 01:30:31,679 --> 01:30:35,520 give you the number of elements that are 2414 01:30:33,280 --> 01:30:37,360 present in the queue and i have inserted 2415 01:30:35,520 --> 01:30:40,000 three elements so the queue size must 2416 01:30:37,360 --> 01:30:42,000 come as three so on execution you can 2417 01:30:40,000 --> 01:30:42,800 see that i am getting the queue size is 2418 01:30:42,000 --> 01:30:45,440 three 2419 01:30:42,800 --> 01:30:47,760 right now i've also told you about the 2420 01:30:45,440 --> 01:30:51,040 max size function right so 2421 01:30:47,760 --> 01:30:52,800 inside this if i'm writing here max 2422 01:30:51,040 --> 01:30:55,440 size 2423 01:30:52,800 --> 01:30:57,679 max size is equal to 3 2424 01:30:55,440 --> 01:30:57,679 and 2425 01:30:57,760 --> 01:31:03,040 if i'm writing here once again print 2426 01:31:00,960 --> 01:31:05,840 and if i'm writing stack 2427 01:31:03,040 --> 01:31:05,840 dot full 2428 01:31:06,000 --> 01:31:10,639 so as i told you that full function will 2429 01:31:08,080 --> 01:31:12,960 return true if my stack is full so here 2430 01:31:10,639 --> 01:31:15,280 i've allocated the max size as 3 and 2431 01:31:12,960 --> 01:31:18,000 i've inserted 3 elements that means my 2432 01:31:15,280 --> 01:31:20,080 stack is full so on execution 2433 01:31:18,000 --> 01:31:22,560 you can see that i am getting true right 2434 01:31:20,080 --> 01:31:24,639 because my stack is full now if i want 2435 01:31:22,560 --> 01:31:26,639 to remove the element from the stack 2436 01:31:24,639 --> 01:31:28,320 then which function i can use i can use 2437 01:31:26,639 --> 01:31:29,840 here stack 2438 01:31:28,320 --> 01:31:30,800 dot 2439 01:31:29,840 --> 01:31:34,960 get 2440 01:31:30,800 --> 01:31:36,400 and now if i am once again writing print 2441 01:31:34,960 --> 01:31:38,800 stack 2442 01:31:36,400 --> 01:31:41,040 dot full 2443 01:31:38,800 --> 01:31:42,639 will i get true value no because i have 2444 01:31:41,040 --> 01:31:45,920 removed one element so if i am running 2445 01:31:42,639 --> 01:31:48,239 it so on execution you can see that i'm 2446 01:31:45,920 --> 01:31:50,080 getting a false value so here you can 2447 01:31:48,239 --> 01:31:52,400 see that we have used the put function 2448 01:31:50,080 --> 01:31:54,719 get function full function cue size 2449 01:31:52,400 --> 01:31:56,560 function max size so this is the basic 2450 01:31:54,719 --> 01:31:58,480 idea about the stack implementation 2451 01:31:56,560 --> 01:32:01,600 using queue now let's talk about the 2452 01:31:58,480 --> 01:32:03,840 advantages and disadvantages of stack if 2453 01:32:01,600 --> 01:32:06,400 you talk about advantages it maintains 2454 01:32:03,840 --> 01:32:08,080 data in a leaf order right the data is 2455 01:32:06,400 --> 01:32:09,600 maintained in a leaf order that means 2456 01:32:08,080 --> 01:32:13,840 the last element that is there in the 2457 01:32:09,600 --> 01:32:15,840 stack is readily available for us to use 2458 01:32:13,840 --> 01:32:18,400 so one of the 2459 01:32:15,840 --> 01:32:21,600 readily available right so one of the uh 2460 01:32:18,400 --> 01:32:24,320 applications of using stack is it can be 2461 01:32:21,600 --> 01:32:25,600 used to evaluate 2462 01:32:24,320 --> 01:32:26,400 expressions 2463 01:32:25,600 --> 01:32:28,800 right 2464 01:32:26,400 --> 01:32:30,080 then we require this feature that is 2465 01:32:28,800 --> 01:32:32,239 lifo 2466 01:32:30,080 --> 01:32:35,840 right and all the operations that are 2467 01:32:32,239 --> 01:32:37,280 performed in stack like push 2468 01:32:35,840 --> 01:32:38,840 pop 2469 01:32:37,280 --> 01:32:41,199 and then is 2470 01:32:38,840 --> 01:32:43,920 empty and then 2471 01:32:41,199 --> 01:32:45,760 all these operations take constant 2472 01:32:43,920 --> 01:32:48,400 amount of time 2473 01:32:45,760 --> 01:32:50,639 which is very important while you are 2474 01:32:48,400 --> 01:32:52,000 playing around with computer programming 2475 01:32:50,639 --> 01:32:55,360 or while you're doing competitive 2476 01:32:52,000 --> 01:32:57,199 programming at that time these are very 2477 01:32:55,360 --> 01:33:00,719 important that you 2478 01:32:57,199 --> 01:33:03,360 program in the most efficient way 2479 01:33:00,719 --> 01:33:05,760 programming in most efficient way is 2480 01:33:03,360 --> 01:33:08,000 important and it is very crucial for 2481 01:33:05,760 --> 01:33:09,679 your selection right in computative 2482 01:33:08,000 --> 01:33:12,320 programming so 2483 01:33:09,679 --> 01:33:14,560 all the since all the operations are 2484 01:33:12,320 --> 01:33:18,320 performed in a constant amount of time 2485 01:33:14,560 --> 01:33:20,400 so these the stack is used oftenly when 2486 01:33:18,320 --> 01:33:22,639 you are doing competitive programming 2487 01:33:20,400 --> 01:33:24,080 now what are the disadvantages since we 2488 01:33:22,639 --> 01:33:28,000 are restricted right there's a 2489 01:33:24,080 --> 01:33:30,800 restriction that we can only add 2490 01:33:28,000 --> 01:33:33,280 and remove elements from top of the 2491 01:33:30,800 --> 01:33:37,440 stack so this is one restriction that is 2492 01:33:33,280 --> 01:33:39,679 there thus it is not very much flexible 2493 01:33:37,440 --> 01:33:42,159 right it's not flexible 2494 01:33:39,679 --> 01:33:44,320 if you talk about the flexibility of how 2495 01:33:42,159 --> 01:33:46,719 we are storing data in the stack it's 2496 01:33:44,320 --> 01:33:50,320 not very much flexible so these are the 2497 01:33:46,719 --> 01:33:52,639 disadvantages of using stack now let's 2498 01:33:50,320 --> 01:33:53,920 try to understand q linear data 2499 01:33:52,639 --> 01:33:55,199 structure 2500 01:33:53,920 --> 01:33:57,840 what is q 2501 01:33:55,199 --> 01:34:00,719 q is a linear data structure 2502 01:33:57,840 --> 01:34:02,840 that means all the elements in the queue 2503 01:34:00,719 --> 01:34:06,320 are stored in linear 2504 01:34:02,840 --> 01:34:08,719 fashion now it follows a principle of 2505 01:34:06,320 --> 01:34:12,000 fifo that means there's a restriction 2506 01:34:08,719 --> 01:34:15,280 that whatever is the first item in 2507 01:34:12,000 --> 01:34:17,520 is the first item that is to be out okay 2508 01:34:15,280 --> 01:34:20,000 so now let's try to make a queue it's 2509 01:34:17,520 --> 01:34:24,480 suppose you are in a queue 2510 01:34:20,000 --> 01:34:24,480 and you're waiting for a movie 2511 01:34:24,719 --> 01:34:28,639 you're waiting for a movie ticket to buy 2512 01:34:26,400 --> 01:34:30,719 okay so there is one person then there 2513 01:34:28,639 --> 01:34:33,120 is another person right so these are few 2514 01:34:30,719 --> 01:34:36,080 persons here right and you're waiting in 2515 01:34:33,120 --> 01:34:37,920 a queue so now the first person who is 2516 01:34:36,080 --> 01:34:39,440 in the queue will be the first person 2517 01:34:37,920 --> 01:34:40,719 who will get his ticket right makes 2518 01:34:39,440 --> 01:34:42,320 sense right 2519 01:34:40,719 --> 01:34:44,320 so he will be the guy who will get his 2520 01:34:42,320 --> 01:34:46,639 movie ticket first and he will be out of 2521 01:34:44,320 --> 01:34:48,400 the queue then the next person who is in 2522 01:34:46,639 --> 01:34:50,480 the queue is the next person who will 2523 01:34:48,400 --> 01:34:52,639 get his tickets right 2524 01:34:50,480 --> 01:34:54,719 and let's suppose a new person comes in 2525 01:34:52,639 --> 01:34:57,199 he's not going to go ahead from this 2526 01:34:54,719 --> 01:34:59,040 person rather he is going to go behind 2527 01:34:57,199 --> 01:35:01,040 this person then the next person comes 2528 01:34:59,040 --> 01:35:03,679 he will go after this person and in the 2529 01:35:01,040 --> 01:35:05,600 same same way so this is nothing but a 2530 01:35:03,679 --> 01:35:08,159 people 2531 01:35:05,600 --> 01:35:11,199 principle okay the first person in is 2532 01:35:08,159 --> 01:35:13,920 the first person out okay now insertion 2533 01:35:11,199 --> 01:35:16,800 will always take place from the rear end 2534 01:35:13,920 --> 01:35:19,040 okay and if you talk about deletion it 2535 01:35:16,800 --> 01:35:21,520 will always take place from the front 2536 01:35:19,040 --> 01:35:22,800 end okay so this is our front end and 2537 01:35:21,520 --> 01:35:24,320 this is our 2538 01:35:22,800 --> 01:35:27,280 rear 2539 01:35:24,320 --> 01:35:29,440 cool so for examples buying the tickets 2540 01:35:27,280 --> 01:35:31,440 from the counter or it can be a movie 2541 01:35:29,440 --> 01:35:32,880 ticket or it can be a bus station you 2542 01:35:31,440 --> 01:35:35,520 are in front of a bus station trying to 2543 01:35:32,880 --> 01:35:38,320 get tickets for your uh bus right 2544 01:35:35,520 --> 01:35:40,320 these are some examples now there are 2545 01:35:38,320 --> 01:35:42,239 four major operations when you talk 2546 01:35:40,320 --> 01:35:43,440 about queue what are those major 2547 01:35:42,239 --> 01:35:45,920 operations 2548 01:35:43,440 --> 01:35:48,000 let me clear my screen so enqueue so you 2549 01:35:45,920 --> 01:35:50,239 are going to insert an element in the 2550 01:35:48,000 --> 01:35:52,080 queue this is what you mean by enqueue 2551 01:35:50,239 --> 01:35:54,480 thank you you are going to delete an 2552 01:35:52,080 --> 01:35:56,239 element okay from the queue then peak 2553 01:35:54,480 --> 01:35:58,719 first that you're going to peak the 2554 01:35:56,239 --> 01:36:00,719 first element that is in there in the 2555 01:35:58,719 --> 01:36:03,119 queue and p class means that you're 2556 01:36:00,719 --> 01:36:04,880 going to pick the last element that is 2557 01:36:03,119 --> 01:36:07,360 in the queue so you will have two 2558 01:36:04,880 --> 01:36:09,920 pointers one is front and another is 2559 01:36:07,360 --> 01:36:12,320 real and with the help of these pointers 2560 01:36:09,920 --> 01:36:14,800 you're going to enqueue dq peak first 2561 01:36:12,320 --> 01:36:17,840 peak last you're going to perform these 2562 01:36:14,800 --> 01:36:20,719 operations now one major 2563 01:36:17,840 --> 01:36:23,040 advantage of these operations these four 2564 01:36:20,719 --> 01:36:26,000 operations is that all of these 2565 01:36:23,040 --> 01:36:28,400 operations are performed in a constant 2566 01:36:26,000 --> 01:36:30,239 amount of time that means the time 2567 01:36:28,400 --> 01:36:33,440 complexity of performing these 2568 01:36:30,239 --> 01:36:34,800 operations is because one so that is why 2569 01:36:33,440 --> 01:36:37,040 when you talk about computative 2570 01:36:34,800 --> 01:36:39,280 programming q is 2571 01:36:37,040 --> 01:36:41,520 most commonly used data structure 2572 01:36:39,280 --> 01:36:44,000 because of these things right because of 2573 01:36:41,520 --> 01:36:46,400 its time complexity right you are able 2574 01:36:44,000 --> 01:36:48,320 to perform your uh operations in a 2575 01:36:46,400 --> 01:36:51,199 constant amount of time 2576 01:36:48,320 --> 01:36:53,440 now let's talk about applications of q 2577 01:36:51,199 --> 01:36:55,679 so it is used in scheduling algorithms 2578 01:36:53,440 --> 01:36:58,000 of the operating system like first in 2579 01:36:55,679 --> 01:37:00,719 first out scheduling algorithm is there 2580 01:36:58,000 --> 01:37:03,280 round robin is there 2581 01:37:00,719 --> 01:37:05,199 and we have multi-level 2582 01:37:03,280 --> 01:37:07,920 queue 2583 01:37:05,199 --> 01:37:09,760 that is there in all these algorithms q 2584 01:37:07,920 --> 01:37:10,560 is used 2585 01:37:09,760 --> 01:37:14,000 okay 2586 01:37:10,560 --> 01:37:16,239 for storing the data or the processes 2587 01:37:14,000 --> 01:37:18,159 it's also used in maintaining playlists 2588 01:37:16,239 --> 01:37:20,639 like when you have a playlist let's 2589 01:37:18,159 --> 01:37:22,960 suppose you have 10 songs in a queue 2590 01:37:20,639 --> 01:37:24,480 right and after one song the next song 2591 01:37:22,960 --> 01:37:27,760 which is in the queue will be played and 2592 01:37:24,480 --> 01:37:29,760 it goes on uh for like this right so for 2593 01:37:27,760 --> 01:37:31,840 maintaining a playlist again a queue is 2594 01:37:29,760 --> 01:37:32,880 used it's also used in interrupt 2595 01:37:31,840 --> 01:37:35,119 handling 2596 01:37:32,880 --> 01:37:36,719 uh let me take an example here you know 2597 01:37:35,119 --> 01:37:39,679 the process state diagram of operating 2598 01:37:36,719 --> 01:37:42,000 system so it is also used at that time 2599 01:37:39,679 --> 01:37:44,000 so uh when you have an interrupt and 2600 01:37:42,000 --> 01:37:46,800 they're in if your process is it is 2601 01:37:44,000 --> 01:37:49,520 being executed at that time that process 2602 01:37:46,800 --> 01:37:52,000 is printed out and it is stored in a 2603 01:37:49,520 --> 01:37:54,960 queue now the next time when this 2604 01:37:52,000 --> 01:37:57,520 priority or this interrupt is handled 2605 01:37:54,960 --> 01:37:59,600 once it is done then it starts picking 2606 01:37:57,520 --> 01:38:01,600 up the process which was in the queue 2607 01:37:59,600 --> 01:38:03,280 and starts executing that in the 2608 01:38:01,600 --> 01:38:05,520 meanwhile if there are some other 2609 01:38:03,280 --> 01:38:07,920 processes that those processes will also 2610 01:38:05,520 --> 01:38:10,000 be in the queue so a queue is maintained 2611 01:38:07,920 --> 01:38:12,239 and once the interrupt is handled they 2612 01:38:10,000 --> 01:38:15,040 will start taking out that process that 2613 01:38:12,239 --> 01:38:17,679 is that was being executed earlier and 2614 01:38:15,040 --> 01:38:20,080 executes it and completes its execution 2615 01:38:17,679 --> 01:38:22,480 and terminates the process so it is also 2616 01:38:20,080 --> 01:38:25,280 used in interrupt handling 2617 01:38:22,480 --> 01:38:27,760 after learning what is q in python 2618 01:38:25,280 --> 01:38:30,560 theoretically let's know how to 2619 01:38:27,760 --> 01:38:33,440 implement that into practicality so q 2620 01:38:30,560 --> 01:38:37,520 will be having two different basic 2621 01:38:33,440 --> 01:38:40,320 operations that is nq and dq so these 2622 01:38:37,520 --> 01:38:43,280 things will be shown in with a simple 2623 01:38:40,320 --> 01:38:46,400 example in python so let's quickly hop 2624 01:38:43,280 --> 01:38:48,400 on to python id that is google collab 2625 01:38:46,400 --> 01:38:50,960 for the reason i'm using it is visible 2626 01:38:48,400 --> 01:38:53,360 for everybody to access because it's 2627 01:38:50,960 --> 01:38:56,639 online availability and it is open 2628 01:38:53,360 --> 01:38:59,520 source so let's quickly start the simple 2629 01:38:56,639 --> 01:39:01,920 program for queue displaying two 2630 01:38:59,520 --> 01:39:03,040 different functions that is enqueue and 2631 01:39:01,920 --> 01:39:06,560 dq 2632 01:39:03,040 --> 01:39:09,040 so here is the google collab environment 2633 01:39:06,560 --> 01:39:10,080 where you'll be working so i'm just 2634 01:39:09,040 --> 01:39:13,360 pasting 2635 01:39:10,080 --> 01:39:16,800 the code here which is already 2636 01:39:13,360 --> 01:39:19,040 done by me so in order to avoid time and 2637 01:39:16,800 --> 01:39:21,440 uh i'll explain each and every line of 2638 01:39:19,040 --> 01:39:23,360 code here now what we are doing in this 2639 01:39:21,440 --> 01:39:25,840 particular code is 2640 01:39:23,360 --> 01:39:28,320 we are creating a class called q right 2641 01:39:25,840 --> 01:39:31,199 we are also giving different functions 2642 01:39:28,320 --> 01:39:33,280 for nq and dq and q is nothing but 2643 01:39:31,199 --> 01:39:36,560 entering or inserting values to the 2644 01:39:33,280 --> 01:39:39,040 queue and dq is deleting values from the 2645 01:39:36,560 --> 01:39:41,760 queue right as you all know q will 2646 01:39:39,040 --> 01:39:43,679 follow fifo that is first and first out 2647 01:39:41,760 --> 01:39:45,840 so whenever you want to buy a ticket for 2648 01:39:43,679 --> 01:39:47,679 example in your railway stations or 2649 01:39:45,840 --> 01:39:49,679 anywhere you will stand in a queue right 2650 01:39:47,679 --> 01:39:52,239 so whoever in the first will get the 2651 01:39:49,679 --> 01:39:54,960 ticket first and he or she will move out 2652 01:39:52,239 --> 01:39:57,920 of the queue it's same in here as well 2653 01:39:54,960 --> 01:39:59,840 but the elements are not humans it's all 2654 01:39:57,920 --> 01:40:02,000 integer numbers so whatever the number 2655 01:39:59,840 --> 01:40:06,080 you put in first is the first number to 2656 01:40:02,000 --> 01:40:07,600 get out right so let's quickly see 2657 01:40:06,080 --> 01:40:09,600 here we have two different functions as 2658 01:40:07,600 --> 01:40:12,159 i mentioned that is enqueue and dq and 2659 01:40:09,600 --> 01:40:14,000 later you will display we are seeing 2660 01:40:12,159 --> 01:40:16,880 three different functions displaying 2661 01:40:14,000 --> 01:40:20,000 enqueue and dq so what happens here is 2662 01:40:16,880 --> 01:40:22,719 we are using self dot q dot append so 2663 01:40:20,000 --> 01:40:24,800 here whatever the item whatever the uh 2664 01:40:22,719 --> 01:40:27,360 number you give right it will be 2665 01:40:24,800 --> 01:40:30,239 inserted to the back of the queue right 2666 01:40:27,360 --> 01:40:32,880 it is maintaining the sequential process 2667 01:40:30,239 --> 01:40:35,199 of inserting the numbers or the integers 2668 01:40:32,880 --> 01:40:38,080 or the values you give in order to 2669 01:40:35,199 --> 01:40:40,800 insert into the queue and while deleting 2670 01:40:38,080 --> 01:40:43,760 you can use pop right is it append for 2671 01:40:40,800 --> 01:40:46,000 insert and pop for deleting and display 2672 01:40:43,760 --> 01:40:47,760 is nothing but a normal print statement 2673 01:40:46,000 --> 01:40:50,400 you will display whatever the queue it 2674 01:40:47,760 --> 01:40:52,480 is accordingly so let's quickly run this 2675 01:40:50,400 --> 01:40:54,400 program here i've just used certain 2676 01:40:52,480 --> 01:40:57,360 numbers one two three four five five 2677 01:40:54,400 --> 01:40:59,280 numbers and though after dequeuing right 2678 01:40:57,360 --> 01:41:01,520 what it should display it should remove 2679 01:40:59,280 --> 01:41:04,080 one first and two three four five should 2680 01:41:01,520 --> 01:41:04,960 be displayed so let's quickly see how it 2681 01:41:04,080 --> 01:41:06,639 is 2682 01:41:04,960 --> 01:41:09,840 right 2683 01:41:06,639 --> 01:41:12,000 okay so as you can see whatever the 2684 01:41:09,840 --> 01:41:14,480 queue is given is printed at the first 2685 01:41:12,000 --> 01:41:16,480 place that is one two three four five 2686 01:41:14,480 --> 01:41:18,960 and then after removing the first 2687 01:41:16,480 --> 01:41:22,880 element right so the first person will 2688 01:41:18,960 --> 01:41:25,440 be removed because it is fif4 so 2 3 4 5 2689 01:41:22,880 --> 01:41:28,000 is there so this is how a simple basic 2690 01:41:25,440 --> 01:41:29,920 queue will work in python 2691 01:41:28,000 --> 01:41:31,840 so after knowing a basic q 2692 01:41:29,920 --> 01:41:34,320 implementation right let's see one of 2693 01:41:31,840 --> 01:41:36,239 the type of queue that is circular queue 2694 01:41:34,320 --> 01:41:38,080 implementation there are many types of 2695 01:41:36,239 --> 01:41:40,080 queues but still i'm taking 2696 01:41:38,080 --> 01:41:42,719 circular queue as an example and showing 2697 01:41:40,080 --> 01:41:45,119 you the same operations of inserting and 2698 01:41:42,719 --> 01:41:46,719 deleting elements from the queue 2699 01:41:45,119 --> 01:41:49,280 so let's quickly hop into the google 2700 01:41:46,719 --> 01:41:50,639 collab ide and check out the program how 2701 01:41:49,280 --> 01:41:53,280 can we build 2702 01:41:50,639 --> 01:41:55,520 a circular queue in python 2703 01:41:53,280 --> 01:41:57,119 here is the program for circular queue 2704 01:41:55,520 --> 01:42:00,239 and what are the different elements we 2705 01:41:57,119 --> 01:42:02,960 have inside this program let me tell you 2706 01:42:00,239 --> 01:42:05,040 the first part is class declaration so 2707 01:42:02,960 --> 01:42:06,800 here my circular queue is the class 2708 01:42:05,040 --> 01:42:08,639 right so 2709 01:42:06,800 --> 01:42:12,080 class can be named accordingly or 2710 01:42:08,639 --> 01:42:14,000 whatever you feel right so keep it very 2711 01:42:12,080 --> 01:42:16,560 program oriented rather than keeping 2712 01:42:14,000 --> 01:42:20,960 which is off topic so here it is my 2713 01:42:16,560 --> 01:42:24,159 circular queue and then again we have 2714 01:42:20,960 --> 01:42:28,960 two different initialization that is for 2715 01:42:24,159 --> 01:42:31,600 nq as well as dq so whatever the 2716 01:42:28,960 --> 01:42:33,920 elements we use here right whatever the 2717 01:42:31,600 --> 01:42:36,560 items we try to insert in the queue we 2718 01:42:33,920 --> 01:42:38,320 have to ensure whether the queue is full 2719 01:42:36,560 --> 01:42:41,280 or the queue is 2720 01:42:38,320 --> 01:42:43,199 empty and there is still space or not so 2721 01:42:41,280 --> 01:42:45,119 all the conditions should be checked so 2722 01:42:43,199 --> 01:42:46,480 let's hop into enqueue and check out 2723 01:42:45,119 --> 01:42:48,800 what are the different conditions you 2724 01:42:46,480 --> 01:42:52,239 have to check so the first thing is the 2725 01:42:48,800 --> 01:42:55,199 queue is full or not so before inserting 2726 01:42:52,239 --> 01:42:58,159 something say for example the queue size 2727 01:42:55,199 --> 01:43:00,480 is 5 and the element 6 has been inserted 2728 01:42:58,159 --> 01:43:01,600 then it has to show an error message 2729 01:43:00,480 --> 01:43:03,600 that is 2730 01:43:01,600 --> 01:43:05,520 there are only five spaces they are 2731 01:43:03,600 --> 01:43:08,480 inserting sixth element it is not 2732 01:43:05,520 --> 01:43:11,119 allowed hence the queue is filled so in 2733 01:43:08,480 --> 01:43:14,000 order to print that we use this the 2734 01:43:11,119 --> 01:43:17,119 circular queues filled 2735 01:43:14,000 --> 01:43:18,880 statement so the next part is 2736 01:43:17,119 --> 01:43:21,840 you have to 2737 01:43:18,880 --> 01:43:24,639 know how when it is empty right in order 2738 01:43:21,840 --> 01:43:26,639 to have the dq the main condition is 2739 01:43:24,639 --> 01:43:29,199 whenever the uh 2740 01:43:26,639 --> 01:43:32,239 elements are out of the queue then it 2741 01:43:29,199 --> 01:43:34,239 has to be declared as the queue is empty 2742 01:43:32,239 --> 01:43:36,239 so nothing to delete from the queue it's 2743 01:43:34,239 --> 01:43:38,880 every all the elements or items are 2744 01:43:36,239 --> 01:43:40,800 deleted already so the error message or 2745 01:43:38,880 --> 01:43:42,800 the statement to the user will be the 2746 01:43:40,800 --> 01:43:43,920 circular queue is empty now there is 2747 01:43:42,800 --> 01:43:47,199 nothing to 2748 01:43:43,920 --> 01:43:50,480 pop out or delete or dequeue so 2749 01:43:47,199 --> 01:43:51,920 apart from that also you can also 2750 01:43:50,480 --> 01:43:54,639 find if you are trying to print 2751 01:43:51,920 --> 01:43:56,639 something right if it is mtq it does not 2752 01:43:54,639 --> 01:43:57,600 have anything then you have to show up 2753 01:43:56,639 --> 01:44:00,000 though 2754 01:43:57,600 --> 01:44:02,480 no element in the circular queue found 2755 01:44:00,000 --> 01:44:04,639 statement why because if there is no 2756 01:44:02,480 --> 01:44:06,800 elements there is nothing to show or 2757 01:44:04,639 --> 01:44:09,440 display the display function does not 2758 01:44:06,800 --> 01:44:12,560 work the print does not happen so this 2759 01:44:09,440 --> 01:44:14,800 is a basic idea of this particular code 2760 01:44:12,560 --> 01:44:17,920 and accordingly we have used the 2761 01:44:14,800 --> 01:44:20,320 iterations and the declarations so next 2762 01:44:17,920 --> 01:44:22,400 you have to look at the 2763 01:44:20,320 --> 01:44:26,800 inputs what we are giving i'm trying to 2764 01:44:22,400 --> 01:44:28,880 give 12 22 31 44 and 57 right so the 2765 01:44:26,800 --> 01:44:31,600 five elements for the queue has been 2766 01:44:28,880 --> 01:44:33,840 given and what you have to do is you 2767 01:44:31,600 --> 01:44:35,760 have to check the initial values first 2768 01:44:33,840 --> 01:44:37,840 you have to display the initial values 2769 01:44:35,760 --> 01:44:40,480 what is the exact queue which you have 2770 01:44:37,840 --> 01:44:42,880 given with the elements to the user and 2771 01:44:40,480 --> 01:44:46,080 then which is deleted so the first 2772 01:44:42,880 --> 01:44:47,920 element is deleted obviously but yes how 2773 01:44:46,080 --> 01:44:50,880 the circular queue is different from the 2774 01:44:47,920 --> 01:44:54,719 basic queue right so let's quickly run 2775 01:44:50,880 --> 01:44:54,719 the program in order to see the output 2776 01:44:55,679 --> 01:45:00,560 okay if you could see the output here 2777 01:44:57,760 --> 01:45:03,119 right so initial q values 2778 01:45:00,560 --> 01:45:07,840 so is whatever we have given here that 2779 01:45:03,119 --> 01:45:09,840 is 12 22 31 44 and 57 so after removing 2780 01:45:07,840 --> 01:45:11,760 an element so obviously the first in 2781 01:45:09,840 --> 01:45:16,960 first hour process the first element 2782 01:45:11,760 --> 01:45:19,440 will be removed so it is 22 31 44 and 57 2783 01:45:16,960 --> 01:45:21,600 what is difference between a normal q 2784 01:45:19,440 --> 01:45:24,239 and a circular queue if you could see 2785 01:45:21,600 --> 01:45:27,040 here right in the last space after 57 2786 01:45:24,239 --> 01:45:29,600 you have a space allocated so 2787 01:45:27,040 --> 01:45:31,520 in normal queue it is not connected here 2788 01:45:29,600 --> 01:45:34,159 the front and rear is being connected 2789 01:45:31,520 --> 01:45:36,719 forming a circle right if one 2790 01:45:34,159 --> 01:45:37,520 the first element for example 12 goes 2791 01:45:36,719 --> 01:45:40,239 out 2792 01:45:37,520 --> 01:45:42,880 the 22 will take the first place and 31 2793 01:45:40,239 --> 01:45:44,400 followed by 44 followed by 57 the last 2794 01:45:42,880 --> 01:45:46,639 place will be empty 2795 01:45:44,400 --> 01:45:49,199 right so it is in circular motion so 2796 01:45:46,639 --> 01:45:51,040 whatever you want to insert again right 2797 01:45:49,199 --> 01:45:54,239 so that will for example if you want to 2798 01:45:51,040 --> 01:45:57,280 insert six right six will sit in the 2799 01:45:54,239 --> 01:45:59,440 fifth position that is after 57 right 2800 01:45:57,280 --> 01:46:01,440 this is this will be connected circular 2801 01:45:59,440 --> 01:46:03,199 motion that is front will be connected 2802 01:46:01,440 --> 01:46:05,600 to the rear part so this is the 2803 01:46:03,199 --> 01:46:07,760 difference between the normal basic q 2804 01:46:05,600 --> 01:46:10,320 and the circular queue now let's talk 2805 01:46:07,760 --> 01:46:12,719 about advantages and disadvantages of q 2806 01:46:10,320 --> 01:46:15,440 first we are talking about advantages so 2807 01:46:12,719 --> 01:46:17,600 it follows a principle of fifo or the 2808 01:46:15,440 --> 01:46:20,639 elements are stored in the fifo manner 2809 01:46:17,600 --> 01:46:23,440 that means let's suppose this is a queue 2810 01:46:20,639 --> 01:46:25,600 and in this queue you have elements 2811 01:46:23,440 --> 01:46:28,560 so the deletion will take place from the 2812 01:46:25,600 --> 01:46:30,639 friend right and 2813 01:46:28,560 --> 01:46:32,080 the insertion will take place from the 2814 01:46:30,639 --> 01:46:35,199 rear side 2815 01:46:32,080 --> 01:46:37,520 so this is known as dq 2816 01:46:35,199 --> 01:46:40,239 the deletion 2817 01:46:37,520 --> 01:46:42,639 and insertion is known as enqueue 2818 01:46:40,239 --> 01:46:44,800 operation and both these operations are 2819 01:46:42,639 --> 01:46:46,800 performed in a constant amount of time 2820 01:46:44,800 --> 01:46:48,400 so that is one of the advantages right 2821 01:46:46,800 --> 01:46:50,560 and the insertion from beginning and 2822 01:46:48,400 --> 01:46:52,880 deletion from end takes a constant 2823 01:46:50,560 --> 01:46:54,960 amount of time plus if we want to do 2824 01:46:52,880 --> 01:46:57,520 peak first peak plus all these 2825 01:46:54,960 --> 01:47:00,320 operations are performed in a constant 2826 01:46:57,520 --> 01:47:02,639 amount of time and this is most widely 2827 01:47:00,320 --> 01:47:04,800 used data structure when we talk about 2828 01:47:02,639 --> 01:47:06,480 cp that is computative programming when 2829 01:47:04,800 --> 01:47:08,480 we talk about computative programming 2830 01:47:06,480 --> 01:47:10,480 this data structure is most commonly 2831 01:47:08,480 --> 01:47:12,159 used because of these features that all 2832 01:47:10,480 --> 01:47:14,880 the operations that are performed like 2833 01:47:12,159 --> 01:47:17,199 insertion deletion peak first p class 2834 01:47:14,880 --> 01:47:20,159 and qdq all these operations are 2835 01:47:17,199 --> 01:47:23,840 performed in a constant amount of time 2836 01:47:20,159 --> 01:47:27,119 now let's talk about disadvantages 2837 01:47:23,840 --> 01:47:29,520 since we are only able to delete or 2838 01:47:27,119 --> 01:47:32,639 insert from the front and the rear that 2839 01:47:29,520 --> 01:47:35,600 means deletion from fret and insertion 2840 01:47:32,639 --> 01:47:36,880 from the rear so the restriction of 2841 01:47:35,600 --> 01:47:39,440 insertion 2842 01:47:36,880 --> 01:47:42,239 or any manipulation right we have a 2843 01:47:39,440 --> 01:47:44,960 restriction over these right what these 2844 01:47:42,239 --> 01:47:47,679 operations insertion and deletion 2845 01:47:44,960 --> 01:47:50,320 so this restriction is always there and 2846 01:47:47,679 --> 01:47:52,480 so this data structure that is the queue 2847 01:47:50,320 --> 01:47:55,360 is not 2848 01:47:52,480 --> 01:47:58,000 is not much flexible 2849 01:47:55,360 --> 01:48:01,280 right we are fixed we can delete and 2850 01:47:58,000 --> 01:48:03,920 insert element in a fixed pattern or 2851 01:48:01,280 --> 01:48:05,840 in the fifo manner thus it's not much 2852 01:48:03,920 --> 01:48:07,760 flexible so now let's start with the 2853 01:48:05,840 --> 01:48:10,080 link list 2854 01:48:07,760 --> 01:48:13,280 so link list is a collection of group of 2855 01:48:10,080 --> 01:48:16,960 nodes now what is node here so here you 2856 01:48:13,280 --> 01:48:19,679 can see that this is a node so a node 2857 01:48:16,960 --> 01:48:22,960 will contain a data 2858 01:48:19,679 --> 01:48:22,960 as well as reference 2859 01:48:24,159 --> 01:48:28,480 so each node contains data and reference 2860 01:48:26,800 --> 01:48:30,639 which contains the address of the next 2861 01:48:28,480 --> 01:48:32,719 node so this is a node and as i told you 2862 01:48:30,639 --> 01:48:34,239 that node will contain a data as well as 2863 01:48:32,719 --> 01:48:36,960 reference so let's suppose i am 2864 01:48:34,239 --> 01:48:38,400 inserting the data here 20 and this is 2865 01:48:36,960 --> 01:48:40,400 nothing but a reference or you can say 2866 01:48:38,400 --> 01:48:42,800 that pointer so 2867 01:48:40,400 --> 01:48:45,360 this pointer will contain the address of 2868 01:48:42,800 --> 01:48:46,880 the next node right so as i told you 2869 01:48:45,360 --> 01:48:48,639 that linked list is a collection of 2870 01:48:46,880 --> 01:48:50,800 nodes so this is nothing but a single 2871 01:48:48,639 --> 01:48:53,679 node so let's suppose if i am having 2872 01:48:50,800 --> 01:48:53,679 more than one node 2873 01:48:55,600 --> 01:48:59,560 and if i am connecting them 2874 01:49:02,480 --> 01:49:06,239 then it will form a linked list so we 2875 01:49:04,560 --> 01:49:08,239 will see the linked list representation 2876 01:49:06,239 --> 01:49:10,320 in the next slide now 2877 01:49:08,239 --> 01:49:12,080 so link list is a linear data structure 2878 01:49:10,320 --> 01:49:13,920 now coming to the last point we know 2879 01:49:12,080 --> 01:49:15,840 that in array as well as in list 2880 01:49:13,920 --> 01:49:17,920 elements are stored at the contiguous 2881 01:49:15,840 --> 01:49:19,920 memory whereas in linked list elements 2882 01:49:17,920 --> 01:49:22,719 are stored randomly now let's see the 2883 01:49:19,920 --> 01:49:24,480 representation of linked list 2884 01:49:22,719 --> 01:49:26,480 so as i told you that ling list is a 2885 01:49:24,480 --> 01:49:29,679 collection of nodes so let's suppose 2886 01:49:26,480 --> 01:49:32,719 that this is my n1 node this is my n2 2887 01:49:29,679 --> 01:49:34,639 node and this is my n3 node so each node 2888 01:49:32,719 --> 01:49:37,360 will contain data as well as reference 2889 01:49:34,639 --> 01:49:39,840 or you can say pointer so this is data 2890 01:49:37,360 --> 01:49:41,760 and this is reference so now i will give 2891 01:49:39,840 --> 01:49:43,760 the address of this n1 node so let me 2892 01:49:41,760 --> 01:49:45,040 write the address of this n1 nose at 2893 01:49:43,760 --> 01:49:48,159 2010 2894 01:49:45,040 --> 01:49:50,560 i will give the address of this as 2020 2895 01:49:48,159 --> 01:49:53,920 and i will give the address of n3 node 2896 01:49:50,560 --> 01:49:56,239 as 2030 so these are the addresses 2897 01:49:53,920 --> 01:49:57,679 so as i told you that each node will 2898 01:49:56,239 --> 01:49:59,520 contain a data 2899 01:49:57,679 --> 01:50:02,320 so let me assign here data let's suppose 2900 01:49:59,520 --> 01:50:04,880 10 is here and a reference or you can 2901 01:50:02,320 --> 01:50:06,880 say a pointer so i told you about 2902 01:50:04,880 --> 01:50:08,960 pointer that pointer will contain the 2903 01:50:06,880 --> 01:50:11,119 address of the next node so what's the 2904 01:50:08,960 --> 01:50:15,280 address of next node the address of next 2905 01:50:11,119 --> 01:50:17,360 node is 2020 so i will write here 2020 2906 01:50:15,280 --> 01:50:19,199 so this pointer or this reference or you 2907 01:50:17,360 --> 01:50:21,520 can say this link will contain the 2908 01:50:19,199 --> 01:50:24,159 address of my next node and what's the 2909 01:50:21,520 --> 01:50:27,440 address of my next node is 2020 now 2910 01:50:24,159 --> 01:50:29,760 again this will be a data 2911 01:50:27,440 --> 01:50:30,960 and this will be a reference 2912 01:50:29,760 --> 01:50:34,080 of my 2913 01:50:30,960 --> 01:50:35,840 n2 node so let me assign here data as 20 2914 01:50:34,080 --> 01:50:37,280 and what will be the reference the 2915 01:50:35,840 --> 01:50:40,080 reference will contain the address of 2916 01:50:37,280 --> 01:50:42,719 the next node so here it will be 20 30. 2917 01:50:40,080 --> 01:50:44,960 now again this n3 will also contain data 2918 01:50:42,719 --> 01:50:46,880 so i will assign here 30 so now you 2919 01:50:44,960 --> 01:50:49,760 might have a question that what should 2920 01:50:46,880 --> 01:50:49,760 be reference here 2921 01:50:50,000 --> 01:50:53,199 so now 2922 01:50:51,119 --> 01:50:56,639 are you seeing any node after this n3 2923 01:50:53,199 --> 01:50:57,360 node do we have node n4 or n5 not right 2924 01:50:56,639 --> 01:51:00,159 so 2925 01:50:57,360 --> 01:51:02,159 this reference will be assigned to null 2926 01:51:00,159 --> 01:51:04,400 so i can write here phi 2927 01:51:02,159 --> 01:51:07,199 because there is no next node is present 2928 01:51:04,400 --> 01:51:09,199 there right now coming to head what is 2929 01:51:07,199 --> 01:51:12,960 head so head will contain the address of 2930 01:51:09,199 --> 01:51:13,920 my n1 node that means 2010 2931 01:51:12,960 --> 01:51:16,719 right 2932 01:51:13,920 --> 01:51:18,639 so this is my linked list representation 2933 01:51:16,719 --> 01:51:21,280 now the question arises that why link 2934 01:51:18,639 --> 01:51:22,719 list so now why do we need link list 2935 01:51:21,280 --> 01:51:24,880 because ling list is having more 2936 01:51:22,719 --> 01:51:26,639 efficiency for performing the operations 2937 01:51:24,880 --> 01:51:28,639 as compared to list so what are the 2938 01:51:26,639 --> 01:51:30,159 operations that we are performing in 2939 01:51:28,639 --> 01:51:32,800 linked list we can perform the 2940 01:51:30,159 --> 01:51:35,119 operations like insertion deletion as 2941 01:51:32,800 --> 01:51:37,440 well as traversal so it is having more 2942 01:51:35,119 --> 01:51:39,440 efficiency in performing the operation 2943 01:51:37,440 --> 01:51:41,599 so moving to the next point as i already 2944 01:51:39,440 --> 01:51:44,239 told you that in linked list elements 2945 01:51:41,599 --> 01:51:46,159 are stored randomly whereas in list or 2946 01:51:44,239 --> 01:51:50,159 you can say in array elements are stored 2947 01:51:46,159 --> 01:51:52,400 at contiguous memory now moving next 2948 01:51:50,159 --> 01:51:54,639 accessing the elements in linked list 2949 01:51:52,400 --> 01:51:56,400 will be slower as compared to list so if 2950 01:51:54,639 --> 01:51:58,400 you want to access the element in linked 2951 01:51:56,400 --> 01:52:00,960 list it will be slower as compared to 2952 01:51:58,400 --> 01:52:02,639 list why i will tell you the reason 2953 01:52:00,960 --> 01:52:05,040 now let's see this slide link list 2954 01:52:02,639 --> 01:52:06,960 representation so here this is my n3 2955 01:52:05,040 --> 01:52:09,119 node right and if i 2956 01:52:06,960 --> 01:52:14,159 want to access the data elements of this 2957 01:52:09,119 --> 01:52:16,000 n3 node then i have to go from n1 n2 2958 01:52:14,159 --> 01:52:18,880 and n3 then only i can access the 2959 01:52:16,000 --> 01:52:21,520 elements whereas in case of list we can 2960 01:52:18,880 --> 01:52:23,599 access the element through indexing but 2961 01:52:21,520 --> 01:52:25,679 in linked list it's not possible so you 2962 01:52:23,599 --> 01:52:26,639 have to go traversely 2963 01:52:25,679 --> 01:52:28,639 right 2964 01:52:26,639 --> 01:52:30,639 here traversal means that you have to go 2965 01:52:28,639 --> 01:52:32,639 through each node so if you want to 2966 01:52:30,639 --> 01:52:35,360 access the elements of n3 then you have 2967 01:52:32,639 --> 01:52:37,920 to start with n1 then you will go to n2 2968 01:52:35,360 --> 01:52:39,679 then you can go to n3 so that's why 2969 01:52:37,920 --> 01:52:42,159 accessing the elements in linked list 2970 01:52:39,679 --> 01:52:44,480 will be slower as compared to list now 2971 01:52:42,159 --> 01:52:46,639 coming to the last point here in linked 2972 01:52:44,480 --> 01:52:48,800 list utilization of memory will be more 2973 01:52:46,639 --> 01:52:50,320 as compared to list 2974 01:52:48,800 --> 01:52:51,199 so let's start with the singly linked 2975 01:52:50,320 --> 01:52:52,480 list 2976 01:52:51,199 --> 01:52:54,560 so i've already showed you the 2977 01:52:52,480 --> 01:52:56,960 representation of linked list which is 2978 01:52:54,560 --> 01:52:58,960 same as the singly linked list so in 2979 01:52:56,960 --> 01:53:02,320 single linked list i am having here a 2980 01:52:58,960 --> 01:53:04,560 data and reference in a node so 2981 01:53:02,320 --> 01:53:07,440 let's suppose that this is my n1 node 2982 01:53:04,560 --> 01:53:09,599 this is my n2 node and this is my n3 2983 01:53:07,440 --> 01:53:12,000 node so as i told you that each node 2984 01:53:09,599 --> 01:53:14,080 will contain data as well as reference 2985 01:53:12,000 --> 01:53:15,119 so i will give here data let's suppose 2986 01:53:14,080 --> 01:53:18,159 10 2987 01:53:15,119 --> 01:53:20,000 in node 2 i will give as 20 and here i 2988 01:53:18,159 --> 01:53:22,159 will give as 30 2989 01:53:20,000 --> 01:53:23,920 and each node is having an address so 2990 01:53:22,159 --> 01:53:26,560 let's suppose the address of this n1 2991 01:53:23,920 --> 01:53:28,639 node is 1000 2992 01:53:26,560 --> 01:53:31,040 it's having 1100 2993 01:53:28,639 --> 01:53:33,040 and it's having 1200 2994 01:53:31,040 --> 01:53:35,440 so this reference or you can say that 2995 01:53:33,040 --> 01:53:37,360 link or pointer this will contain the 2996 01:53:35,440 --> 01:53:41,040 address of the next node so this will 2997 01:53:37,360 --> 01:53:42,880 contain 1100 similarly my this reference 2998 01:53:41,040 --> 01:53:45,199 or this link will contain the address of 2999 01:53:42,880 --> 01:53:46,800 n3 node so i'll write here 3000 01:53:45,199 --> 01:53:49,440 1200 3001 01:53:46,800 --> 01:53:52,560 and here after n3 node do you see any 3002 01:53:49,440 --> 01:53:56,000 node we are not having any node so here 3003 01:53:52,560 --> 01:53:58,880 this link or this reference will null 3004 01:53:56,000 --> 01:54:00,800 now coming to here what is head here so 3005 01:53:58,880 --> 01:54:03,520 head will contain the address of my 3006 01:54:00,800 --> 01:54:05,520 first node that is 1000 so in single 3007 01:54:03,520 --> 01:54:07,520 linked list the traversal is done only 3008 01:54:05,520 --> 01:54:09,360 in one direction so what do you mean by 3009 01:54:07,520 --> 01:54:11,360 traversal troublesome means that you are 3010 01:54:09,360 --> 01:54:13,599 going through each node so let's suppose 3011 01:54:11,360 --> 01:54:16,159 that if you want to go to the n3 node 3012 01:54:13,599 --> 01:54:18,800 first go to n1 then n2 then only you 3013 01:54:16,159 --> 01:54:20,639 come to n3 you can't directly jump to n3 3014 01:54:18,800 --> 01:54:23,360 you have to go through n1 and n2 then 3015 01:54:20,639 --> 01:54:25,840 only you come to n3 now let's see some 3016 01:54:23,360 --> 01:54:27,440 operations in singly linked list so we 3017 01:54:25,840 --> 01:54:29,760 are having several operations in linked 3018 01:54:27,440 --> 01:54:31,840 list we are having insertion deletion 3019 01:54:29,760 --> 01:54:34,719 traversal so insertion as well as 3020 01:54:31,840 --> 01:54:38,239 deletion can be done at beginning at any 3021 01:54:34,719 --> 01:54:40,159 specified node as well as end 3022 01:54:38,239 --> 01:54:42,480 now coming to the traversal i have 3023 01:54:40,159 --> 01:54:44,719 already told you that traversal means 3024 01:54:42,480 --> 01:54:46,560 you have to go through each node 3025 01:54:44,719 --> 01:54:48,800 so going through each node of the linked 3026 01:54:46,560 --> 01:54:51,280 list is a traversal now let's see the 3027 01:54:48,800 --> 01:54:53,280 pseudo code of singling link list so if 3028 01:54:51,280 --> 01:54:55,040 you want to create a node in a singly 3029 01:54:53,280 --> 01:54:58,960 linked list then what should be the code 3030 01:54:55,040 --> 01:55:00,480 here so i will write first here class 3031 01:54:58,960 --> 01:55:03,440 node 3032 01:55:00,480 --> 01:55:06,000 so here i have created a class whose 3033 01:55:03,440 --> 01:55:07,760 name is node so this class node will 3034 01:55:06,000 --> 01:55:10,159 also be having an object right i will 3035 01:55:07,760 --> 01:55:12,639 create object later on but 3036 01:55:10,159 --> 01:55:12,639 let's see 3037 01:55:12,800 --> 01:55:18,080 let's suppose this is my n1 node as i 3038 01:55:15,520 --> 01:55:20,560 told you that a node will contain data 3039 01:55:18,080 --> 01:55:20,560 as well as 3040 01:55:20,800 --> 01:55:23,840 reference 3041 01:55:22,000 --> 01:55:26,000 right so instead of reference i am 3042 01:55:23,840 --> 01:55:27,679 writing here next i am taking a small 3043 01:55:26,000 --> 01:55:31,280 word here so that it will be easy for 3044 01:55:27,679 --> 01:55:33,920 coding now so this is my node creation 3045 01:55:31,280 --> 01:55:35,920 now what i will do here so in this class 3046 01:55:33,920 --> 01:55:38,239 node you have seen that i am creating a 3047 01:55:35,920 --> 01:55:40,400 init method or you can say a constructor 3048 01:55:38,239 --> 01:55:42,639 so to create it first i will write a 3049 01:55:40,400 --> 01:55:45,280 reserved word that is def and then i 3050 01:55:42,639 --> 01:55:47,440 will write init method so 3051 01:55:45,280 --> 01:55:50,480 i will write first def 3052 01:55:47,440 --> 01:55:51,920 and then underscore underscore init 3053 01:55:50,480 --> 01:55:55,679 and then i will write underscore 3054 01:55:51,920 --> 01:55:58,800 underscore then i will write self 3055 01:55:55,679 --> 01:56:00,960 and then comma comma data 3056 01:55:58,800 --> 01:56:03,840 so why i have written here self i will 3057 01:56:00,960 --> 01:56:06,560 tell you later on and i've also passed 3058 01:56:03,840 --> 01:56:08,480 data as a parameter here so inside this 3059 01:56:06,560 --> 01:56:10,880 i have written self dot data is equal to 3060 01:56:08,480 --> 01:56:12,880 data and self dot reference is equal to 3061 01:56:10,880 --> 01:56:14,560 none so why i have written this because 3062 01:56:12,880 --> 01:56:16,639 my node will contain data as well as 3063 01:56:14,560 --> 01:56:19,119 reference so i will write here in this 3064 01:56:16,639 --> 01:56:20,480 method self 3065 01:56:19,119 --> 01:56:23,119 dot data 3066 01:56:20,480 --> 01:56:25,440 is equal to data 3067 01:56:23,119 --> 01:56:27,440 and self dot reference i've written here 3068 01:56:25,440 --> 01:56:29,679 next so i will take here next 3069 01:56:27,440 --> 01:56:31,520 is equal to none 3070 01:56:29,679 --> 01:56:34,320 so when you are creating a node let's 3071 01:56:31,520 --> 01:56:37,199 see this is a node n1 3072 01:56:34,320 --> 01:56:39,199 so initially it will be having a data 3073 01:56:37,199 --> 01:56:42,239 and because i am just creating a node as 3074 01:56:39,199 --> 01:56:44,880 of now i am not linking this node so the 3075 01:56:42,239 --> 01:56:46,800 link or you can say the reference 3076 01:56:44,880 --> 01:56:49,040 will be none 3077 01:56:46,800 --> 01:56:51,199 right so this is my initial load i have 3078 01:56:49,040 --> 01:56:53,840 written here self.data is equal to data 3079 01:56:51,199 --> 01:56:54,800 and self.next is equal to none 3080 01:56:53,840 --> 01:56:55,760 right 3081 01:56:54,800 --> 01:56:58,320 now 3082 01:56:55,760 --> 01:56:59,920 this is a class was name is node i can 3083 01:56:58,320 --> 01:57:02,080 create the object so how to create an 3084 01:56:59,920 --> 01:57:04,000 object i will write here n1 and then i 3085 01:57:02,080 --> 01:57:05,199 will write here class name 3086 01:57:04,000 --> 01:57:06,880 node 3087 01:57:05,199 --> 01:57:09,679 and inside this class i will pass the 3088 01:57:06,880 --> 01:57:12,239 parameter 7 so here now what will happen 3089 01:57:09,679 --> 01:57:15,280 instead of self my object will pass here 3090 01:57:12,239 --> 01:57:19,520 so my n1 will pass here instead of self 3091 01:57:15,280 --> 01:57:22,000 so now it will be n1 dot data 3092 01:57:19,520 --> 01:57:23,599 is equal to and what's my data data is 3093 01:57:22,000 --> 01:57:25,679 seven 3094 01:57:23,599 --> 01:57:27,599 now the next step we are having the self 3095 01:57:25,679 --> 01:57:29,840 dot reference is equal to none right 3096 01:57:27,599 --> 01:57:32,480 let's see here self dot next is equal to 3097 01:57:29,840 --> 01:57:35,360 none so instead of self my n1 is there 3098 01:57:32,480 --> 01:57:38,560 so n1 will be pass here and n1 dot next 3099 01:57:35,360 --> 01:57:40,480 i will be having a none 3100 01:57:38,560 --> 01:57:42,239 so this is nothing but a creation of my 3101 01:57:40,480 --> 01:57:43,760 node now if you want to check then we 3102 01:57:42,239 --> 01:57:46,159 can write print function and when you 3103 01:57:43,760 --> 01:57:47,440 will write here node 1 data inside a 3104 01:57:46,159 --> 01:57:49,360 print function you will see that you are 3105 01:57:47,440 --> 01:57:51,199 getting the value s7 similarly if you 3106 01:57:49,360 --> 01:57:53,520 are writing node 1 dot reference inside 3107 01:57:51,199 --> 01:57:56,080 a print method then you will be getting 3108 01:57:53,520 --> 01:57:58,320 none so now this is the idea about how 3109 01:57:56,080 --> 01:57:59,280 to create a node now let's see this into 3110 01:57:58,320 --> 01:58:00,800 a coding 3111 01:57:59,280 --> 01:58:03,520 so i will be using here jupyter 3112 01:58:00,800 --> 01:58:06,880 notebooks so i will go on here new 3113 01:58:03,520 --> 01:58:08,480 and then i will click on python 3 3114 01:58:06,880 --> 01:58:11,760 and here you can see that i am getting a 3115 01:58:08,480 --> 01:58:14,719 name untitled 21 let me change it i will 3116 01:58:11,760 --> 01:58:14,719 write here linked list 3117 01:58:16,000 --> 01:58:19,719 linked list 3118 01:58:21,280 --> 01:58:24,560 and i will write here python 3119 01:58:25,920 --> 01:58:29,599 now 3120 01:58:27,360 --> 01:58:33,880 let's create a node so i will comment it 3121 01:58:29,599 --> 01:58:33,880 down creating a node 3122 01:58:35,360 --> 01:58:41,040 so first i will create a class 3123 01:58:37,679 --> 01:58:42,960 and i will give the name as node 3124 01:58:41,040 --> 01:58:45,760 and inside this 3125 01:58:42,960 --> 01:58:47,520 class node i will create my edit method 3126 01:58:45,760 --> 01:58:49,760 so i will write here def which is a 3127 01:58:47,520 --> 01:58:50,880 reserved word and then i will write here 3128 01:58:49,760 --> 01:58:52,639 init 3129 01:58:50,880 --> 01:58:55,199 but before that i will write underscore 3130 01:58:52,639 --> 01:58:55,199 underscore 3131 01:58:55,360 --> 01:58:58,880 then again i will write underscore 3132 01:58:56,719 --> 01:58:59,920 underscore and then i will write here 3133 01:58:58,880 --> 01:59:01,119 self 3134 01:58:59,920 --> 01:59:03,599 comma 3135 01:59:01,119 --> 01:59:05,920 data so so why i have written here self 3136 01:59:03,599 --> 01:59:07,840 so when i'm creating a class object that 3137 01:59:05,920 --> 01:59:10,239 is n1 i've already showed you in the 3138 01:59:07,840 --> 01:59:11,280 example so instead of self and one is 3139 01:59:10,239 --> 01:59:12,719 fast 3140 01:59:11,280 --> 01:59:14,639 so as we know that we can create a 3141 01:59:12,719 --> 01:59:17,679 multiple object of a class so if i'm 3142 01:59:14,639 --> 01:59:20,960 writing here n2 or n3 then instead of 3143 01:59:17,679 --> 01:59:23,119 self i can pass n2 and n3 also 3144 01:59:20,960 --> 01:59:25,360 so now let's create a node 3145 01:59:23,119 --> 01:59:29,360 so i will write here self 3146 01:59:25,360 --> 01:59:29,360 dot data is equal to data 3147 01:59:29,520 --> 01:59:32,639 and i will write here self 3148 01:59:31,840 --> 01:59:34,239 dot 3149 01:59:32,639 --> 01:59:36,960 next 3150 01:59:34,239 --> 01:59:38,880 i will write here none so this is my 3151 01:59:36,960 --> 01:59:41,040 node creation so whenever i am having a 3152 01:59:38,880 --> 01:59:43,599 node it will contain a data and it will 3153 01:59:41,040 --> 01:59:46,320 be having a reference so initially it is 3154 01:59:43,599 --> 01:59:48,080 not linked so the reference is none 3155 01:59:46,320 --> 01:59:51,280 so this is my class now i will create an 3156 01:59:48,080 --> 01:59:54,719 object of node so i'll write here n1 3157 01:59:51,280 --> 01:59:54,719 and i will write class name 3158 01:59:55,040 --> 02:00:00,400 and i will pass data as let's suppose 8 3159 01:59:57,920 --> 02:00:03,360 value here 3160 02:00:00,400 --> 02:00:06,080 so if i'm executing it so on executing 3161 02:00:03,360 --> 02:00:08,800 this n1 will go to the self and this 8 3162 02:00:06,080 --> 02:00:11,599 will go into the data so my n1 dot data 3163 02:00:08,800 --> 02:00:14,000 is equal to 8 and my n1 dot next is 3164 02:00:11,599 --> 02:00:16,960 equal to none let me print it so if i'm 3165 02:00:14,000 --> 02:00:16,960 writing here print 3166 02:00:18,960 --> 02:00:24,159 and inside that if i'm writing my n1 dot 3167 02:00:21,760 --> 02:00:24,159 data 3168 02:00:25,040 --> 02:00:29,440 once again if i'm writing print function 3169 02:00:27,280 --> 02:00:31,679 and inside that if i'm writing n1 dot 3170 02:00:29,440 --> 02:00:31,679 next 3171 02:00:32,800 --> 02:00:37,040 then you can see that i'm getting the 3172 02:00:34,400 --> 02:00:39,040 data as 8 and the next that is a 3173 02:00:37,040 --> 02:00:41,920 reference i'm getting as none because i 3174 02:00:39,040 --> 02:00:43,679 didn't link this node to any other node 3175 02:00:41,920 --> 02:00:45,440 so this is the basic idea how to create 3176 02:00:43,679 --> 02:00:48,080 a node now let's see how to create a 3177 02:00:45,440 --> 02:00:50,080 class of singly linked list so 3178 02:00:48,080 --> 02:00:52,800 when will my singly link list will be 3179 02:00:50,080 --> 02:00:55,840 empty so as i told you that 3180 02:00:52,800 --> 02:00:55,840 if this is a node 3181 02:00:56,560 --> 02:01:03,760 let's suppose n1 this is another node n2 3182 02:01:01,520 --> 02:01:05,599 so n1 and n2 are connected with each 3183 02:01:03,760 --> 02:01:07,920 other so we are having a head pointer 3184 02:01:05,599 --> 02:01:10,400 which always points to the first node 3185 02:01:07,920 --> 02:01:12,639 right so if there is no head if head is 3186 02:01:10,400 --> 02:01:14,159 none then my linked list will be empty 3187 02:01:12,639 --> 02:01:15,920 so now what i will do here so i will 3188 02:01:14,159 --> 02:01:19,599 create a class and i will give the class 3189 02:01:15,920 --> 02:01:19,599 name as let's say singly link list 3190 02:01:20,560 --> 02:01:25,199 and inside this class once again i will 3191 02:01:22,239 --> 02:01:28,080 write init method so i'll write here def 3192 02:01:25,199 --> 02:01:31,040 underscore underscore init 3193 02:01:28,080 --> 02:01:33,599 underscore and i will write here self 3194 02:01:31,040 --> 02:01:34,560 now here i will write self 3195 02:01:33,599 --> 02:01:36,159 dot 3196 02:01:34,560 --> 02:01:38,080 head 3197 02:01:36,159 --> 02:01:39,679 is equal to none 3198 02:01:38,080 --> 02:01:41,840 so this is my condition to create a 3199 02:01:39,679 --> 02:01:43,520 class so if the head is pointing to none 3200 02:01:41,840 --> 02:01:45,040 that means it is not pointing to any 3201 02:01:43,520 --> 02:01:47,760 node and 3202 02:01:45,040 --> 02:01:49,440 it shows that linked list is empty 3203 02:01:47,760 --> 02:01:50,560 so now let's see the creation of single 3204 02:01:49,440 --> 02:01:54,159 link list 3205 02:01:50,560 --> 02:01:54,159 yeah so let me remove this 3206 02:01:57,280 --> 02:02:04,040 now i will create a class so 3207 02:02:00,320 --> 02:02:04,040 creating a linked list 3208 02:02:05,360 --> 02:02:08,560 so if i want to create a linked list i 3209 02:02:07,040 --> 02:02:11,119 will create a class of linked lists so i 3210 02:02:08,560 --> 02:02:13,599 will write here class 3211 02:02:11,119 --> 02:02:13,599 singly 3212 02:02:13,760 --> 02:02:17,440 linked list so this is my class and 3213 02:02:15,440 --> 02:02:20,400 inside this class i will once again 3214 02:02:17,440 --> 02:02:20,400 write init method 3215 02:02:23,679 --> 02:02:27,520 inside the single linked list i will 3216 02:02:25,280 --> 02:02:29,760 create a init method so i'll write def 3217 02:02:27,520 --> 02:02:31,280 underscore underscore init underscore 3218 02:02:29,760 --> 02:02:33,840 underscore and then i will write here 3219 02:02:31,280 --> 02:02:33,840 self 3220 02:02:35,360 --> 02:02:39,280 and when my link list will be empty so 3221 02:02:37,920 --> 02:02:40,480 when self 3222 02:02:39,280 --> 02:02:42,080 dot 3223 02:02:40,480 --> 02:02:44,719 head 3224 02:02:42,080 --> 02:02:46,560 is equal to none 3225 02:02:44,719 --> 02:02:48,320 so this is the simple way to create a 3226 02:02:46,560 --> 02:02:49,199 single english class 3227 02:02:48,320 --> 02:02:50,960 now 3228 02:02:49,199 --> 02:02:53,440 so after creating a class now let's 3229 02:02:50,960 --> 02:02:55,840 create a object of this linked list so 3230 02:02:53,440 --> 02:02:57,679 i'll write here sll singly link list 3231 02:02:55,840 --> 02:02:58,560 this object name 3232 02:02:57,679 --> 02:03:00,639 and 3233 02:02:58,560 --> 02:03:02,880 now i will write the class name so class 3234 02:03:00,639 --> 02:03:05,520 name is singly 3235 02:03:02,880 --> 02:03:06,400 and then capital ll is there right 3236 02:03:05,520 --> 02:03:09,199 so 3237 02:03:06,400 --> 02:03:11,360 on execution always remember whenever 3238 02:03:09,199 --> 02:03:14,000 you are creating an object and if you 3239 02:03:11,360 --> 02:03:16,239 are executing it so inside this class 3240 02:03:14,000 --> 02:03:18,480 init method will always run i will show 3241 02:03:16,239 --> 02:03:20,560 you the example if inside this init 3242 02:03:18,480 --> 02:03:23,760 method let's suppose if i'm writing here 3243 02:03:20,560 --> 02:03:23,760 print gaurav 3244 02:03:26,960 --> 02:03:32,639 so i've created the object here sll 3245 02:03:30,320 --> 02:03:35,679 and right now if i'm executing then you 3246 02:03:32,639 --> 02:03:37,840 can see that gaurav is executing here 3247 02:03:35,679 --> 02:03:40,080 right so always remember whenever i'm 3248 02:03:37,840 --> 02:03:42,239 creating an object of class and whenever 3249 02:03:40,080 --> 02:03:44,480 i'm executing it so whatever the 3250 02:03:42,239 --> 02:03:47,280 statements are inside the init method it 3251 02:03:44,480 --> 02:03:47,280 will execute 3252 02:03:48,159 --> 02:03:53,280 so on execution what will happen here 3253 02:03:50,960 --> 02:03:54,960 i will get here 3254 02:03:53,280 --> 02:03:58,159 sll 3255 02:03:54,960 --> 02:03:58,159 dot head 3256 02:03:58,639 --> 02:04:01,440 is equal to none 3257 02:04:02,400 --> 02:04:08,880 right so sll is my object so instead of 3258 02:04:05,520 --> 02:04:10,639 self sll will assign here so sll dot 3259 02:04:08,880 --> 02:04:12,719 head is equal to none 3260 02:04:10,639 --> 02:04:14,480 right so this is my basic concept of 3261 02:04:12,719 --> 02:04:15,679 creating a node and creating a linked 3262 02:04:14,480 --> 02:04:17,679 list 3263 02:04:15,679 --> 02:04:19,599 so after creating a node and then 3264 02:04:17,679 --> 02:04:21,760 creating a class of singling link list 3265 02:04:19,599 --> 02:04:23,920 now let's see the traversal in ling list 3266 02:04:21,760 --> 02:04:25,679 so what is traversal as i already told 3267 02:04:23,920 --> 02:04:28,320 you that traversal means going through 3268 02:04:25,679 --> 02:04:28,320 each node 3269 02:04:28,480 --> 02:04:33,280 so now let's see when we can do the 3270 02:04:30,079 --> 02:04:36,920 traversal in a linked list so this is my 3271 02:04:33,280 --> 02:04:36,920 singly linked list 3272 02:04:44,800 --> 02:04:50,320 so this is n1 node 3273 02:04:46,960 --> 02:04:53,199 this is n2 node this is n3 node and this 3274 02:04:50,320 --> 02:04:55,040 is n4 node and each node will contain a 3275 02:04:53,199 --> 02:04:57,360 data 3276 02:04:55,040 --> 02:04:59,599 as well as next 3277 02:04:57,360 --> 02:04:59,599 right 3278 02:04:59,920 --> 02:05:03,679 so this will contain a data next 3279 02:05:01,440 --> 02:05:06,400 similarly n3 and n4 will contain a data 3280 02:05:03,679 --> 02:05:08,880 in next so as of now these nodes are not 3281 02:05:06,400 --> 02:05:11,040 linked with each other and if i want to 3282 02:05:08,880 --> 02:05:13,199 do the traversal then i have to go 3283 02:05:11,040 --> 02:05:16,880 through each node that is from n1 then i 3284 02:05:13,199 --> 02:05:18,960 will go to n2 then n3 and then n4 so 3285 02:05:16,880 --> 02:05:21,360 now let's see the case scenario so the 3286 02:05:18,960 --> 02:05:22,800 first case will be if self dot head is 3287 02:05:21,360 --> 02:05:25,199 none 3288 02:05:22,800 --> 02:05:26,960 so in that case my linked list is empty 3289 02:05:25,199 --> 02:05:30,159 right so when my english is empty i 3290 02:05:26,960 --> 02:05:32,960 can't do the traversal so in this case i 3291 02:05:30,159 --> 02:05:36,560 will write here print 3292 02:05:32,960 --> 02:05:36,560 linked list is empty 3293 02:05:38,400 --> 02:05:42,800 if condition this is if condition and 3294 02:05:41,119 --> 02:05:45,840 then i will write one more condition 3295 02:05:42,800 --> 02:05:47,360 else and inside else if my self.head is 3296 02:05:45,840 --> 02:05:50,000 not none 3297 02:05:47,360 --> 02:05:51,520 then what i will do see as we know that 3298 02:05:50,000 --> 02:05:53,920 head will be always pointing to the 3299 02:05:51,520 --> 02:05:55,840 first node so this is my head here 3300 02:05:53,920 --> 02:05:59,119 so i will create a temporary variable a 3301 02:05:55,840 --> 02:05:59,119 is equal to self.head 3302 02:05:59,440 --> 02:06:03,920 now if i want to traverse then i have to 3303 02:06:01,280 --> 02:06:07,520 develop a logic so that i can go from n1 3304 02:06:03,920 --> 02:06:08,320 to n2 to n3 to n4 so i will use a while 3305 02:06:07,520 --> 02:06:10,880 loop 3306 02:06:08,320 --> 02:06:12,239 let me write here 3307 02:06:10,880 --> 02:06:14,320 this was else 3308 02:06:12,239 --> 02:06:15,440 and inside that i'm writing a is equal 3309 02:06:14,320 --> 02:06:16,639 to self 3310 02:06:15,440 --> 02:06:18,000 dot head 3311 02:06:16,639 --> 02:06:19,599 now i'll write a while loop so i'll 3312 02:06:18,000 --> 02:06:22,639 write here while 3313 02:06:19,599 --> 02:06:24,480 a is not none 3314 02:06:22,639 --> 02:06:27,199 i'll give a condition a is not none 3315 02:06:24,480 --> 02:06:29,360 because if my a is none and here a is 3316 02:06:27,199 --> 02:06:31,119 self dot head so if self dot head is 3317 02:06:29,360 --> 02:06:33,840 none then it's an empty linguist so 3318 02:06:31,119 --> 02:06:36,400 traversal can't be done so inside this i 3319 02:06:33,840 --> 02:06:39,440 will write a logic a is equal to a dot 3320 02:06:36,400 --> 02:06:41,679 next now what is a dot next here let's 3321 02:06:39,440 --> 02:06:44,560 suppose that this is a n 3322 02:06:41,679 --> 02:06:46,480 if i'm writing here n1 dot next 3323 02:06:44,560 --> 02:06:49,119 is equal to n2 3324 02:06:46,480 --> 02:06:51,199 so that means i am linking this node and 3325 02:06:49,119 --> 02:06:53,280 one node to n2 the link has been 3326 02:06:51,199 --> 02:06:54,880 generated now if i am writing here n2 3327 02:06:53,280 --> 02:06:57,440 dot next 3328 02:06:54,880 --> 02:06:59,440 is equal to n3 then again 3329 02:06:57,440 --> 02:07:01,840 the link has been generated and this 3330 02:06:59,440 --> 02:07:04,480 next will contain the address of n2 so 3331 02:07:01,840 --> 02:07:07,599 let's suppose the address of n2 is 1200 3332 02:07:04,480 --> 02:07:10,880 so this next will contain 1200 3333 02:07:07,599 --> 02:07:13,119 now if i'm writing here n3 dot next 3334 02:07:10,880 --> 02:07:15,119 so we know that this is next here right 3335 02:07:13,119 --> 02:07:17,920 so n3 dot next 3336 02:07:15,119 --> 02:07:17,920 will be n4 3337 02:07:18,079 --> 02:07:22,639 so you can see that i am writing here a 3338 02:07:20,480 --> 02:07:25,280 is equal to a dot next inside a while 3339 02:07:22,639 --> 02:07:27,679 condition so initially this will be my a 3340 02:07:25,280 --> 02:07:31,040 the loop will run and my a will shift to 3341 02:07:27,679 --> 02:07:33,760 n2 similarly my a will be is equal to n3 3342 02:07:31,040 --> 02:07:36,000 then similarly my a is equal to n4 so in 3343 02:07:33,760 --> 02:07:37,760 this way the traversing will be done 3344 02:07:36,000 --> 02:07:39,840 so i have given you the basic idea now 3345 02:07:37,760 --> 02:07:41,119 let's see the coding to understand in a 3346 02:07:39,840 --> 02:07:43,199 better way 3347 02:07:41,119 --> 02:07:45,679 so let's see the coding of traversal in 3348 02:07:43,199 --> 02:07:46,719 linked list so let me write here in 3349 02:07:45,679 --> 02:07:49,360 comment 3350 02:07:46,719 --> 02:07:53,079 traversal 3351 02:07:49,360 --> 02:07:53,079 and linked list 3352 02:07:54,320 --> 02:07:59,440 so first i will create a class 3353 02:07:57,199 --> 02:08:00,719 of a node and i will give the name as 3354 02:07:59,440 --> 02:08:02,719 node 3355 02:08:00,719 --> 02:08:04,320 and inside that i will create 3356 02:08:02,719 --> 02:08:06,480 my init method 3357 02:08:04,320 --> 02:08:08,320 so i'll write def underscore underscore 3358 02:08:06,480 --> 02:08:11,760 init 3359 02:08:08,320 --> 02:08:15,280 underscore underscore and then self 3360 02:08:11,760 --> 02:08:17,119 and i will write data also 3361 02:08:15,280 --> 02:08:18,400 because i will pass the data element 3362 02:08:17,119 --> 02:08:21,440 value 3363 02:08:18,400 --> 02:08:24,159 so i'll write here self dot data is 3364 02:08:21,440 --> 02:08:24,159 equal to data 3365 02:08:24,239 --> 02:08:28,400 and then i will write your cell dot 3366 02:08:26,880 --> 02:08:31,040 next 3367 02:08:28,400 --> 02:08:32,960 is equal to none 3368 02:08:31,040 --> 02:08:35,679 so this is my node in which i'm having a 3369 02:08:32,960 --> 02:08:38,960 data and i'm having my reference or link 3370 02:08:35,679 --> 02:08:38,960 you can say that as none 3371 02:08:40,079 --> 02:08:45,599 now after creating my node i will create 3372 02:08:42,079 --> 02:08:45,599 a class of single linked list 3373 02:08:47,920 --> 02:08:50,719 so this is a class of my single link 3374 02:08:49,679 --> 02:08:52,400 list 3375 02:08:50,719 --> 02:08:55,520 and inside this class once again i will 3376 02:08:52,400 --> 02:08:55,520 create a init method 3377 02:09:02,960 --> 02:09:08,320 now so as i know that my single link 3378 02:09:05,920 --> 02:09:10,719 list will be empty when my head is 3379 02:09:08,320 --> 02:09:12,880 pointing to none so if head is pointing 3380 02:09:10,719 --> 02:09:14,880 to none then ling list is empty so you 3381 02:09:12,880 --> 02:09:17,520 can't traverse right because traversal 3382 02:09:14,880 --> 02:09:18,400 can't be possible in the empty link list 3383 02:09:17,520 --> 02:09:20,159 so 3384 02:09:18,400 --> 02:09:21,599 this is my linked list so inside this 3385 02:09:20,159 --> 02:09:26,639 linked list first i'm giving the 3386 02:09:21,599 --> 02:09:26,639 condition self dot head is equal to none 3387 02:09:27,199 --> 02:09:29,440 now 3388 02:09:29,679 --> 02:09:35,599 inside the same class sll what i will do 3389 02:09:32,880 --> 02:09:39,760 i will create a function 3390 02:09:35,599 --> 02:09:39,760 def and i will write here traversal 3391 02:09:41,040 --> 02:09:45,280 and then i will write here self 3392 02:09:43,679 --> 02:09:46,960 now i will get the condition so the 3393 02:09:45,280 --> 02:09:49,040 first condition is that if your linked 3394 02:09:46,960 --> 02:09:51,760 list is empty so when your linked list 3395 02:09:49,040 --> 02:09:54,880 will be empty when self dot head is none 3396 02:09:51,760 --> 02:09:57,040 then you can't do the traversing 3397 02:09:54,880 --> 02:10:00,320 so i'll write here if 3398 02:09:57,040 --> 02:10:03,199 self dot head 3399 02:10:00,320 --> 02:10:03,199 is none 3400 02:10:03,360 --> 02:10:06,239 a right print 3401 02:10:07,119 --> 02:10:10,079 singly linguist 3402 02:10:10,639 --> 02:10:13,880 is empty 3403 02:10:17,119 --> 02:10:20,800 and i give the another condition else 3404 02:10:21,360 --> 02:10:25,599 so the first condition was that if self 3405 02:10:23,360 --> 02:10:27,360 dot head is none the ling list is empty 3406 02:10:25,599 --> 02:10:29,679 now coming to the next condition when 3407 02:10:27,360 --> 02:10:32,159 myself dot head is pointing to the first 3408 02:10:29,679 --> 02:10:33,040 node then only i can do the traversal 3409 02:10:32,159 --> 02:10:34,960 right 3410 02:10:33,040 --> 02:10:36,800 so so i will create a temporary variable 3411 02:10:34,960 --> 02:10:38,639 where a is equal to self dot head so why 3412 02:10:36,800 --> 02:10:40,960 am creating a temporary variable a 3413 02:10:38,639 --> 02:10:42,880 because i want my head value to be fixed 3414 02:10:40,960 --> 02:10:44,960 i don't want to change my head value but 3415 02:10:42,880 --> 02:10:46,079 i am using while loop here so a value 3416 02:10:44,960 --> 02:10:48,079 will be changed so that's why i've 3417 02:10:46,079 --> 02:10:49,920 created a temporary variable in which i 3418 02:10:48,079 --> 02:10:52,000 have assigned self.head so that head 3419 02:10:49,920 --> 02:10:55,880 value is not changed now after this what 3420 02:10:52,000 --> 02:10:55,880 i will do i'll write here while 3421 02:10:58,320 --> 02:11:01,679 while a is not none 3422 02:11:06,079 --> 02:11:11,840 i like simple print 3423 02:11:09,199 --> 02:11:11,840 a dot data 3424 02:11:12,400 --> 02:11:15,360 and then i will write here 3425 02:11:15,679 --> 02:11:19,440 and 3426 02:11:17,119 --> 02:11:21,040 is equal to then i will write codes and 3427 02:11:19,440 --> 02:11:22,639 i will give a space so that whatever the 3428 02:11:21,040 --> 02:11:24,639 data elements are printing there is a 3429 02:11:22,639 --> 02:11:26,639 space between them and then after that i 3430 02:11:24,639 --> 02:11:28,320 will write my logic a is equal to a dot 3431 02:11:26,639 --> 02:11:30,239 next 3432 02:11:28,320 --> 02:11:34,000 so a dot next why i am writing here to 3433 02:11:30,239 --> 02:11:35,840 connect one node with to the another 3434 02:11:34,000 --> 02:11:38,480 so this is my main logic that i have 3435 02:11:35,840 --> 02:11:40,560 written inside a traversal function 3436 02:11:38,480 --> 02:11:42,800 now let's see how to execute this 3437 02:11:40,560 --> 02:11:45,520 program so first i will create a node so 3438 02:11:42,800 --> 02:11:48,400 let me write here n1 is equal to node 3439 02:11:45,520 --> 02:11:50,880 and let me insert the values five 3440 02:11:48,400 --> 02:11:52,880 so i'll create four nodes in this so 3441 02:11:50,880 --> 02:11:55,760 i'll write here n2 3442 02:11:52,880 --> 02:11:58,000 is equal to node 3443 02:11:55,760 --> 02:12:00,800 i'll give the data element value 10 here 3444 02:11:58,000 --> 02:12:04,560 end to node and n3 3445 02:12:00,800 --> 02:12:04,560 i'll give the value as 15 3446 02:12:05,199 --> 02:12:10,239 and i will give the value for n4 let's 3447 02:12:07,119 --> 02:12:10,239 say 20 3448 02:12:10,880 --> 02:12:15,440 so these are the nodes right after 3449 02:12:12,960 --> 02:12:17,040 creating node what i will do here since 3450 02:12:15,440 --> 02:12:19,119 this is the sll class i have to create 3451 02:12:17,040 --> 02:12:23,520 the object of this also so i'll create a 3452 02:12:19,119 --> 02:12:23,520 object here so let me create the object 3453 02:12:26,560 --> 02:12:31,360 i'll create here sll is equal to 3454 02:12:31,599 --> 02:12:35,760 sll 3455 02:12:32,960 --> 02:12:38,400 so just write any variable name so i've 3456 02:12:35,760 --> 02:12:40,400 written here small sll then i've written 3457 02:12:38,400 --> 02:12:42,560 equal to then i've written the class 3458 02:12:40,400 --> 02:12:45,119 name capital s 3459 02:12:42,560 --> 02:12:47,199 small l small l then i've written 3460 02:12:45,119 --> 02:12:49,360 open and close parentheses right so this 3461 02:12:47,199 --> 02:12:51,440 is a way of creating a object of sll 3462 02:12:49,360 --> 02:12:54,400 class so as soon as i have created this 3463 02:12:51,440 --> 02:12:56,719 sll object this init method will run 3464 02:12:54,400 --> 02:12:58,639 automatically right so my sll.head is 3465 02:12:56,719 --> 02:13:01,360 equal to none initially now what i will 3466 02:12:58,639 --> 02:13:06,159 do here after this i will write here sll 3467 02:13:01,360 --> 02:13:07,920 dot head and i will assign to n1 3468 02:13:06,159 --> 02:13:10,880 now i have to connect the node so what i 3469 02:13:07,920 --> 02:13:13,360 will do here once i have created n2 node 3470 02:13:10,880 --> 02:13:14,960 so for connecting i will write here n1 3471 02:13:13,360 --> 02:13:17,840 dot next 3472 02:13:14,960 --> 02:13:17,840 is equal to n2 3473 02:13:18,800 --> 02:13:23,679 similarly after creating n3 node i will 3474 02:13:20,719 --> 02:13:27,199 write here n2 dot next 3475 02:13:23,679 --> 02:13:27,199 is equal to n3 3476 02:13:28,239 --> 02:13:34,079 and after creating n4 node i will write 3477 02:13:31,040 --> 02:13:36,239 here n3 dot next 3478 02:13:34,079 --> 02:13:38,000 is equal to n4 3479 02:13:36,239 --> 02:13:39,280 now what i will do here i want to call 3480 02:13:38,000 --> 02:13:41,679 this function so how to call this 3481 02:13:39,280 --> 02:13:44,320 function so what's the object of this 3482 02:13:41,679 --> 02:13:47,280 class this is right small sll so i'll 3483 02:13:44,320 --> 02:13:48,560 write here sll dot was the function name 3484 02:13:47,280 --> 02:13:51,040 traversal 3485 02:13:48,560 --> 02:13:52,880 so i'll write here traversal 3486 02:13:51,040 --> 02:13:54,880 so now what will happen here once you 3487 02:13:52,880 --> 02:13:56,159 are calling this function so instead of 3488 02:13:54,880 --> 02:13:59,760 self this 3489 02:13:56,159 --> 02:14:01,599 object will go sll will go here right 3490 02:13:59,760 --> 02:14:03,199 now let's execute this and then i will 3491 02:14:01,599 --> 02:14:05,599 explain you the logic how this is 3492 02:14:03,199 --> 02:14:05,599 working 3493 02:14:06,159 --> 02:14:09,760 so on execution you can see that 3494 02:14:07,840 --> 02:14:11,280 sll.head is equal to 3495 02:14:09,760 --> 02:14:14,159 n1 3496 02:14:11,280 --> 02:14:16,079 so here n1 is not defined yeah because 3497 02:14:14,159 --> 02:14:18,960 once i have to create n1 after that i 3498 02:14:16,079 --> 02:14:21,199 have to assign it right so let me 3499 02:14:18,960 --> 02:14:22,719 write here control x 3500 02:14:21,199 --> 02:14:24,239 and then ctrl v 3501 02:14:22,719 --> 02:14:26,480 now if i'm running it 3502 02:14:24,239 --> 02:14:28,639 you can see that i am getting 5 10 15 20 3503 02:14:26,480 --> 02:14:30,560 right because here first i have assigned 3504 02:14:28,639 --> 02:14:32,960 sll dot head is equal to n1 so here what 3505 02:14:30,560 --> 02:14:35,199 was the mistake i didn't create the n1 3506 02:14:32,960 --> 02:14:37,040 node first and i have assigned the sl 3507 02:14:35,199 --> 02:14:38,320 dot head is equal to n1 so first you 3508 02:14:37,040 --> 02:14:40,560 have to create the node then only you 3509 02:14:38,320 --> 02:14:42,560 can assign right 3510 02:14:40,560 --> 02:14:46,000 now let's understand the logic so here 3511 02:14:42,560 --> 02:14:47,760 n1 is the object of class node right so 3512 02:14:46,000 --> 02:14:50,000 n one is equal to node five and five is 3513 02:14:47,760 --> 02:14:52,480 the data element value so when i'm 3514 02:14:50,000 --> 02:14:54,800 writing this so what i will get here so 3515 02:14:52,480 --> 02:14:56,800 instead of self n1 will pass and instead 3516 02:14:54,800 --> 02:14:59,119 of data 5 will pass so i will get here 3517 02:14:56,800 --> 02:15:02,400 n1.data is equal to 5 and i will get 3518 02:14:59,119 --> 02:15:02,400 here n1.next 3519 02:15:02,639 --> 02:15:05,440 is equal to none 3520 02:15:07,040 --> 02:15:10,880 so after this i have created the object 3521 02:15:08,719 --> 02:15:12,239 of my singly linked list so what will 3522 02:15:10,880 --> 02:15:14,560 happen as soon as i am creating the 3523 02:15:12,239 --> 02:15:16,719 object this init method will run so 3524 02:15:14,560 --> 02:15:18,639 instead of self sll will pass so i will 3525 02:15:16,719 --> 02:15:21,119 be getting here 3526 02:15:18,639 --> 02:15:23,119 sll dot 3527 02:15:21,119 --> 02:15:25,199 head 3528 02:15:23,119 --> 02:15:27,280 is equal to none 3529 02:15:25,199 --> 02:15:29,360 so as of now you can see that my sl dot 3530 02:15:27,280 --> 02:15:31,119 head is equal to none so still my 3531 02:15:29,360 --> 02:15:32,239 list is empty 3532 02:15:31,119 --> 02:15:34,480 right 3533 02:15:32,239 --> 02:15:37,840 so i have to assign this sll dot head to 3534 02:15:34,480 --> 02:15:39,520 n1 so that my head is pointing to n1 so 3535 02:15:37,840 --> 02:15:42,159 here i've written sl dot head is equal 3536 02:15:39,520 --> 02:15:43,679 to n1 so now my head is pointing to n1 3537 02:15:42,159 --> 02:15:44,800 now after that once again i have created 3538 02:15:43,679 --> 02:15:45,760 the node 3539 02:15:44,800 --> 02:15:47,280 n2 3540 02:15:45,760 --> 02:15:48,880 so 3541 02:15:47,280 --> 02:15:50,400 what will happen here i will get here 3542 02:15:48,880 --> 02:15:53,119 n2.data 3543 02:15:50,400 --> 02:15:53,119 is equal to 10 3544 02:15:54,719 --> 02:15:57,840 let me write comma u 3545 02:15:59,520 --> 02:16:02,800 and enter.next 3546 02:16:02,880 --> 02:16:06,159 i will be getting as none 3547 02:16:06,480 --> 02:16:11,119 now as soon as i have created my n2 node 3548 02:16:09,119 --> 02:16:12,960 what i will do i will connect n1 node 3549 02:16:11,119 --> 02:16:15,920 with n2 so i have written here n1 dot 3550 02:16:12,960 --> 02:16:18,320 next is equal to n2 so initially my n1 3551 02:16:15,920 --> 02:16:20,560 dot next is equal to none but now my n1 3552 02:16:18,320 --> 02:16:22,960 dot next is equal to n2 so as of now 3553 02:16:20,560 --> 02:16:24,800 i've created the node n1 and n2 and the 3554 02:16:22,960 --> 02:16:28,079 head is pointing to n1 and i've also 3555 02:16:24,800 --> 02:16:29,840 linked the nmr node with n2 3556 02:16:28,079 --> 02:16:32,319 similarly once again i'm creating a n3 3557 02:16:29,840 --> 02:16:34,559 node so 3558 02:16:32,319 --> 02:16:38,319 n3 dot data 3559 02:16:34,559 --> 02:16:38,319 n3 dot data is equal to 15 3560 02:16:39,519 --> 02:16:43,840 and after at n3 3561 02:16:41,920 --> 02:16:45,439 dot next 3562 02:16:43,840 --> 02:16:47,840 is equal to 3563 02:16:45,439 --> 02:16:47,840 none 3564 02:16:49,200 --> 02:16:52,319 now after creating this 3565 02:16:50,800 --> 02:16:55,120 what will happen once i have created my 3566 02:16:52,319 --> 02:16:57,120 n3 node then i will connect n2 with n3 3567 02:16:55,120 --> 02:16:59,359 so i've written here n2 dot next is 3568 02:16:57,120 --> 02:17:01,519 equal to n3 similarly once again i will 3569 02:16:59,359 --> 02:17:03,280 create my n4 node so again the same 3570 02:17:01,519 --> 02:17:05,519 logic will perform n4 is equal to node 3571 02:17:03,280 --> 02:17:08,240 20. so again i've written here n4 so n4 3572 02:17:05,519 --> 02:17:11,200 is my object of class node and n4 will 3573 02:17:08,240 --> 02:17:14,800 pass into self and my data will be 20. 3574 02:17:11,200 --> 02:17:16,559 so let me write here n4.data 3575 02:17:14,800 --> 02:17:18,000 is equal to 20 3576 02:17:16,559 --> 02:17:20,319 and the same thing 3577 02:17:18,000 --> 02:17:22,719 and 4 3578 02:17:20,319 --> 02:17:25,439 dot next 3579 02:17:22,719 --> 02:17:25,439 is equal to none 3580 02:17:26,639 --> 02:17:31,439 so after creating n4 3581 02:17:28,800 --> 02:17:33,200 now i will connect n3 to n4 right so i'm 3582 02:17:31,439 --> 02:17:37,439 having only four node here you can see 3583 02:17:33,200 --> 02:17:39,920 that i've created n1 n2 n3 and n4 3584 02:17:37,439 --> 02:17:42,479 so after n4 i don't have any node so 3585 02:17:39,920 --> 02:17:44,559 what will be n4.next is equal to none so 3586 02:17:42,479 --> 02:17:45,519 that's why i didn't write anything after 3587 02:17:44,559 --> 02:17:46,639 this 3588 02:17:45,519 --> 02:17:48,559 so 3589 02:17:46,639 --> 02:17:50,000 now it's time for calling this function 3590 02:17:48,559 --> 02:17:52,719 so how to call this function so i have 3591 02:17:50,000 --> 02:17:55,120 already created the object of sll class 3592 02:17:52,719 --> 02:17:57,519 so the object was a small sll so i've 3593 02:17:55,120 --> 02:17:59,760 write small sll dot traversal which is 3594 02:17:57,519 --> 02:18:02,559 the name of this function so inside this 3595 02:17:59,760 --> 02:18:04,160 cell sll will pass here right 3596 02:18:02,559 --> 02:18:07,359 so what will happen here 3597 02:18:04,160 --> 02:18:09,200 sll dot head so sll dot head is what 3598 02:18:07,359 --> 02:18:11,040 initially sl dot head is equal to none 3599 02:18:09,200 --> 02:18:14,399 but i have a sign here sl dot head is 3600 02:18:11,040 --> 02:18:16,240 equal to n1 right so this is not none so 3601 02:18:14,399 --> 02:18:17,519 it will jump into this condition so now 3602 02:18:16,240 --> 02:18:20,080 what will happen here let's see the 3603 02:18:17,519 --> 02:18:24,160 logic so here a is equal to self dot 3604 02:18:20,080 --> 02:18:28,160 head and what's my self dot head 3605 02:18:24,160 --> 02:18:28,160 myself dot head is sll dot head 3606 02:18:28,639 --> 02:18:33,359 so my a is equal to sl dot head so now 3607 02:18:31,280 --> 02:18:34,399 after this i have created a while loop 3608 02:18:33,359 --> 02:18:36,559 so here 3609 02:18:34,399 --> 02:18:38,639 i will write here while and then instead 3610 02:18:36,559 --> 02:18:40,000 of a i i'll write sll dot head is not 3611 02:18:38,639 --> 02:18:41,439 none so 3612 02:18:40,000 --> 02:18:43,599 after this what will happen this is my 3613 02:18:41,439 --> 02:18:46,399 while loop so here i have written while 3614 02:18:43,599 --> 02:18:48,000 a is not named so my a value is sll dot 3615 02:18:46,399 --> 02:18:50,880 head so 3616 02:18:48,000 --> 02:18:53,439 is my sll dot head is none no why 3617 02:18:50,880 --> 02:18:56,240 because my sll dot head is pointing to 3618 02:18:53,439 --> 02:18:58,880 n1 you can see that my sll dot head is 3619 02:18:56,240 --> 02:19:00,000 pointing to n1 so it's not none 3620 02:18:58,880 --> 02:19:03,200 right 3621 02:19:00,000 --> 02:19:04,880 so what will happen here 3622 02:19:03,200 --> 02:19:07,200 initially my 3623 02:19:04,880 --> 02:19:08,559 is 3624 02:19:07,200 --> 02:19:09,519 let me write here 3625 02:19:08,559 --> 02:19:12,479 is 3626 02:19:09,519 --> 02:19:12,479 sll dot head 3627 02:19:15,840 --> 02:19:22,319 so now it will print me 3628 02:19:19,280 --> 02:19:24,160 the value a dot data so what is a dot 3629 02:19:22,319 --> 02:19:26,000 data 3630 02:19:24,160 --> 02:19:28,160 so slr dot head is nothing but pointing 3631 02:19:26,000 --> 02:19:30,719 to n1 so i will write here n1 3632 02:19:28,160 --> 02:19:32,319 so a dot data is nothing but it is equal 3633 02:19:30,719 --> 02:19:35,840 to n1 dot data 3634 02:19:32,319 --> 02:19:38,000 and what's n1 dot data n1.data is 5 so 3635 02:19:35,840 --> 02:19:40,000 it will print 5 right so you can see 3636 02:19:38,000 --> 02:19:41,920 that i'm getting 5 as the output now if 3637 02:19:40,000 --> 02:19:45,840 you see here 3638 02:19:41,920 --> 02:19:48,399 a is equal to a dot next so what is my a 3639 02:19:45,840 --> 02:19:52,319 a is nothing but my n1 right 3640 02:19:48,399 --> 02:19:53,760 so i'm writing here n1 dot next 3641 02:19:52,319 --> 02:19:55,600 so here you can see that i'm getting a 3642 02:19:53,760 --> 02:19:57,359 is equal to n1 dot next so again it's a 3643 02:19:55,600 --> 02:19:59,680 while loop right so once again it will 3644 02:19:57,359 --> 02:20:02,720 jump here now a value is equal to n1 dot 3645 02:19:59,680 --> 02:20:05,520 next so n1 dot next is pointing to none 3646 02:20:02,720 --> 02:20:07,760 no n1 dot next is pointing to n2 so it's 3647 02:20:05,520 --> 02:20:08,960 not none so this condition is still 3648 02:20:07,760 --> 02:20:11,200 satisfying 3649 02:20:08,960 --> 02:20:14,160 right and once again it will print the 3650 02:20:11,200 --> 02:20:14,160 data that is 3651 02:20:15,280 --> 02:20:20,319 here the a value will be 3652 02:20:17,920 --> 02:20:21,680 n1 dot 3653 02:20:20,319 --> 02:20:23,439 next 3654 02:20:21,680 --> 02:20:26,080 so now once again if i'm moving to this 3655 02:20:23,439 --> 02:20:27,920 print statement so a dot data so let me 3656 02:20:26,080 --> 02:20:29,359 write here a dot data so what will be 3657 02:20:27,920 --> 02:20:31,840 the a dot data 3658 02:20:29,359 --> 02:20:35,359 this time n1 dot next so what is n1 dot 3659 02:20:31,840 --> 02:20:38,160 next n1 dot next is nothing but n2 right 3660 02:20:35,359 --> 02:20:41,280 so n2 dot data 3661 02:20:38,160 --> 02:20:43,840 it will give so n2 dot data is what 3662 02:20:41,280 --> 02:20:47,120 n2 dot data i am getting as 10 3663 02:20:43,840 --> 02:20:48,800 right so it will print 10. similarly 3664 02:20:47,120 --> 02:20:50,880 once again 3665 02:20:48,800 --> 02:20:52,399 you can see that my a is equal to i will 3666 02:20:50,880 --> 02:20:57,560 be getting here 3667 02:20:52,399 --> 02:20:57,560 a is equal to n2 dot next 3668 02:20:58,080 --> 02:21:02,000 so once again it is going into dot next 3669 02:20:59,840 --> 02:21:05,200 so n2 dot next is containing the address 3670 02:21:02,000 --> 02:21:07,680 of n3 so it's not none so once again it 3671 02:21:05,200 --> 02:21:10,319 will print here n2 dot next dot data so 3672 02:21:07,680 --> 02:21:12,640 what is n2 dot next n2 dot next is 3673 02:21:10,319 --> 02:21:13,359 nothing but n3 so n3 dot data will give 3674 02:21:12,640 --> 02:21:14,640 me 3675 02:21:13,359 --> 02:21:15,520 15 3676 02:21:14,640 --> 02:21:17,680 right 3677 02:21:15,520 --> 02:21:21,040 now what will be a a is equal to a dot 3678 02:21:17,680 --> 02:21:23,840 next so my a value is here n3 so n3 dot 3679 02:21:21,040 --> 02:21:26,720 next right so my a value will be here 3680 02:21:23,840 --> 02:21:29,840 entry dot next so once again it will go 3681 02:21:26,720 --> 02:21:32,640 here so a is equal to n3 dot next so n3 3682 02:21:29,840 --> 02:21:33,520 dot next is none no it's again pointing 3683 02:21:32,640 --> 02:21:36,080 to 3684 02:21:33,520 --> 02:21:39,600 n4 that means entry dot next contains 3685 02:21:36,080 --> 02:21:41,920 the address of n4 right so it's not none 3686 02:21:39,600 --> 02:21:44,160 so a is equal to n3 dot next so what is 3687 02:21:41,920 --> 02:21:47,520 n3 dot next so n3 dot next is nothing 3688 02:21:44,160 --> 02:21:50,160 but n4 so n4 dot data will give me 20 3689 02:21:47,520 --> 02:21:51,920 right so after giving me 20 once again 3690 02:21:50,160 --> 02:21:54,080 i'm getting here 3691 02:21:51,920 --> 02:21:57,359 n4 now what will happen here a is equal 3692 02:21:54,080 --> 02:22:00,000 to a dot next and my a value is n4 right 3693 02:21:57,359 --> 02:22:02,720 so a is equal to n4 dot next so if i'm 3694 02:22:00,000 --> 02:22:04,960 getting here n4 dot next so what is n4 3695 02:22:02,720 --> 02:22:06,560 dot next and four dot next i am getting 3696 02:22:04,960 --> 02:22:08,479 here none right 3697 02:22:06,560 --> 02:22:11,200 so yeah this condition is not satisfying 3698 02:22:08,479 --> 02:22:13,680 and hence the loop will end 3699 02:22:11,200 --> 02:22:16,080 because n4 dot next is equal to none and 3700 02:22:13,680 --> 02:22:17,840 my condition is while a is not none so 3701 02:22:16,080 --> 02:22:20,560 this is the basic logic so in this way 3702 02:22:17,840 --> 02:22:23,280 we'll get the values 5 10 15 20 and the 3703 02:22:20,560 --> 02:22:25,280 traversal will be done in linked list 3704 02:22:23,280 --> 02:22:27,200 now after traversal we will see the 3705 02:22:25,280 --> 02:22:29,040 insertion in singly linked list so 3706 02:22:27,200 --> 02:22:30,800 insertion can be done in three ways we 3707 02:22:29,040 --> 02:22:32,880 can do the insertion at the beginning of 3708 02:22:30,800 --> 02:22:34,479 the node we can also do the insertion at 3709 02:22:32,880 --> 02:22:37,040 the end of the node and we can do the 3710 02:22:34,479 --> 02:22:39,120 insertion at specified node now let's 3711 02:22:37,040 --> 02:22:41,600 see the insertion first at the beginning 3712 02:22:39,120 --> 02:22:43,439 so in the previous coding example i was 3713 02:22:41,600 --> 02:22:47,600 having four nodes 3714 02:22:43,439 --> 02:22:47,600 so let me write here the name of node n1 3715 02:22:48,840 --> 02:22:51,680 n2 3716 02:22:50,560 --> 02:22:53,040 n3 3717 02:22:51,680 --> 02:22:55,040 and n4 3718 02:22:53,040 --> 02:22:57,840 and i've also shown you the traversal in 3719 02:22:55,040 --> 02:22:59,600 linked list so here i've assigned the 3720 02:22:57,840 --> 02:23:00,880 data as 5 3721 02:22:59,600 --> 02:23:02,000 10 3722 02:23:00,880 --> 02:23:03,600 15 3723 02:23:02,000 --> 02:23:05,439 and 20 3724 02:23:03,600 --> 02:23:07,280 right 3725 02:23:05,439 --> 02:23:09,600 and this n1 was connected to n2 3726 02:23:07,280 --> 02:23:11,600 similarly n2 was connected to n3 n3 to 3727 02:23:09,600 --> 02:23:13,840 n4 so we have seen the traversal like 3728 02:23:11,600 --> 02:23:15,680 this head was pointing to my n1 node 3729 02:23:13,840 --> 02:23:17,760 right so this is nothing but my link so 3730 02:23:15,680 --> 02:23:19,439 link will contain the address of this n2 3731 02:23:17,760 --> 02:23:21,600 road 3732 02:23:19,439 --> 02:23:23,200 similarly this is again the link of end 3733 02:23:21,600 --> 02:23:25,920 to node so this will contain the address 3734 02:23:23,200 --> 02:23:28,000 of n3 node so up to here we have seen 3735 02:23:25,920 --> 02:23:29,920 right now i will insert a node at 3736 02:23:28,000 --> 02:23:32,720 beginning so 3737 02:23:29,920 --> 02:23:35,040 let me create a node nb so how i will 3738 02:23:32,720 --> 02:23:37,359 create a new node i will write here nb 3739 02:23:35,040 --> 02:23:39,920 is equal to node 3740 02:23:37,359 --> 02:23:41,680 and i'll write here data so nb is 3741 02:23:39,920 --> 02:23:44,319 nothing but it is an object of a class 3742 02:23:41,680 --> 02:23:46,479 node right so here instead of data if 3743 02:23:44,319 --> 02:23:48,640 i'm passing to so my data will be here 3744 02:23:46,479 --> 02:23:51,600 too and this next value or you can say 3745 02:23:48,640 --> 02:23:53,520 the reference will be none 3746 02:23:51,600 --> 02:23:57,760 right now i didn't link this nb node to 3747 02:23:53,520 --> 02:23:57,760 anyone so this is my nb node here 3748 02:23:57,920 --> 02:24:02,720 and let's suppose the data is here too 3749 02:24:00,640 --> 02:24:03,840 and it's not connected so initially it 3750 02:24:02,720 --> 02:24:06,479 will be none 3751 02:24:03,840 --> 02:24:08,960 now i have to find the link between nb 3752 02:24:06,479 --> 02:24:12,319 node to n1 right so what will be the 3753 02:24:08,960 --> 02:24:15,520 step nb dot next 3754 02:24:12,319 --> 02:24:17,359 is equal to self dot head 3755 02:24:15,520 --> 02:24:19,200 so this is a simple logic so when i'm 3756 02:24:17,359 --> 02:24:21,840 writing nb dot next is equal to 3757 02:24:19,200 --> 02:24:24,399 self.head that means i'm creating a link 3758 02:24:21,840 --> 02:24:26,479 from nb dot next because this is my next 3759 02:24:24,399 --> 02:24:30,880 right and i'm creating a link from nb to 3760 02:24:26,479 --> 02:24:33,200 n1 now if i am creating a np node 3761 02:24:30,880 --> 02:24:35,439 then this is my first node 3762 02:24:33,200 --> 02:24:38,160 so you can see here nb is now my first 3763 02:24:35,439 --> 02:24:39,920 node so head must point to nb right 3764 02:24:38,160 --> 02:24:42,560 because head will point to the first 3765 02:24:39,920 --> 02:24:45,280 node so here i have to remove this head 3766 02:24:42,560 --> 02:24:47,359 and point to here so for that which 3767 02:24:45,280 --> 02:24:50,080 logic should i write i will write here 3768 02:24:47,359 --> 02:24:50,080 self dot head 3769 02:24:50,240 --> 02:24:54,479 is equal to nb 3770 02:24:52,640 --> 02:24:57,840 so now my head will be pointing to this 3771 02:24:54,479 --> 02:24:57,840 nb so let me remove this 3772 02:25:03,040 --> 02:25:06,399 so i will write here now let's see it in 3773 02:25:05,040 --> 02:25:08,160 coding 3774 02:25:06,399 --> 02:25:10,160 so this is my traversal code so what i 3775 02:25:08,160 --> 02:25:11,840 will do here inside this class singly 3776 02:25:10,160 --> 02:25:14,640 linked list i will create one more 3777 02:25:11,840 --> 02:25:16,160 function and let me create the function 3778 02:25:14,640 --> 02:25:18,800 and i will give the function name let's 3779 02:25:16,160 --> 02:25:20,479 suppose def and the function name insert 3780 02:25:18,800 --> 02:25:23,479 at the beginning so i'll write here 3781 02:25:20,479 --> 02:25:23,479 insert 3782 02:25:24,640 --> 02:25:27,359 at beginning 3783 02:25:31,840 --> 02:25:35,200 so i have to insert here so which 3784 02:25:33,439 --> 02:25:36,800 parameter cell i gave you i have to 3785 02:25:35,200 --> 02:25:40,399 write self 3786 02:25:36,800 --> 02:25:42,479 first so after self i have to give data 3787 02:25:40,399 --> 02:25:44,800 because i want to put the data value 3788 02:25:42,479 --> 02:25:46,800 inside a node 3789 02:25:44,800 --> 02:25:49,439 so inside this function i will first 3790 02:25:46,800 --> 02:25:52,439 create a node so nb is equal to 3791 02:25:49,439 --> 02:25:52,439 node 3792 02:25:52,640 --> 02:25:57,280 and v is equal to node and then inside 3793 02:25:54,399 --> 02:25:58,960 this node i will write here data 3794 02:25:57,280 --> 02:26:00,960 now 3795 02:25:58,960 --> 02:26:04,800 after this i will perform my logic so 3796 02:26:00,960 --> 02:26:07,120 what my logic is my logic is nb.next 3797 02:26:04,800 --> 02:26:09,040 is equal to self 3798 02:26:07,120 --> 02:26:11,200 dot head 3799 02:26:09,040 --> 02:26:13,280 so nb dot next is equal to self.head 3800 02:26:11,200 --> 02:26:15,359 will create a link between nv and n1 3801 02:26:13,280 --> 02:26:18,560 node and after that i want to change my 3802 02:26:15,359 --> 02:26:21,120 head from n1 to nb so i'll write here 3803 02:26:18,560 --> 02:26:22,479 self dot head 3804 02:26:21,120 --> 02:26:24,319 is equal to 3805 02:26:22,479 --> 02:26:26,160 nb 3806 02:26:24,319 --> 02:26:27,840 so this is my function so how to call a 3807 02:26:26,160 --> 02:26:30,160 function so here you can see that this 3808 02:26:27,840 --> 02:26:32,000 is a class sll so the object of this 3809 02:26:30,160 --> 02:26:33,359 class is sll 3810 02:26:32,000 --> 02:26:34,720 so i can 3811 02:26:33,359 --> 02:26:36,080 call it through 3812 02:26:34,720 --> 02:26:37,600 i'll write here 3813 02:26:36,080 --> 02:26:39,200 sll 3814 02:26:37,600 --> 02:26:42,800 dot 3815 02:26:39,200 --> 02:26:42,800 and after that i will write here insert 3816 02:26:43,439 --> 02:26:47,240 at 3817 02:26:44,240 --> 02:26:47,240 beginning 3818 02:26:48,720 --> 02:26:53,439 and let me put the value here too 3819 02:26:51,439 --> 02:26:55,280 so i will call this function and 3820 02:26:53,439 --> 02:26:58,160 instead of data i have given the value 3821 02:26:55,280 --> 02:27:00,399 as 2 so all the statements inside this 3822 02:26:58,160 --> 02:27:02,880 function will execute 3823 02:27:00,399 --> 02:27:04,399 now i want to print this function right 3824 02:27:02,880 --> 02:27:07,439 so here you can see that the traversal 3825 02:27:04,399 --> 02:27:11,280 logic is written here so i'll write here 3826 02:27:07,439 --> 02:27:11,280 sll dot traversal 3827 02:27:12,240 --> 02:27:15,520 and now if i'm executing it 3828 02:27:15,680 --> 02:27:20,800 so you can see that i'm getting my 3829 02:27:17,680 --> 02:27:23,040 output but let me write here print so if 3830 02:27:20,800 --> 02:27:25,120 i'm writing here print then i will get 3831 02:27:23,040 --> 02:27:27,120 my output in the next line right because 3832 02:27:25,120 --> 02:27:28,840 here inside this print i didn't write 3833 02:27:27,120 --> 02:27:32,080 anything so it will give a 3834 02:27:28,840 --> 02:27:35,680 space so now if you see i'm getting 2 5 3835 02:27:32,080 --> 02:27:38,399 10 15 20 so the nb node was having a 3836 02:27:35,680 --> 02:27:40,880 data element as 2 right here you can see 3837 02:27:38,399 --> 02:27:42,640 that my envy node has been inserted so 3838 02:27:40,880 --> 02:27:44,720 this is my first node so the new link 3839 02:27:42,640 --> 02:27:47,120 list will be having the elements 2 5 10 3840 02:27:44,720 --> 02:27:49,600 15 20. now let's understand the logic 3841 02:27:47,120 --> 02:27:51,520 how this thing is working so here i've 3842 02:27:49,600 --> 02:27:54,000 given the data as 2 3843 02:27:51,520 --> 02:27:56,960 so data is equal to 2 here 3844 02:27:54,000 --> 02:27:56,960 and now after this 3845 02:27:57,280 --> 02:28:00,960 and b is equal to 3846 02:27:59,200 --> 02:28:03,200 node 3847 02:28:00,960 --> 02:28:05,439 instead of data two will be here so now 3848 02:28:03,200 --> 02:28:08,160 if you see this this is my object of nb 3849 02:28:05,439 --> 02:28:11,200 right and this is a class so here what i 3850 02:28:08,160 --> 02:28:13,120 will get here i'll be getting here 3851 02:28:11,200 --> 02:28:16,720 let me write comma 3852 02:28:13,120 --> 02:28:16,720 i'll be getting here nb.data 3853 02:28:17,520 --> 02:28:22,319 is equal to what's the data value 2 3854 02:28:20,720 --> 02:28:24,840 and now 3855 02:28:22,319 --> 02:28:26,640 i'll getting here nb dot 3856 02:28:24,840 --> 02:28:27,920 next 3857 02:28:26,640 --> 02:28:29,359 is equal to 3858 02:28:27,920 --> 02:28:31,359 none 3859 02:28:29,359 --> 02:28:34,080 so this is the creation of an node which 3860 02:28:31,359 --> 02:28:36,399 is having a data element as 2 and its 3861 02:28:34,080 --> 02:28:38,399 next that is the reference is none so 3862 02:28:36,399 --> 02:28:40,240 this means this node is not connected 3863 02:28:38,399 --> 02:28:42,080 with any other node now i have to 3864 02:28:40,240 --> 02:28:45,359 connect this node so i have written the 3865 02:28:42,080 --> 02:28:45,359 logic here nb dot next 3866 02:28:45,600 --> 02:28:52,160 so here you can see that nb dot next 3867 02:28:49,520 --> 02:28:54,800 is equal to self dot head so what is my 3868 02:28:52,160 --> 02:28:57,359 self self is nothing but i am calling 3869 02:28:54,800 --> 02:28:59,600 through the object sll right 3870 02:28:57,359 --> 02:29:01,840 so instead of self it will be sll so 3871 02:28:59,600 --> 02:29:04,640 sll.head is what 3872 02:29:01,840 --> 02:29:06,800 sll.head is myanman 3873 02:29:04,640 --> 02:29:10,160 here you can see that 3874 02:29:06,800 --> 02:29:12,880 so it will be n1 so here sll dot head is 3875 02:29:10,160 --> 02:29:15,520 nothing but my n1 now what will happen 3876 02:29:12,880 --> 02:29:17,120 here nb dot next and this is the n1 node 3877 02:29:15,520 --> 02:29:19,439 so i have created a link between these 3878 02:29:17,120 --> 02:29:21,920 two nodes so nb and n1 node are now 3879 02:29:19,439 --> 02:29:23,600 connected now after connection i have to 3880 02:29:21,920 --> 02:29:25,840 change my head position so my initially 3881 02:29:23,600 --> 02:29:29,359 my head position was assigning to n1 but 3882 02:29:25,840 --> 02:29:29,359 i have to change it so how to change it 3883 02:29:29,840 --> 02:29:36,399 so here instead of self my sll will be 3884 02:29:32,479 --> 02:29:37,920 here so sll dot head is equal to nv 3885 02:29:36,399 --> 02:29:40,080 so you can see that i have changed my 3886 02:29:37,920 --> 02:29:42,240 head position so this is the basic logic 3887 02:29:40,080 --> 02:29:44,160 for the insertion at the beginning so 3888 02:29:42,240 --> 02:29:46,560 what i've done here i've created a node 3889 02:29:44,160 --> 02:29:48,640 then i connected the nv node with n1 and 3890 02:29:46,560 --> 02:29:51,280 then i've changed the head position from 3891 02:29:48,640 --> 02:29:53,040 n1 to nb this is the basic logic for 3892 02:29:51,280 --> 02:29:54,720 insertion at beginning 3893 02:29:53,040 --> 02:29:58,160 now we will see the logic of insertion 3894 02:29:54,720 --> 02:29:59,760 at end so this was my node 3895 02:29:58,160 --> 02:30:01,520 initially i was having 3896 02:29:59,760 --> 02:30:03,040 four nodes n1 3897 02:30:01,520 --> 02:30:04,560 n2 3898 02:30:03,040 --> 02:30:07,200 n3 3899 02:30:04,560 --> 02:30:07,200 and n4 3900 02:30:07,439 --> 02:30:11,520 but i've created a new node right 3901 02:30:09,120 --> 02:30:13,040 insertion at beginning so this will my 3902 02:30:11,520 --> 02:30:15,760 head 3903 02:30:13,040 --> 02:30:17,840 and it was containing a data s2 3904 02:30:15,760 --> 02:30:19,840 and the link of this nb node contains 3905 02:30:17,840 --> 02:30:23,280 the address of n1 node 3906 02:30:19,840 --> 02:30:27,040 so n1 node data was 5 here 10 3907 02:30:23,280 --> 02:30:29,680 for n3 it was 15 and 20 3908 02:30:27,040 --> 02:30:31,200 and n1 was connected to n2 and 2 was n3 3909 02:30:29,680 --> 02:30:34,319 and 32 n4 3910 02:30:31,200 --> 02:30:36,160 and here next value was none 3911 02:30:34,319 --> 02:30:37,520 so this is the singly linked list up to 3912 02:30:36,160 --> 02:30:39,439 insertion at the beginning now we will 3913 02:30:37,520 --> 02:30:41,600 see the insertion at end 3914 02:30:39,439 --> 02:30:43,680 so for the end insertion i have to 3915 02:30:41,600 --> 02:30:45,840 create a node 3916 02:30:43,680 --> 02:30:48,160 so let me give the node name here any 3917 02:30:45,840 --> 02:30:49,520 and once again this node will contain a 3918 02:30:48,160 --> 02:30:50,560 data 3919 02:30:49,520 --> 02:30:52,479 as well as 3920 02:30:50,560 --> 02:30:55,600 next or you can say the link but this is 3921 02:30:52,479 --> 02:30:58,880 the last node insertion at last so this 3922 02:30:55,600 --> 02:30:58,880 will contain the value as none 3923 02:30:58,960 --> 02:31:03,280 so initially when you are creating any 3924 02:31:00,399 --> 02:31:04,880 node then its link is pointing to none 3925 02:31:03,280 --> 02:31:06,720 so this is the any node and here the 3926 02:31:04,880 --> 02:31:08,800 link is pointing to none but this is the 3927 02:31:06,720 --> 02:31:09,760 last node so i want the link to be none 3928 02:31:08,800 --> 02:31:10,800 itself 3929 02:31:09,760 --> 02:31:12,479 right 3930 02:31:10,800 --> 02:31:14,080 now i'll give the data here 3931 02:31:12,479 --> 02:31:15,040 so let me write here n e is equal to 3932 02:31:14,080 --> 02:31:16,640 node 3933 02:31:15,040 --> 02:31:18,960 and then data 3934 02:31:16,640 --> 02:31:20,479 so this is my basic creation of node now 3935 02:31:18,960 --> 02:31:22,479 what i will do 3936 02:31:20,479 --> 02:31:25,200 i want to 3937 02:31:22,479 --> 02:31:28,560 create a link 3938 02:31:25,200 --> 02:31:31,040 of this any node with n4 right so this 3939 02:31:28,560 --> 02:31:33,120 is my any node and i have to connect any 3940 02:31:31,040 --> 02:31:34,960 with n4 so as we know that in linked 3941 02:31:33,120 --> 02:31:36,640 list you have to 3942 02:31:34,960 --> 02:31:38,720 go through each node that means you have 3943 02:31:36,640 --> 02:31:40,960 to do the traversal right you can't 3944 02:31:38,720 --> 02:31:44,080 directly jump to any you have to first 3945 02:31:40,960 --> 02:31:46,240 start from nb then n1 n2 n3 n4 and then 3946 02:31:44,080 --> 02:31:48,160 you can go to any you can't directly 3947 02:31:46,240 --> 02:31:50,319 jump so now what will happen we know 3948 02:31:48,160 --> 02:31:52,960 that we have to go from nb to n1 n1 to 3949 02:31:50,319 --> 02:31:54,960 n2 n2 to n3 and n3 to n4 and then we can 3950 02:31:52,960 --> 02:31:57,520 come to n4 to any so we have to develop 3951 02:31:54,960 --> 02:31:59,600 a logic so this is my head my head is 3952 02:31:57,520 --> 02:32:01,600 pointing to nb so i don't want to change 3953 02:31:59,600 --> 02:32:05,520 my head so i'll create a temporary 3954 02:32:01,600 --> 02:32:06,960 variable a is equal to self.head 3955 02:32:05,520 --> 02:32:08,560 now you might have a doubt that why i'm 3956 02:32:06,960 --> 02:32:11,200 creating a temporary variable because i 3957 02:32:08,560 --> 02:32:13,520 will use this temporary variable 3958 02:32:11,200 --> 02:32:15,680 in a while loop so that if i am using a 3959 02:32:13,520 --> 02:32:17,520 while loop then the value of a will be 3960 02:32:15,680 --> 02:32:19,280 changed right so that's why i have 3961 02:32:17,520 --> 02:32:21,520 created this temporary variable so that 3962 02:32:19,280 --> 02:32:24,399 my head is fixed now i'll create a while 3963 02:32:21,520 --> 02:32:27,200 loop i will write here while a dot next 3964 02:32:24,399 --> 02:32:27,200 is not none 3965 02:32:27,359 --> 02:32:30,960 so here i will give a condition while a 3966 02:32:28,960 --> 02:32:33,200 dot next is not none so why i am giving 3967 02:32:30,960 --> 02:32:35,040 this condition because here you can see 3968 02:32:33,200 --> 02:32:37,520 that inside this while loop i will write 3969 02:32:35,040 --> 02:32:39,920 here a is equal to a dot next so what 3970 02:32:37,520 --> 02:32:42,319 will happen here my a is equal to this 3971 02:32:39,920 --> 02:32:44,640 one self dot head initially my a is here 3972 02:32:42,319 --> 02:32:47,040 right and here it will check a dot next 3973 02:32:44,640 --> 02:32:50,160 is not none so this is my a right so a 3974 02:32:47,040 --> 02:32:52,560 dot next is pointing to n1 so is it none 3975 02:32:50,160 --> 02:32:54,640 no it's not done why because a dot next 3976 02:32:52,560 --> 02:32:56,640 will contain the address of n1 3977 02:32:54,640 --> 02:32:58,479 right so here 3978 02:32:56,640 --> 02:33:00,080 it will execute the statement inside 3979 02:32:58,479 --> 02:33:02,560 this while loop and i am having the 3980 02:33:00,080 --> 02:33:03,760 statement a equal to a dot next so my a 3981 02:33:02,560 --> 02:33:04,800 value will be 3982 02:33:03,760 --> 02:33:07,359 here 3983 02:33:04,800 --> 02:33:09,120 so my new a value will be here at n1 3984 02:33:07,359 --> 02:33:11,280 node similarly once again this while 3985 02:33:09,120 --> 02:33:13,760 loop will work and again a dot next so a 3986 02:33:11,280 --> 02:33:16,000 dot next is pointing to n2 so it is none 3987 02:33:13,760 --> 02:33:17,920 no it's not none so once again a is 3988 02:33:16,000 --> 02:33:19,840 equal to a dot next so my a value will 3989 02:33:17,920 --> 02:33:22,080 be coming here similarly once again this 3990 02:33:19,840 --> 02:33:25,520 loop will work and my a value will shift 3991 02:33:22,080 --> 02:33:27,439 here here so up to n4 my a value be here 3992 02:33:25,520 --> 02:33:29,760 now i have to create a link between n4 3993 02:33:27,439 --> 02:33:32,720 and any so how can i create a link i can 3994 02:33:29,760 --> 02:33:34,720 simply write here a dot next 3995 02:33:32,720 --> 02:33:37,120 is equal to any 3996 02:33:34,720 --> 02:33:39,120 so if i'm writing a dot next is equal to 3997 02:33:37,120 --> 02:33:42,160 any so this none will be removed and i 3998 02:33:39,120 --> 02:33:44,479 will create a link from n4 to any node 3999 02:33:42,160 --> 02:33:46,800 so this is my logic here and here this a 4000 02:33:44,479 --> 02:33:48,800 dot next is equal to any is not inside 4001 02:33:46,800 --> 02:33:51,920 this while loop it's it is outside the 4002 02:33:48,800 --> 02:33:54,160 while loop right so indentation is here 4003 02:33:51,920 --> 02:33:55,200 up to a equal to a dot next this is 4004 02:33:54,160 --> 02:33:56,640 outside 4005 02:33:55,200 --> 02:33:58,800 this while loop 4006 02:33:56,640 --> 02:34:00,800 now let's see the coding part 4007 02:33:58,800 --> 02:34:02,960 so i've zoom it so that you can see it 4008 02:34:00,800 --> 02:34:05,120 clearly now after insertion at beginning 4009 02:34:02,960 --> 02:34:07,120 i will create a function 4010 02:34:05,120 --> 02:34:07,840 inside the class node itself i'll write 4011 02:34:07,120 --> 02:34:11,439 here 4012 02:34:07,840 --> 02:34:13,920 def and write here insertion or insert 4013 02:34:11,439 --> 02:34:13,920 at end 4014 02:34:14,240 --> 02:34:17,840 so insert at end 4015 02:34:19,120 --> 02:34:23,439 and once again i will write yourself 4016 02:34:21,439 --> 02:34:25,040 comma data 4017 02:34:23,439 --> 02:34:27,120 so now if i want to insert the element 4018 02:34:25,040 --> 02:34:29,200 at end also then i have to create a node 4019 02:34:27,120 --> 02:34:31,840 so let me write here node any is equal 4020 02:34:29,200 --> 02:34:31,840 to node 4021 02:34:32,319 --> 02:34:37,200 and then i will write here data 4022 02:34:35,280 --> 02:34:39,439 now after creating a node i want to 4023 02:34:37,200 --> 02:34:41,680 traverse because this is my last node so 4024 02:34:39,439 --> 02:34:43,359 if i want to reach to the last node i 4025 02:34:41,680 --> 02:34:44,960 have to go through the each node so 4026 02:34:43,359 --> 02:34:47,200 that's why i will create a temporary 4027 02:34:44,960 --> 02:34:49,439 variable here a is equal to self dot 4028 02:34:47,200 --> 02:34:49,439 head 4029 02:34:49,840 --> 02:34:53,280 so once i'm creating a temporary 4030 02:34:51,280 --> 02:34:54,479 variable i'll create a loop so i'll 4031 02:34:53,280 --> 02:34:59,840 write here while 4032 02:34:54,479 --> 02:34:59,840 so while a dot next is not none 4033 02:35:01,359 --> 02:35:05,680 then a is equal to 4034 02:35:03,280 --> 02:35:08,160 a dot next 4035 02:35:05,680 --> 02:35:10,720 and now after this i will simply assign 4036 02:35:08,160 --> 02:35:12,399 a dot next 4037 02:35:10,720 --> 02:35:13,680 is equal to 4038 02:35:12,399 --> 02:35:15,680 n e 4039 02:35:13,680 --> 02:35:19,200 so this is the connection of my last 4040 02:35:15,680 --> 02:35:21,120 node that is n4 node to any right and 4041 02:35:19,200 --> 02:35:22,240 here i have created this while loop so 4042 02:35:21,120 --> 02:35:24,319 that i can 4043 02:35:22,240 --> 02:35:27,359 go through each node now let me print 4044 02:35:24,319 --> 02:35:30,080 out this so what i will do now 4045 02:35:27,359 --> 02:35:31,280 i will simply write here 4046 02:35:30,080 --> 02:35:33,439 sll 4047 02:35:31,280 --> 02:35:35,760 dot 4048 02:35:33,439 --> 02:35:38,760 insert 4049 02:35:35,760 --> 02:35:38,760 attend 4050 02:35:40,960 --> 02:35:44,960 and 4051 02:35:41,920 --> 02:35:46,399 let's take the value as 25 4052 02:35:44,960 --> 02:35:47,840 attend 4053 02:35:46,399 --> 02:35:51,479 and now i will display so i'll write 4054 02:35:47,840 --> 02:35:51,479 simple here sll.traversal 4055 02:35:53,200 --> 02:35:57,520 so let me do the execution here 4056 02:35:55,680 --> 02:35:59,760 and on execution i'm getting the output 4057 02:35:57,520 --> 02:36:02,720 but it's confusing so i'll write here 4058 02:35:59,760 --> 02:36:02,720 once again print 4059 02:36:03,600 --> 02:36:07,920 so that i can get space 4060 02:36:05,600 --> 02:36:09,680 between the outputs so here you can see 4061 02:36:07,920 --> 02:36:12,319 that i have inserted a node which is 4062 02:36:09,680 --> 02:36:14,319 having a data element as 25 and which is 4063 02:36:12,319 --> 02:36:16,000 inserted at the end so this is the 4064 02:36:14,319 --> 02:36:18,479 execution part now let's understand the 4065 02:36:16,000 --> 02:36:20,319 logic how this thing is working exactly 4066 02:36:18,479 --> 02:36:22,640 so here 4067 02:36:20,319 --> 02:36:25,200 i've given the data as 25 4068 02:36:22,640 --> 02:36:27,280 so data is equal to 25 here 4069 02:36:25,200 --> 02:36:29,120 now 4070 02:36:27,280 --> 02:36:31,920 what will be here any 4071 02:36:29,120 --> 02:36:31,920 if you see here 4072 02:36:32,560 --> 02:36:37,840 then my any 4073 02:36:34,240 --> 02:36:37,840 dot data is equal to 25 4074 02:36:41,040 --> 02:36:44,960 and here my any 4075 02:36:42,880 --> 02:36:47,120 dot next 4076 02:36:44,960 --> 02:36:50,120 is equal to none 4077 02:36:47,120 --> 02:36:50,120 right 4078 02:36:50,800 --> 02:36:55,840 so this is my creation of a node 4079 02:36:53,680 --> 02:36:55,840 now 4080 02:36:56,880 --> 02:36:59,600 after this 4081 02:36:58,080 --> 02:37:01,840 what will happen here so here you can 4082 02:36:59,600 --> 02:37:04,399 see that a is equal to self dot head so 4083 02:37:01,840 --> 02:37:06,160 instead of self sll will pass right so 4084 02:37:04,399 --> 02:37:08,399 sll dot head 4085 02:37:06,160 --> 02:37:10,479 why sll will be passed here because i am 4086 02:37:08,399 --> 02:37:14,640 calling this function 4087 02:37:10,479 --> 02:37:17,280 with my object sll right so sll dot 4088 02:37:14,640 --> 02:37:20,080 insert at end so instead of self sll 4089 02:37:17,280 --> 02:37:20,080 will pass here 4090 02:37:20,960 --> 02:37:25,520 so here sll dot head was my nb 4091 02:37:24,640 --> 02:37:27,520 right 4092 02:37:25,520 --> 02:37:29,280 so it will assign to nb so as we know 4093 02:37:27,520 --> 02:37:31,680 that nb was my first node so head will 4094 02:37:29,280 --> 02:37:33,680 be the pointing to nb itself up to this 4095 02:37:31,680 --> 02:37:35,600 this was the logic now coming to here 4096 02:37:33,680 --> 02:37:38,080 here you can see that a dot next so what 4097 02:37:35,600 --> 02:37:40,640 is a dot next 4098 02:37:38,080 --> 02:37:43,200 so here a dot next is equal to nb dot 4099 02:37:40,640 --> 02:37:45,600 next why because my a value is nb so nb 4100 02:37:43,200 --> 02:37:47,840 dot next is my none no it's not none 4101 02:37:45,600 --> 02:37:50,080 because nb dot next contains the address 4102 02:37:47,840 --> 02:37:52,080 of n1 so that means 4103 02:37:50,080 --> 02:37:55,120 it's not none so once again inside this 4104 02:37:52,080 --> 02:37:55,120 loop what will happen 4105 02:37:56,000 --> 02:38:02,720 my a will assign to a dot next that is 4106 02:37:59,840 --> 02:38:04,479 nb dot next 4107 02:38:02,720 --> 02:38:07,040 now you might have a doubt what is nb 4108 02:38:04,479 --> 02:38:10,080 dot next nb dot is next is nothing but 4109 02:38:07,040 --> 02:38:12,479 n1 right here you can see that 4110 02:38:10,080 --> 02:38:14,880 so nb dot next is equal to n n1 that 4111 02:38:12,479 --> 02:38:16,800 means my nb is connected to n1 similarly 4112 02:38:14,880 --> 02:38:19,920 once again what will happen here this is 4113 02:38:16,800 --> 02:38:20,960 a loop once again it will go here 4114 02:38:19,920 --> 02:38:24,080 so 4115 02:38:20,960 --> 02:38:25,439 if you see here a dot next so what is a 4116 02:38:24,080 --> 02:38:27,520 dot next my 4117 02:38:25,439 --> 02:38:31,200 a is equal to n1 now so i will be 4118 02:38:27,520 --> 02:38:31,200 getting here n1.next 4119 02:38:33,840 --> 02:38:39,520 right so once again a is equal to n1 dot 4120 02:38:36,800 --> 02:38:41,760 next so what is n1.next 4121 02:38:39,520 --> 02:38:42,960 so my a value will be here 4122 02:38:41,760 --> 02:38:45,680 n2 4123 02:38:42,960 --> 02:38:48,240 because n1 dot next is n2 right so once 4124 02:38:45,680 --> 02:38:50,960 again it will go n2 dot next so n2 dot 4125 02:38:48,240 --> 02:38:52,800 next is nothing but n3 so inside this 4126 02:38:50,960 --> 02:38:55,280 once again a value will be assigned is 4127 02:38:52,800 --> 02:38:57,200 n3 then once again n3 dot next will go 4128 02:38:55,280 --> 02:38:58,560 so in this way you can see that we are 4129 02:38:57,200 --> 02:39:00,319 going through each node and we are 4130 02:38:58,560 --> 02:39:02,800 connecting them now what will happen 4131 02:39:00,319 --> 02:39:05,760 here once i reach to n4 right so i have 4132 02:39:02,800 --> 02:39:07,680 to create a link from n4 to any so i 4133 02:39:05,760 --> 02:39:10,080 will write here n4 dot next is equal to 4134 02:39:07,680 --> 02:39:12,240 any right so here you see this is my 4135 02:39:10,080 --> 02:39:14,399 while loop so inside this at last what i 4136 02:39:12,240 --> 02:39:17,359 will be getting here i will getting here 4137 02:39:14,399 --> 02:39:17,359 and four dot next 4138 02:39:23,600 --> 02:39:28,240 because n4 dot next is none right so 4139 02:39:26,080 --> 02:39:30,080 this loop will end 4140 02:39:28,240 --> 02:39:32,399 if you see here 4141 02:39:30,080 --> 02:39:35,280 you can see that n4 dot next is none so 4142 02:39:32,399 --> 02:39:37,680 my loop will end here and 4143 02:39:35,280 --> 02:39:40,800 what is my a dot next value so instead 4144 02:39:37,680 --> 02:39:42,960 of a i was getting n4 so n4 dot next was 4145 02:39:40,800 --> 02:39:44,080 my none but i have assigned here n4 dot 4146 02:39:42,960 --> 02:39:46,640 next 4147 02:39:44,080 --> 02:39:46,640 to any 4148 02:39:49,120 --> 02:39:54,880 right so that means now n4 is connected 4149 02:39:51,280 --> 02:39:57,280 to any now what will be the any dot next 4150 02:39:54,880 --> 02:39:59,120 it's none because this is last node so 4151 02:39:57,280 --> 02:40:01,680 here you can see that while creating the 4152 02:39:59,120 --> 02:40:04,640 node itself i got any dot next is equal 4153 02:40:01,680 --> 02:40:06,800 to none so this is my basic logic of 4154 02:40:04,640 --> 02:40:08,640 insertion at end 4155 02:40:06,800 --> 02:40:10,479 so we have seen the insertion at the 4156 02:40:08,640 --> 02:40:12,240 beginning of the node we have also seen 4157 02:40:10,479 --> 02:40:14,000 the insertion at the end of the node now 4158 02:40:12,240 --> 02:40:15,920 we will see the insertion in between so 4159 02:40:14,000 --> 02:40:18,479 that means we will see the insertion at 4160 02:40:15,920 --> 02:40:21,040 any specified node so initially i was 4161 02:40:18,479 --> 02:40:22,240 having four nodes n1 4162 02:40:21,040 --> 02:40:23,760 n2 4163 02:40:22,240 --> 02:40:25,439 n3 4164 02:40:23,760 --> 02:40:26,640 and n4 4165 02:40:25,439 --> 02:40:27,840 then i've done the insertion at 4166 02:40:26,640 --> 02:40:29,760 beginning 4167 02:40:27,840 --> 02:40:32,080 so this was my node nb and then i've 4168 02:40:29,760 --> 02:40:34,399 done the insertion at end so this was my 4169 02:40:32,080 --> 02:40:38,240 node any and these were the four nodes 4170 02:40:34,399 --> 02:40:39,840 n1 n2 n3 n4 and we were having the data 4171 02:40:38,240 --> 02:40:41,279 right 5 4172 02:40:39,840 --> 02:40:42,479 10 4173 02:40:41,279 --> 02:40:44,000 15 4174 02:40:42,479 --> 02:40:46,880 20 4175 02:40:44,000 --> 02:40:48,399 and 25 for any and for this we were 4176 02:40:46,880 --> 02:40:50,319 having the data s2 4177 02:40:48,399 --> 02:40:51,840 right now what i will do i have to 4178 02:40:50,319 --> 02:40:54,399 insert the node 4179 02:40:51,840 --> 02:40:55,840 at specified position so the process 4180 02:40:54,399 --> 02:40:58,720 will be the same i have to create the 4181 02:40:55,840 --> 02:41:01,040 node first so for creating node what i 4182 02:40:58,720 --> 02:41:02,960 will do here let's suppose this is my nb 4183 02:41:01,040 --> 02:41:06,319 node so initially 4184 02:41:02,960 --> 02:41:08,240 i'll create a node nb is equal to node 4185 02:41:06,319 --> 02:41:10,000 data 4186 02:41:08,240 --> 02:41:11,840 so when i'm creating a node then this 4187 02:41:10,000 --> 02:41:14,880 will contain a data 4188 02:41:11,840 --> 02:41:16,240 and this next will contain none 4189 02:41:14,880 --> 02:41:18,080 so let's suppose that these are the 4190 02:41:16,240 --> 02:41:19,600 positions let me give the position at 4191 02:41:18,080 --> 02:41:20,560 one two 4192 02:41:19,600 --> 02:41:21,520 three 4193 02:41:20,560 --> 02:41:22,399 four 4194 02:41:21,520 --> 02:41:24,640 five 4195 02:41:22,399 --> 02:41:25,920 six so let's suppose that at position 4196 02:41:24,640 --> 02:41:28,240 three 4197 02:41:25,920 --> 02:41:29,600 i want to insert this nb node that means 4198 02:41:28,240 --> 02:41:31,520 in this position 4199 02:41:29,600 --> 02:41:33,680 so initially if you see this linked list 4200 02:41:31,520 --> 02:41:34,560 this linked list is connected like this 4201 02:41:33,680 --> 02:41:36,640 right 4202 02:41:34,560 --> 02:41:41,040 nv is connected to n1 n1 is connected to 4203 02:41:36,640 --> 02:41:42,880 n2 n2 to n3 n3 to n4 and 4 to ne and any 4204 02:41:41,040 --> 02:41:45,439 is connected to none right because we 4205 02:41:42,880 --> 02:41:47,680 don't have any note after any and 4206 02:41:45,439 --> 02:41:49,600 initially my head will be pointing to my 4207 02:41:47,680 --> 02:41:51,279 first node that is nb 4208 02:41:49,600 --> 02:41:52,800 now what will happen here 4209 02:41:51,279 --> 02:41:55,520 so let's say at position 3 i want to 4210 02:41:52,800 --> 02:41:57,760 insert my node so what will happen here 4211 02:41:55,520 --> 02:42:00,560 so this will be my position 3 and after 4212 02:41:57,760 --> 02:42:01,439 that this will be my position 4 5 6 7 4213 02:42:00,560 --> 02:42:03,040 right 4214 02:42:01,439 --> 02:42:04,880 so what will happen here as we know that 4215 02:42:03,040 --> 02:42:06,960 our head will be always pointing to the 4216 02:42:04,880 --> 02:42:09,680 first note so i will create a temporary 4217 02:42:06,960 --> 02:42:11,359 variable a is equal to self.head 4218 02:42:09,680 --> 02:42:13,040 now you might have a question that why i 4219 02:42:11,359 --> 02:42:14,720 am creating a temporary variable a 4220 02:42:13,040 --> 02:42:16,800 because i will be using this temporary 4221 02:42:14,720 --> 02:42:18,240 variable in a loop so in a loop i will 4222 02:42:16,800 --> 02:42:20,000 do the iteration so a value will be 4223 02:42:18,240 --> 02:42:22,319 changed but i don't want to change my a 4224 02:42:20,000 --> 02:42:24,000 value right and a value is self dot head 4225 02:42:22,319 --> 02:42:25,520 because head will be fixed so that's why 4226 02:42:24,000 --> 02:42:27,600 i have created a temporary variable here 4227 02:42:25,520 --> 02:42:29,840 a is equal to self.head now what i will 4228 02:42:27,600 --> 02:42:30,800 do i will create a loop which look for 4229 02:42:29,840 --> 02:42:32,160 loop 4230 02:42:30,800 --> 02:42:34,640 so let's suppose that if you want to 4231 02:42:32,160 --> 02:42:36,960 insert this nb node at position 3 then 4232 02:42:34,640 --> 02:42:38,880 in a linked list you have to go through 4233 02:42:36,960 --> 02:42:41,359 each node right so first you have to go 4234 02:42:38,880 --> 02:42:42,880 to position one position two then only 4235 02:42:41,359 --> 02:42:44,560 you can come to position three so that's 4236 02:42:42,880 --> 02:42:47,520 why i'm using here for loop so i'll 4237 02:42:44,560 --> 02:42:49,520 write here for i in range 4238 02:42:47,520 --> 02:42:52,319 and then i will give the value here one 4239 02:42:49,520 --> 02:42:52,319 and position 4240 02:42:52,880 --> 02:42:56,000 then i will give the value here one and 4241 02:42:54,240 --> 02:42:57,680 position minus one now you might have a 4242 02:42:56,000 --> 02:43:00,319 diode why i am giving here position 4243 02:42:57,680 --> 02:43:02,080 minus one so see this is my node right 4244 02:43:00,319 --> 02:43:04,240 one two three 4245 02:43:02,080 --> 02:43:06,479 i want to insert this node at third 4246 02:43:04,240 --> 02:43:07,600 position so i have to traverse here from 4247 02:43:06,479 --> 02:43:10,000 one 4248 02:43:07,600 --> 02:43:11,359 and then two then only i can go to three 4249 02:43:10,000 --> 02:43:13,120 that's why i have written here position 4250 02:43:11,359 --> 02:43:15,439 minus one so let me correct here it will 4251 02:43:13,120 --> 02:43:17,040 be position minus one 4252 02:43:15,439 --> 02:43:19,359 so here you can see that my position is 4253 02:43:17,040 --> 02:43:22,640 three so i'll write here for 4254 02:43:19,359 --> 02:43:24,479 i in range 4255 02:43:22,640 --> 02:43:26,399 range starts from 1 4256 02:43:24,479 --> 02:43:28,479 and what is position minus 1 3 minus 1 4257 02:43:26,399 --> 02:43:30,560 will be 2 so as i know that if i'm 4258 02:43:28,479 --> 02:43:32,560 writing here for i in range 1 comma 2 4259 02:43:30,560 --> 02:43:34,960 then this 2 will be excluded so that 4260 02:43:32,560 --> 02:43:37,359 means for i is equal to 1 my iteration 4261 02:43:34,960 --> 02:43:39,760 will be done right so here what do you 4262 02:43:37,359 --> 02:43:41,680 mean by a equal to a dot next my a is 4263 02:43:39,760 --> 02:43:43,840 equal to self dot head so if i am doing 4264 02:43:41,680 --> 02:43:46,000 here a dot next that means my temporary 4265 02:43:43,840 --> 02:43:48,479 variable a is now shifting to here and 4266 02:43:46,000 --> 02:43:50,720 one node right a dot next is nothing but 4267 02:43:48,479 --> 02:43:53,840 it will contain the address of n1 node 4268 02:43:50,720 --> 02:43:56,560 so this will be my new a so you can see 4269 02:43:53,840 --> 02:43:59,840 that through iteration the a value was 4270 02:43:56,560 --> 02:44:01,920 moving from nb to n1 right and after n1 4271 02:43:59,840 --> 02:44:04,319 i want to connect this nb node right so 4272 02:44:01,920 --> 02:44:07,600 after this loop what i will do this is 4273 02:44:04,319 --> 02:44:09,760 my nb so initially my nb dot next is 4274 02:44:07,600 --> 02:44:11,600 none right 4275 02:44:09,760 --> 02:44:13,359 when you will create a node then we will 4276 02:44:11,600 --> 02:44:15,279 assign the data right nb dot data is 4277 02:44:13,359 --> 02:44:17,439 equal to data and nb dot next is equal 4278 02:44:15,279 --> 02:44:19,520 to none so i want to connect this nb dot 4279 02:44:17,439 --> 02:44:21,200 next with this n2 so i have to write a 4280 02:44:19,520 --> 02:44:22,560 logic so what i will write here i'll 4281 02:44:21,200 --> 02:44:24,399 write here nb 4282 02:44:22,560 --> 02:44:28,000 dot next 4283 02:44:24,399 --> 02:44:30,000 is equal to i will write here a dot next 4284 02:44:28,000 --> 02:44:32,080 because my a value is assigning to n1 4285 02:44:30,000 --> 02:44:34,000 right so if i'm writing a dot next then 4286 02:44:32,080 --> 02:44:35,680 a dot next will contain the address of 4287 02:44:34,000 --> 02:44:38,000 n2 right so that's why i have written 4288 02:44:35,680 --> 02:44:40,319 here nb dot next is equal to a dot next 4289 02:44:38,000 --> 02:44:42,560 so that means nb dot next will contain 4290 02:44:40,319 --> 02:44:43,600 the address of n2 4291 02:44:42,560 --> 02:44:45,840 right 4292 02:44:43,600 --> 02:44:48,000 and after that i have to link this node 4293 02:44:45,840 --> 02:44:50,080 also 4294 02:44:48,000 --> 02:44:53,279 so for linking this node i will write 4295 02:44:50,080 --> 02:44:55,200 here a dot next 4296 02:44:53,279 --> 02:44:56,560 is equal to what's the name of this node 4297 02:44:55,200 --> 02:44:58,720 nb 4298 02:44:56,560 --> 02:45:01,359 so if you see here what will happen here 4299 02:44:58,720 --> 02:45:03,040 let me remove this 4300 02:45:01,359 --> 02:45:04,960 so this connection 4301 02:45:03,040 --> 02:45:07,279 so when i'm writing here nb dot next is 4302 02:45:04,960 --> 02:45:10,479 equal to a dot next so this is my nb dot 4303 02:45:07,279 --> 02:45:12,800 next so nb dot next will be connecting 4304 02:45:10,479 --> 02:45:13,680 to this n2 4305 02:45:12,800 --> 02:45:15,600 right 4306 02:45:13,680 --> 02:45:18,720 and here i've written a dot next is 4307 02:45:15,600 --> 02:45:20,240 equal to nb so a dot next will connect 4308 02:45:18,720 --> 02:45:22,319 here 4309 02:45:20,240 --> 02:45:24,720 right so in this way we can insert a 4310 02:45:22,319 --> 02:45:26,960 node at any specified position now let's 4311 02:45:24,720 --> 02:45:28,720 see the coding example so that we can 4312 02:45:26,960 --> 02:45:30,640 understand in a better way 4313 02:45:28,720 --> 02:45:32,000 yeah so this was our program and here we 4314 02:45:30,640 --> 02:45:33,920 have already done the insertion at 4315 02:45:32,000 --> 02:45:36,479 beginning and insertion at end so once 4316 02:45:33,920 --> 02:45:38,319 again i will create a function and for 4317 02:45:36,479 --> 02:45:39,840 creating a function i will write def and 4318 02:45:38,319 --> 02:45:40,840 then function name let's suppose i will 4319 02:45:39,840 --> 02:45:44,240 write here 4320 02:45:40,840 --> 02:45:46,000 insert at specified node 4321 02:45:44,240 --> 02:45:49,359 i'm writing this large name so that you 4322 02:45:46,000 --> 02:45:49,359 can understand easily 4323 02:45:50,319 --> 02:45:53,359 or else you can also give the sorter 4324 02:45:52,080 --> 02:45:55,520 name too 4325 02:45:53,359 --> 02:45:57,120 so insert add specified node so first 4326 02:45:55,520 --> 02:45:58,720 alright yourself and what are the 4327 02:45:57,120 --> 02:46:00,880 parameter i have to pass here i have to 4328 02:45:58,720 --> 02:46:03,600 give the position for this 4329 02:46:00,880 --> 02:46:03,600 and then data 4330 02:46:04,560 --> 02:46:08,640 so if i want to insert a node at any 4331 02:46:06,880 --> 02:46:10,240 specified position then again i have to 4332 02:46:08,640 --> 02:46:11,840 create the nodes so i will create here 4333 02:46:10,240 --> 02:46:14,000 nib 4334 02:46:11,840 --> 02:46:15,600 so node in between i've given the name 4335 02:46:14,000 --> 02:46:17,359 an ib shortcut 4336 02:46:15,600 --> 02:46:19,520 so i'll write here 4337 02:46:17,359 --> 02:46:21,279 node 4338 02:46:19,520 --> 02:46:23,200 and then data 4339 02:46:21,279 --> 02:46:26,399 so i've created my node in which 4340 02:46:23,200 --> 02:46:29,120 node.data is equal to data and then node 4341 02:46:26,399 --> 02:46:30,640 dot next is equal to none so i will give 4342 02:46:29,120 --> 02:46:33,040 the data value then data value will be 4343 02:46:30,640 --> 02:46:34,640 assigned here 4344 02:46:33,040 --> 02:46:36,319 so what will happen here after that once 4345 02:46:34,640 --> 02:46:39,840 again i will create a temporary variable 4346 02:46:36,319 --> 02:46:42,080 a is equal to self dot head 4347 02:46:39,840 --> 02:46:44,399 and then i will create a loop for i in 4348 02:46:42,080 --> 02:46:44,399 range 4349 02:46:45,520 --> 02:46:50,680 one comma 4350 02:46:46,880 --> 02:46:50,680 position minus one 4351 02:46:52,080 --> 02:46:55,760 and after this 4352 02:46:53,520 --> 02:46:56,800 i will write here a is equal to a dot 4353 02:46:55,760 --> 02:46:58,399 next 4354 02:46:56,800 --> 02:47:00,720 this is a logic for traversing through 4355 02:46:58,399 --> 02:47:03,279 each node so if i'm giving here position 4356 02:47:00,720 --> 02:47:04,880 3 then i will go through the n1 node 4357 02:47:03,279 --> 02:47:06,720 then n2 node then only i can reach to 4358 02:47:04,880 --> 02:47:07,920 the n3 node right 4359 02:47:06,720 --> 02:47:09,840 so 4360 02:47:07,920 --> 02:47:11,439 after traversing from the n1 node n2 4361 02:47:09,840 --> 02:47:13,279 node i'm having n3 node so i have to 4362 02:47:11,439 --> 02:47:15,279 connect it right so how to connect that 4363 02:47:13,279 --> 02:47:16,800 n3 node so for connection i will write 4364 02:47:15,279 --> 02:47:17,760 here niv dot 4365 02:47:16,800 --> 02:47:19,439 next 4366 02:47:17,760 --> 02:47:22,000 is equal to 4367 02:47:19,439 --> 02:47:24,160 a dot next 4368 02:47:22,000 --> 02:47:26,720 so that means at position three five 4369 02:47:24,160 --> 02:47:28,399 created an ib node so nb node was there 4370 02:47:26,720 --> 02:47:30,080 and one node was there and ib node is 4371 02:47:28,399 --> 02:47:33,040 there so that means i am connecting an 4372 02:47:30,080 --> 02:47:35,200 ib dot next is equal to n2 4373 02:47:33,040 --> 02:47:36,800 right now 4374 02:47:35,200 --> 02:47:38,479 what i will do i have to connect an ib 4375 02:47:36,800 --> 02:47:41,680 node with n1 also so i'll write here 4376 02:47:38,479 --> 02:47:41,680 logic a dot next 4377 02:47:42,319 --> 02:47:47,120 is equal to nib 4378 02:47:45,200 --> 02:47:48,720 so a dot next is what my ember node 4379 02:47:47,120 --> 02:47:50,880 which will contain the address of niv 4380 02:47:48,720 --> 02:47:54,240 node so this is my logic now if i want 4381 02:47:50,880 --> 02:47:54,240 to call this function 4382 02:47:54,800 --> 02:47:59,920 let me again 4383 02:47:57,600 --> 02:48:03,200 copy and paste this 4384 02:47:59,920 --> 02:48:05,520 was the name of the function insert at 4385 02:48:03,200 --> 02:48:08,560 insert at specified node 4386 02:48:05,520 --> 02:48:10,880 so i will write here insert at specified 4387 02:48:08,560 --> 02:48:10,880 node 4388 02:48:14,960 --> 02:48:19,040 and then i have given here position and 4389 02:48:17,279 --> 02:48:21,200 then data so i will here pass the 4390 02:48:19,040 --> 02:48:22,880 position at three position 4391 02:48:21,200 --> 02:48:25,680 i want to insert a node 4392 02:48:22,880 --> 02:48:27,279 and let's say data is seven 4393 02:48:25,680 --> 02:48:29,279 and once again i am calling it through 4394 02:48:27,279 --> 02:48:31,439 sll dot reversal 4395 02:48:29,279 --> 02:48:33,760 so if i'm executing it then you can see 4396 02:48:31,439 --> 02:48:35,279 that i am getting the output so for 4397 02:48:33,760 --> 02:48:38,399 getting the output in the next line let 4398 02:48:35,279 --> 02:48:38,399 me write here print 4399 02:48:41,279 --> 02:48:45,040 so you can clearly see in the output 4400 02:48:42,800 --> 02:48:47,680 that at position 3 a new node has been 4401 02:48:45,040 --> 02:48:49,279 inserted whose data is 7 so now let's 4402 02:48:47,680 --> 02:48:51,520 understand the logic 4403 02:48:49,279 --> 02:48:54,880 so the logic is simple here here my 4404 02:48:51,520 --> 02:48:54,880 position was 3 right 4405 02:48:54,960 --> 02:48:59,760 so let me write here position 3 and my 4406 02:48:56,880 --> 02:48:59,760 data was seven 4407 02:49:00,240 --> 02:49:04,160 so when i'm creating here nib is equal 4408 02:49:01,920 --> 02:49:05,439 to node data so what will happen here 4409 02:49:04,160 --> 02:49:06,560 once again 4410 02:49:05,439 --> 02:49:08,399 here 4411 02:49:06,560 --> 02:49:11,040 and ib 4412 02:49:08,399 --> 02:49:11,040 dot data 4413 02:49:11,520 --> 02:49:14,640 will be equal to 7 4414 02:49:16,960 --> 02:49:19,520 and here 4415 02:49:20,640 --> 02:49:26,520 and ib dot next 4416 02:49:22,960 --> 02:49:26,520 is equal to none 4417 02:49:26,640 --> 02:49:31,920 so after creating the node 4418 02:49:29,040 --> 02:49:33,520 here a is equal to self dot head 4419 02:49:31,920 --> 02:49:35,439 so a is equal to self dot head so 4420 02:49:33,520 --> 02:49:36,319 instead of self my sll will be there 4421 02:49:35,439 --> 02:49:38,160 right 4422 02:49:36,319 --> 02:49:42,399 here you can see that i am passing sll 4423 02:49:38,160 --> 02:49:42,399 so instead of self sll will be passed 4424 02:49:42,479 --> 02:49:47,040 so sll.head is what sll.head is nothing 4425 02:49:45,520 --> 02:49:50,080 but is nb 4426 02:49:47,040 --> 02:49:51,920 so sll dot head is equal to nb 4427 02:49:50,080 --> 02:49:53,760 so my head is pointing to the first node 4428 02:49:51,920 --> 02:49:55,760 that is nb 4429 02:49:53,760 --> 02:49:57,920 now coming to the loop logic here you 4430 02:49:55,760 --> 02:50:01,200 can see that position is 3 so 3 minus 1 4431 02:49:57,920 --> 02:50:03,840 will be 2 so if i in range 1 comma 2 is 4432 02:50:01,200 --> 02:50:05,279 there then for i is equal to 1 only this 4433 02:50:03,840 --> 02:50:07,600 loop will run 4434 02:50:05,279 --> 02:50:10,880 so i is equal to 1 here 4435 02:50:07,600 --> 02:50:10,880 what will be the a dot next 4436 02:50:11,920 --> 02:50:18,240 a is equal to a dot next so what is my a 4437 02:50:14,160 --> 02:50:18,240 here a is nothing but nb so nb dot next 4438 02:50:18,640 --> 02:50:25,680 right so nb dot next is what 4439 02:50:21,520 --> 02:50:25,680 nb dot next is nothing but its n1 4440 02:50:26,800 --> 02:50:31,120 so here if i am writing here 4441 02:50:29,680 --> 02:50:33,520 nib 4442 02:50:31,120 --> 02:50:36,240 dot next 4443 02:50:33,520 --> 02:50:40,319 is equal to a dot next so what is my a 4444 02:50:36,240 --> 02:50:42,640 dot next a dot next is n1 dot next 4445 02:50:40,319 --> 02:50:44,880 so you can clearly see that an ib dot 4446 02:50:42,640 --> 02:50:48,840 next is n1 dot next and what is n1 dot 4447 02:50:44,880 --> 02:50:51,200 next n1 dot next is nothing but n2 4448 02:50:48,840 --> 02:50:53,520 right so you can see that i have created 4449 02:50:51,200 --> 02:50:56,080 the node and initially and an ib dot 4450 02:50:53,520 --> 02:50:59,040 next was none but here 4451 02:50:56,080 --> 02:51:00,800 an ib dot next is n2 so that means the 4452 02:50:59,040 --> 02:51:02,960 node i've created at a specified 4453 02:51:00,800 --> 02:51:05,840 position i've connected to the n2 node 4454 02:51:02,960 --> 02:51:05,840 now after this 4455 02:51:06,640 --> 02:51:12,319 once again a dot next 4456 02:51:09,040 --> 02:51:16,000 so what will be a dot next 4457 02:51:12,319 --> 02:51:20,000 a dot next is equal to nib 4458 02:51:16,000 --> 02:51:22,640 and what was my a was my n1 right so n1 4459 02:51:20,000 --> 02:51:25,040 dot next will be an ib 4460 02:51:22,640 --> 02:51:27,680 it's simple so what is my a dot next a 4461 02:51:25,040 --> 02:51:29,439 dot next is n1 dot next and now here you 4462 02:51:27,680 --> 02:51:31,760 can see that i am assigning an ib into a 4463 02:51:29,439 --> 02:51:34,080 dot next so here n1 dot next is equal to 4464 02:51:31,760 --> 02:51:36,240 nib that means an ib is assigned to 4465 02:51:34,080 --> 02:51:38,000 n1.net so here you can see that with 4466 02:51:36,240 --> 02:51:39,359 these two logic the node i have created 4467 02:51:38,000 --> 02:51:41,520 that is an ib node i have done the 4468 02:51:39,359 --> 02:51:42,880 connection with n1 and n2 so this is the 4469 02:51:41,520 --> 02:51:45,760 basic program 4470 02:51:42,880 --> 02:51:47,040 of insertion in single linked list 4471 02:51:45,760 --> 02:51:48,640 so now after seeing the insertion 4472 02:51:47,040 --> 02:51:50,720 operation now let's jump into the 4473 02:51:48,640 --> 02:51:52,240 deletion operation so even deletion 4474 02:51:50,720 --> 02:51:53,920 operation will be of three types we will 4475 02:51:52,240 --> 02:51:56,960 see the deletion at beginning deletion 4476 02:51:53,920 --> 02:51:58,880 at end and we can also delete a 4477 02:51:56,960 --> 02:52:00,960 particular node at any specified 4478 02:51:58,880 --> 02:52:03,520 position so now let's see deletion at 4479 02:52:00,960 --> 02:52:05,840 beginning so 4480 02:52:03,520 --> 02:52:07,040 i was having a node here 4481 02:52:05,840 --> 02:52:09,600 nb 4482 02:52:07,040 --> 02:52:13,600 and then i was having n1 and at position 4483 02:52:09,600 --> 02:52:16,560 3 i've inserted a new node right nib 4484 02:52:13,600 --> 02:52:17,600 then i was having a n2 4485 02:52:16,560 --> 02:52:19,200 n3 4486 02:52:17,600 --> 02:52:21,600 and n4 4487 02:52:19,200 --> 02:52:23,200 and at last i was having any node 4488 02:52:21,600 --> 02:52:25,359 so if you see now i'm having a total 4489 02:52:23,200 --> 02:52:28,880 seven node one two three four five six 4490 02:52:25,359 --> 02:52:30,960 seven right so now nb is connected to n1 4491 02:52:28,880 --> 02:52:32,880 n1 is connected to nib so what does that 4492 02:52:30,960 --> 02:52:34,240 mean that nv is connected to n1 so that 4493 02:52:32,880 --> 02:52:36,880 means the link or you can say the 4494 02:52:34,240 --> 02:52:39,600 reference of nb will contain the address 4495 02:52:36,880 --> 02:52:42,479 of n1 similarly nib is connected to n2 4496 02:52:39,600 --> 02:52:44,319 n2 to n3 and 32 n4 and 4 to n e so the 4497 02:52:42,479 --> 02:52:46,240 link of any will be none because it is 4498 02:52:44,319 --> 02:52:49,680 not connected to any other node so 4499 02:52:46,240 --> 02:52:51,040 initially my head will be pointing to 4500 02:52:49,680 --> 02:52:53,040 right 4501 02:52:51,040 --> 02:52:54,960 and let me write the data here so nb i 4502 02:52:53,040 --> 02:52:58,560 was having a data 2 and when i was 4503 02:52:54,960 --> 02:53:01,200 having five and i be seven end to 10 4504 02:52:58,560 --> 02:53:02,800 15 20 and 25 4505 02:53:01,200 --> 02:53:04,000 now in deletion what i have to do i have 4506 02:53:02,800 --> 02:53:06,160 to delete the first node at the 4507 02:53:04,000 --> 02:53:08,000 beginning so what will be the logic if i 4508 02:53:06,160 --> 02:53:10,399 want to delete this node then my head 4509 02:53:08,000 --> 02:53:12,479 will shift here right so let me create 4510 02:53:10,399 --> 02:53:14,240 the logic so initially what i will do 4511 02:53:12,479 --> 02:53:15,840 i'll write here a is equal to self dot 4512 02:53:14,240 --> 02:53:17,840 head 4513 02:53:15,840 --> 02:53:19,439 so i have created a temporary variable a 4514 02:53:17,840 --> 02:53:21,600 and inside that i have assigned self dot 4515 02:53:19,439 --> 02:53:23,200 head now after creating a temporary 4516 02:53:21,600 --> 02:53:25,200 variable a is equal to self dot head 4517 02:53:23,200 --> 02:53:27,600 what will happen here 4518 02:53:25,200 --> 02:53:30,080 since i will delete this node so i want 4519 02:53:27,600 --> 02:53:32,479 that my head must be pointing to n1 4520 02:53:30,080 --> 02:53:35,040 right so what will the logic so i'll 4521 02:53:32,479 --> 02:53:37,439 write here self.head and i will assign 4522 02:53:35,040 --> 02:53:39,600 here a dot next 4523 02:53:37,439 --> 02:53:41,760 so my a is equal to self dot head right 4524 02:53:39,600 --> 02:53:43,920 this is my a initially so a dot next 4525 02:53:41,760 --> 02:53:45,920 will be n1 right so i've written here 4526 02:53:43,920 --> 02:53:48,479 self.head is equal to a dot next that 4527 02:53:45,920 --> 02:53:51,200 means self.head is equal to n1 so now my 4528 02:53:48,479 --> 02:53:54,080 head will be shift from nb to n1 so let 4529 02:53:51,200 --> 02:53:55,920 me remove this head 4530 02:53:54,080 --> 02:53:57,840 so my head will be here 4531 02:53:55,920 --> 02:54:00,479 so this is my head now 4532 02:53:57,840 --> 02:54:02,399 since i want to delete this nb node so 4533 02:54:00,479 --> 02:54:04,319 since i have to delete this nv node so i 4534 02:54:02,399 --> 02:54:06,319 want to disconnect this link from nb to 4535 02:54:04,319 --> 02:54:08,160 n1 so how to disconnect this link so 4536 02:54:06,319 --> 02:54:11,279 what i will write here i will write here 4537 02:54:08,160 --> 02:54:13,040 a dot next is equal to none 4538 02:54:11,279 --> 02:54:15,840 so this is my a so if i am writing here 4539 02:54:13,040 --> 02:54:18,399 a dot next is equal to none that means 4540 02:54:15,840 --> 02:54:20,640 that nb is not connected to n1 right so 4541 02:54:18,399 --> 02:54:22,640 let me remove this link 4542 02:54:20,640 --> 02:54:24,560 so i will remove this link so once this 4543 02:54:22,640 --> 02:54:26,319 link has been removed that means this 4544 02:54:24,560 --> 02:54:28,479 node has been deleted so this is the 4545 02:54:26,319 --> 02:54:30,080 basic simple program 4546 02:54:28,479 --> 02:54:31,439 for the deletion at the beginning 4547 02:54:30,080 --> 02:54:33,600 initially you have to create a temporary 4548 02:54:31,439 --> 02:54:35,760 variable in which you are assigning self 4549 02:54:33,600 --> 02:54:37,200 dot head now i will change myself dot 4550 02:54:35,760 --> 02:54:39,359 head so i'll write here self dot head is 4551 02:54:37,200 --> 02:54:40,800 equal to a dot next and then i have to 4552 02:54:39,359 --> 02:54:42,800 disconnect the link so for that i will 4553 02:54:40,800 --> 02:54:46,800 write a dot next is equal to none that 4554 02:54:42,800 --> 02:54:48,000 means nb dot next is none so a dot next 4555 02:54:46,800 --> 02:54:50,399 is equal to none means it doesn't 4556 02:54:48,000 --> 02:54:52,960 contain the address of the next node 4557 02:54:50,399 --> 02:54:54,720 so now let's see the coding example 4558 02:54:52,960 --> 02:54:56,720 so this was my coding part so i will 4559 02:54:54,720 --> 02:54:58,479 create a function so 4560 02:54:56,720 --> 02:55:02,920 i'll write here def and let me write the 4561 02:54:58,479 --> 02:55:02,920 function name deletion at the beginning 4562 02:55:03,200 --> 02:55:05,840 deletion 4563 02:55:04,640 --> 02:55:08,319 at 4564 02:55:05,840 --> 02:55:08,319 beginning 4565 02:55:09,840 --> 02:55:14,399 and inside that i will write yourself 4566 02:55:12,880 --> 02:55:15,680 so what will be the logic so first i 4567 02:55:14,399 --> 02:55:18,720 will create a temporary variable i will 4568 02:55:15,680 --> 02:55:20,640 write here a is equal to self dot head 4569 02:55:18,720 --> 02:55:23,120 as we know that we gonna delete the 4570 02:55:20,640 --> 02:55:26,319 first node so my head will be changed so 4571 02:55:23,120 --> 02:55:26,319 i'll write here self dot head 4572 02:55:26,560 --> 02:55:29,200 is equal to 4573 02:55:29,520 --> 02:55:32,319 a dot next 4574 02:55:33,120 --> 02:55:35,840 so that means now my head will be 4575 02:55:34,560 --> 02:55:37,520 changed now after that i have to 4576 02:55:35,840 --> 02:55:39,600 disconnect the link so for that i will 4577 02:55:37,520 --> 02:55:43,160 write here a dot next 4578 02:55:39,600 --> 02:55:43,160 is equal to none 4579 02:55:44,319 --> 02:55:48,479 and let me call this function 4580 02:55:46,880 --> 02:55:51,120 so i will copy this thing 4581 02:55:48,479 --> 02:55:51,120 ctrl c 4582 02:55:52,319 --> 02:55:54,880 and let me 4583 02:55:55,760 --> 02:56:01,600 ctrl b 4584 02:55:57,600 --> 02:56:01,600 and then i will also copy this 4585 02:56:04,240 --> 02:56:07,760 so deletion at beginning is the function 4586 02:56:06,000 --> 02:56:09,200 name so how to call a function i will 4587 02:56:07,760 --> 02:56:10,880 write here sl 4588 02:56:09,200 --> 02:56:12,479 so this is the object of a class 4589 02:56:10,880 --> 02:56:15,200 sll.deletion 4590 02:56:12,479 --> 02:56:15,200 at the beginning 4591 02:56:15,840 --> 02:56:19,439 and now if i am running it 4592 02:56:18,160 --> 02:56:21,840 then you can see that i am getting the 4593 02:56:19,439 --> 02:56:23,040 output but let me right here once again 4594 02:56:21,840 --> 02:56:25,840 print 4595 02:56:23,040 --> 02:56:28,240 so that i can see 4596 02:56:25,840 --> 02:56:29,920 the output in the next line 4597 02:56:28,240 --> 02:56:33,439 so now you can see that the new singly 4598 02:56:29,920 --> 02:56:36,319 linked list is from 5 7 10 15 20 25 so 4599 02:56:33,439 --> 02:56:38,160 that means my nb note that i was created 4600 02:56:36,319 --> 02:56:40,319 has been deleted right so this is the 4601 02:56:38,160 --> 02:56:41,439 basic logic for deletion at the 4602 02:56:40,319 --> 02:56:42,960 beginning 4603 02:56:41,439 --> 02:56:44,720 so we have seen the deletion at the 4604 02:56:42,960 --> 02:56:46,560 beginning now we will see the deletion 4605 02:56:44,720 --> 02:56:48,800 at end so 4606 02:56:46,560 --> 02:56:50,560 let me first create my linked list so we 4607 02:56:48,800 --> 02:56:52,640 have done the deletion at beginning so 4608 02:56:50,560 --> 02:56:55,359 now my new single english will start 4609 02:56:52,640 --> 02:56:58,000 from n1 so my single english will start 4610 02:56:55,359 --> 02:57:01,439 from n1 then nib 4611 02:56:58,000 --> 02:57:02,960 then i was having n2 4612 02:57:01,439 --> 02:57:05,200 then n3 4613 02:57:02,960 --> 02:57:08,800 n4 4614 02:57:05,200 --> 02:57:10,800 and then at last i was having any so n1 4615 02:57:08,800 --> 02:57:11,840 i was having the value as 4616 02:57:10,800 --> 02:57:14,560 5 4617 02:57:11,840 --> 02:57:15,840 and ib i was having the value as 7 4618 02:57:14,560 --> 02:57:17,920 here 10 4619 02:57:15,840 --> 02:57:20,240 15 4620 02:57:17,920 --> 02:57:21,439 20 and then 25 4621 02:57:20,240 --> 02:57:24,960 and 4622 02:57:21,439 --> 02:57:27,760 any dot next was none so let me connect 4623 02:57:24,960 --> 02:57:27,760 all these nodes 4624 02:57:27,920 --> 02:57:33,439 so now as we know that head will be 4625 02:57:29,920 --> 02:57:33,439 always pointing to this n1 4626 02:57:33,840 --> 02:57:37,200 so this is my singly linked list now 4627 02:57:35,439 --> 02:57:39,279 what will happen here i want to delete 4628 02:57:37,200 --> 02:57:41,040 this node so how to delete this node so 4629 02:57:39,279 --> 02:57:43,279 in a linked list if you want to delete 4630 02:57:41,040 --> 02:57:45,040 any node then you have to go through 4631 02:57:43,279 --> 02:57:46,640 each node you can't directly jump to the 4632 02:57:45,040 --> 02:57:50,720 last node so once again i have to go 4633 02:57:46,640 --> 02:57:52,479 from n1 and ib n2 n3 n4 and at last any 4634 02:57:50,720 --> 02:57:54,000 then only i can delete it so now let's 4635 02:57:52,479 --> 02:57:56,080 see the logic here 4636 02:57:54,000 --> 02:57:57,760 so now coming to the logic here 4637 02:57:56,080 --> 02:57:59,680 you can see that i have created here two 4638 02:57:57,760 --> 02:58:01,600 variable now you might have a doubt that 4639 02:57:59,680 --> 02:58:03,920 why i have created two variable i will 4640 02:58:01,600 --> 02:58:08,000 tell you the logic so here i've written 4641 02:58:03,920 --> 02:58:08,000 here previous is equal to self.head 4642 02:58:08,160 --> 02:58:12,000 so previous is nothing but it is 4643 02:58:09,680 --> 02:58:14,160 assigning to n1 and this is my head so 4644 02:58:12,000 --> 02:58:16,000 i'll write here previous 4645 02:58:14,160 --> 02:58:17,040 and i have created here a is equal to 4646 02:58:16,000 --> 02:58:18,479 self 4647 02:58:17,040 --> 02:58:19,840 dot head 4648 02:58:18,479 --> 02:58:22,240 dot next 4649 02:58:19,840 --> 02:58:24,319 so what is a equal to self.head.next 4650 02:58:22,240 --> 02:58:27,120 self.head is nothing but its previous 4651 02:58:24,319 --> 02:58:29,120 right and previous dot next is an ib so 4652 02:58:27,120 --> 02:58:30,880 this is my a 4653 02:58:29,120 --> 02:58:33,279 now what i will do i will create a loop 4654 02:58:30,880 --> 02:58:35,359 and right here inside this loop while 4655 02:58:33,279 --> 02:58:38,080 a dot next 4656 02:58:35,359 --> 02:58:38,080 is not none 4657 02:58:38,800 --> 02:58:42,080 then i will write here a is equal to a 4658 02:58:40,640 --> 02:58:44,840 dot next 4659 02:58:42,080 --> 02:58:47,600 and previous equal to previous dot 4660 02:58:44,840 --> 02:58:50,720 next now you will see the logic here so 4661 02:58:47,600 --> 02:58:52,479 is a dot next this is my a dot next is 4662 02:58:50,720 --> 02:58:54,240 it none no it's not then a dot next 4663 02:58:52,479 --> 02:58:56,319 contains the address of n2 so what will 4664 02:58:54,240 --> 02:58:58,240 happen here a is equal to a dot next and 4665 02:58:56,319 --> 02:59:00,720 previous equal to previous dot next so 4666 02:58:58,240 --> 02:59:01,840 my new a will be this and then previous 4667 02:59:00,720 --> 02:59:03,439 will be this 4668 02:59:01,840 --> 02:59:05,040 similarly once again 4669 02:59:03,439 --> 02:59:07,359 this is a while loop so once again a dot 4670 02:59:05,040 --> 02:59:09,920 next is not none so it will see oh a dot 4671 02:59:07,359 --> 02:59:11,760 next is not none so once again the a 4672 02:59:09,920 --> 02:59:14,080 will be here and then previous will be 4673 02:59:11,760 --> 02:59:15,840 here once again the loop will run it 4674 02:59:14,080 --> 02:59:17,760 will see a dot next is not none once 4675 02:59:15,840 --> 02:59:19,680 again the a value will be changed here 4676 02:59:17,760 --> 02:59:21,920 and the previous will be here similarly 4677 02:59:19,680 --> 02:59:23,200 it will see here a dot next is not none 4678 02:59:21,920 --> 02:59:25,439 so a will be 4679 02:59:23,200 --> 02:59:28,080 here at any node and here previous will 4680 02:59:25,439 --> 02:59:30,399 be here now it will check a dot next is 4681 02:59:28,080 --> 02:59:31,920 none so this while loop will be end so 4682 02:59:30,399 --> 02:59:33,760 here you can see that 4683 02:59:31,920 --> 02:59:35,680 my a is here at the last node and my 4684 02:59:33,760 --> 02:59:37,920 previous is at the end for node now what 4685 02:59:35,680 --> 02:59:40,160 i will do here i have to delete the last 4686 02:59:37,920 --> 02:59:42,319 node so i have to disconnect this link 4687 02:59:40,160 --> 02:59:44,399 right so this is my previous so if i'm 4688 02:59:42,319 --> 02:59:47,359 writing here previous 4689 02:59:44,399 --> 02:59:49,680 dot next is equal to none 4690 02:59:47,359 --> 02:59:51,120 so my link has been disconnected and 4691 02:59:49,680 --> 02:59:53,439 hence this is a deletion of the last 4692 02:59:51,120 --> 02:59:55,200 node and that's what we want because if 4693 02:59:53,439 --> 02:59:57,600 you see here any dot next is already 4694 02:59:55,200 --> 02:59:59,279 none and if i disconnect this link then 4695 02:59:57,600 --> 03:00:01,279 i've already deleted this note so this 4696 02:59:59,279 --> 03:00:02,880 is the basic logic so here i've done 4697 03:00:01,279 --> 03:00:05,279 nothing but i have just created a two 4698 03:00:02,880 --> 03:00:07,840 variable and then i have used the while 4699 03:00:05,279 --> 03:00:09,600 loop so this is the basic logic of the 4700 03:00:07,840 --> 03:00:11,359 deletion at the end position now let's 4701 03:00:09,600 --> 03:00:13,120 see the coding part of it 4702 03:00:11,359 --> 03:00:14,560 now what i will do here i will once 4703 03:00:13,120 --> 03:00:18,319 again create a function so i'll write 4704 03:00:14,560 --> 03:00:21,359 here def and i will write here deletion 4705 03:00:18,319 --> 03:00:21,359 at end 4706 03:00:22,319 --> 03:00:29,680 and i will write yourself 4707 03:00:25,680 --> 03:00:33,359 now initially i will write my previous 4708 03:00:29,680 --> 03:00:33,359 is equal to self.head 4709 03:00:34,399 --> 03:00:39,319 and then i will write here a is equal to 4710 03:00:41,200 --> 03:00:44,000 self dot head 4711 03:00:44,479 --> 03:00:48,080 dot next 4712 03:00:46,560 --> 03:00:50,640 then i will create a while loop so i'll 4713 03:00:48,080 --> 03:00:53,840 write here while 4714 03:00:50,640 --> 03:00:53,840 a dot next 4715 03:00:54,319 --> 03:00:57,040 is not none 4716 03:00:59,359 --> 03:01:05,240 then i will change the value so i will 4717 03:01:00,720 --> 03:01:05,240 write here a is equal to a dot next 4718 03:01:06,560 --> 03:01:12,000 and previous is equal to 4719 03:01:08,960 --> 03:01:12,000 previous dot next 4720 03:01:14,080 --> 03:01:17,680 and once i have done the traversal 4721 03:01:15,840 --> 03:01:20,240 through each node so what i will do here 4722 03:01:17,680 --> 03:01:21,439 i will write here previous 4723 03:01:20,240 --> 03:01:23,760 dot 4724 03:01:21,439 --> 03:01:23,760 next 4725 03:01:24,800 --> 03:01:28,880 is equal to none so if i'm writing here 4726 03:01:26,880 --> 03:01:31,120 previous dot next is equal to none so 4727 03:01:28,880 --> 03:01:33,439 that means the link was disconnected 4728 03:01:31,120 --> 03:01:35,840 from the last node now let me execute 4729 03:01:33,439 --> 03:01:35,840 this 4730 03:01:36,960 --> 03:01:39,200 so 4731 03:01:39,840 --> 03:01:44,479 let me copy and paste 4732 03:01:42,880 --> 03:01:47,760 and i have to call it so i'll write here 4733 03:01:44,479 --> 03:01:49,840 sll dot deletion at end 4734 03:01:47,760 --> 03:01:51,439 so this is the program and not running 4735 03:01:49,840 --> 03:01:52,960 so on executing you can see that i am 4736 03:01:51,439 --> 03:01:54,479 getting the error so why i'm getting 4737 03:01:52,960 --> 03:01:57,520 here because 4738 03:01:54,479 --> 03:01:59,439 i have to write here self 4739 03:01:57,520 --> 03:02:01,040 so now if i'm executing it you can see 4740 03:01:59,439 --> 03:02:04,560 that i'm getting the output 4741 03:02:01,040 --> 03:02:04,560 once again i have to write here print 4742 03:02:06,960 --> 03:02:10,080 so here you can see that the last node 4743 03:02:08,640 --> 03:02:11,600 has been deleted so if you want to 4744 03:02:10,080 --> 03:02:13,359 understand the logic for deletion and 4745 03:02:11,600 --> 03:02:16,560 end then follow the similar approach 4746 03:02:13,359 --> 03:02:18,080 that i have showed you earlier right so 4747 03:02:16,560 --> 03:02:19,600 now we will see the deletion of a 4748 03:02:18,080 --> 03:02:21,520 particular node at any specified 4749 03:02:19,600 --> 03:02:23,359 position 4750 03:02:21,520 --> 03:02:25,439 so after deleting the node at beginning 4751 03:02:23,359 --> 03:02:29,200 as well as end we will see how to delete 4752 03:02:25,439 --> 03:02:30,720 a node at a specified position so 4753 03:02:29,200 --> 03:02:33,600 the singly linked list 4754 03:02:30,720 --> 03:02:35,760 that i'm having as of now 4755 03:02:33,600 --> 03:02:39,040 looks like this 4756 03:02:35,760 --> 03:02:41,040 this is my n1 then after n1 i am having 4757 03:02:39,040 --> 03:02:42,560 niv then n2 4758 03:02:41,040 --> 03:02:44,560 and n3 4759 03:02:42,560 --> 03:02:47,200 and n4 4760 03:02:44,560 --> 03:02:49,680 so n1 is connected to niv that means the 4761 03:02:47,200 --> 03:02:52,240 link or the reference of n1 will contain 4762 03:02:49,680 --> 03:02:55,600 the address of nib 4763 03:02:52,240 --> 03:02:57,279 and ib2 n2 n2 n3 n3 to n4 4764 03:02:55,600 --> 03:02:59,840 so after n4 we don't have any other 4765 03:02:57,279 --> 03:03:02,319 nodes so n4 link will contain the 4766 03:02:59,840 --> 03:03:04,640 address none 4767 03:03:02,319 --> 03:03:07,040 so in n1 i'm having the data element as 4768 03:03:04,640 --> 03:03:10,000 five and ibs seven 4769 03:03:07,040 --> 03:03:12,080 then ten and here i'm having 15 here i'm 4770 03:03:10,000 --> 03:03:14,720 having 20 and if i want to delete let's 4771 03:03:12,080 --> 03:03:16,880 suppose n2 node so this is my position 4772 03:03:14,720 --> 03:03:19,760 three right so let me write the position 4773 03:03:16,880 --> 03:03:22,479 one two three four and five this is the 4774 03:03:19,760 --> 03:03:22,479 position three 4775 03:03:23,840 --> 03:03:27,439 so at position three i want to delete 4776 03:03:25,439 --> 03:03:30,080 this end to node so what i will do here 4777 03:03:27,439 --> 03:03:31,840 once again this is my head so as usual 4778 03:03:30,080 --> 03:03:33,600 this is my head so i've created a 4779 03:03:31,840 --> 03:03:35,680 variable previous in which i have 4780 03:03:33,600 --> 03:03:37,600 assigned self dot head then i have 4781 03:03:35,680 --> 03:03:40,240 created one more variable like the last 4782 03:03:37,600 --> 03:03:42,399 time a is equal to self dot head dot 4783 03:03:40,240 --> 03:03:44,560 next so self dot head is here previous 4784 03:03:42,399 --> 03:03:48,080 right so self dot head dot next will be 4785 03:03:44,560 --> 03:03:50,880 assigning to nib so this will be a 4786 03:03:48,080 --> 03:03:53,279 right so previous is equal to 4787 03:03:50,880 --> 03:03:56,080 self dot head 4788 03:03:53,279 --> 03:03:57,600 this is my head and a will be 4789 03:03:56,080 --> 03:04:00,479 self dot 4790 03:03:57,600 --> 03:04:00,479 head dot next 4791 03:04:00,560 --> 03:04:04,399 so now i want to delete this node so i 4792 03:04:02,880 --> 03:04:06,319 will be using this time for loop so i 4793 03:04:04,399 --> 03:04:08,319 will write here for i in range and then 4794 03:04:06,319 --> 03:04:10,399 one comma position minus one so let's 4795 03:04:08,319 --> 03:04:12,479 suppose that if i want to delete 4796 03:04:10,399 --> 03:04:14,960 the node at position three so what will 4797 03:04:12,479 --> 03:04:16,240 be happening here one 4798 03:04:14,960 --> 03:04:18,640 and then three minus one i will be 4799 03:04:16,240 --> 03:04:20,240 getting two so two will be excluded so 4800 03:04:18,640 --> 03:04:23,279 iteration will be done only for i is 4801 03:04:20,240 --> 03:04:25,680 equal to one so for i is equal to 1 a is 4802 03:04:23,279 --> 03:04:28,000 equal to a dot next and previous is 4803 03:04:25,680 --> 03:04:30,640 equal to previous dot next so what does 4804 03:04:28,000 --> 03:04:32,960 that mean that means simple 4805 03:04:30,640 --> 03:04:34,960 iteration i is equal to 1 4806 03:04:32,960 --> 03:04:37,920 my a value will be here now and my 4807 03:04:34,960 --> 03:04:39,600 previous will be here 4808 03:04:37,920 --> 03:04:41,520 right now 4809 03:04:39,600 --> 03:04:43,840 let's see this logic so i've written 4810 03:04:41,520 --> 03:04:46,000 here previous dot next is equal to a dot 4811 03:04:43,840 --> 03:04:48,720 next so this is my previous now so if 4812 03:04:46,000 --> 03:04:50,960 i'm writing here previous dot next 4813 03:04:48,720 --> 03:04:52,720 is equal to a dot next 4814 03:04:50,960 --> 03:04:55,120 so if you see this is the previous dot 4815 03:04:52,720 --> 03:04:57,520 next and this is the a dot next so that 4816 03:04:55,120 --> 03:04:59,120 means i am connecting previous dot next 4817 03:04:57,520 --> 03:05:00,399 with n3 4818 03:04:59,120 --> 03:05:02,560 right 4819 03:05:00,399 --> 03:05:04,720 and after that i have written a dot next 4820 03:05:02,560 --> 03:05:07,200 is equal to none so this is my a dot 4821 03:05:04,720 --> 03:05:09,120 next so i have disconnected this link 4822 03:05:07,200 --> 03:05:11,279 so you can see that this node has been 4823 03:05:09,120 --> 03:05:13,359 deleted so this is a basic logic just 4824 03:05:11,279 --> 03:05:15,279 simple i have done the iteration using 4825 03:05:13,359 --> 03:05:16,960 for loop and then after that i have 4826 03:05:15,279 --> 03:05:18,640 written previous dot next so this is the 4827 03:05:16,960 --> 03:05:20,240 previous dot next 4828 03:05:18,640 --> 03:05:21,840 which is equal to a dot next so in 4829 03:05:20,240 --> 03:05:24,080 previous dot next now i'm assigning the 4830 03:05:21,840 --> 03:05:25,279 value a dot next that is the entry value 4831 03:05:24,080 --> 03:05:27,760 so that means 4832 03:05:25,279 --> 03:05:29,359 here this link has been disconnected now 4833 03:05:27,760 --> 03:05:31,760 i want to disconnect this link so i'll 4834 03:05:29,359 --> 03:05:34,000 write here a dot next is equal to none 4835 03:05:31,760 --> 03:05:35,279 now let's see in the coding part 4836 03:05:34,000 --> 03:05:38,960 so let me create the function i will 4837 03:05:35,279 --> 03:05:38,960 write here def deletion 4838 03:05:40,399 --> 03:05:44,279 at particular node 4839 03:05:48,880 --> 03:05:53,279 so i will give the function name here 4840 03:05:50,399 --> 03:05:55,120 deletion at particular node so here 4841 03:05:53,279 --> 03:05:57,439 i'll give one more parameter other than 4842 03:05:55,120 --> 03:05:59,520 self i'll give you a position because i 4843 03:05:57,439 --> 03:06:03,120 want to delete a node at particular 4844 03:05:59,520 --> 03:06:03,120 position so i'll write here position 4845 03:06:03,359 --> 03:06:09,520 and inside this i'll write once again 4846 03:06:05,520 --> 03:06:09,520 previous is equal to self.head 4847 03:06:11,520 --> 03:06:15,520 and then i will write here a is equal to 4848 03:06:13,760 --> 03:06:17,520 self 4849 03:06:15,520 --> 03:06:20,000 dot head 4850 03:06:17,520 --> 03:06:22,800 dot next 4851 03:06:20,000 --> 03:06:25,279 now i will create a loop for i 4852 03:06:22,800 --> 03:06:25,279 in range 4853 03:06:25,680 --> 03:06:29,359 one comma position minus 1 4854 03:06:29,680 --> 03:06:35,600 and inside the slope 4855 03:06:31,520 --> 03:06:35,600 i'll write here a is equal to a dot next 4856 03:06:36,399 --> 03:06:39,760 and previous is equal to 4857 03:06:40,399 --> 03:06:45,920 previous dot next 4858 03:06:43,680 --> 03:06:47,520 now after this 4859 03:06:45,920 --> 03:06:49,600 what i will do here i'll write here 4860 03:06:47,520 --> 03:06:52,240 previous dot next 4861 03:06:49,600 --> 03:06:53,760 is equal to a dot next 4862 03:06:52,240 --> 03:06:56,000 so after writing this logic there will 4863 03:06:53,760 --> 03:06:59,520 be a disconnection of the link and then 4864 03:06:56,000 --> 03:06:59,520 i will write here a dot next 4865 03:07:02,000 --> 03:07:04,800 is equal to none 4866 03:07:05,680 --> 03:07:09,640 now let me call this function 4867 03:07:10,080 --> 03:07:15,359 so i will write here 4868 03:07:11,920 --> 03:07:15,359 let me copy and paste this 4869 03:07:17,200 --> 03:07:21,840 and what's the name of this function 4870 03:07:18,560 --> 03:07:25,240 deletion at particular node 4871 03:07:21,840 --> 03:07:25,240 at particular 4872 03:07:26,000 --> 03:07:29,359 node 4873 03:07:27,680 --> 03:07:31,359 and let's suppose that position 3 i want 4874 03:07:29,359 --> 03:07:33,439 to delete so i'll write here 4875 03:07:31,359 --> 03:07:34,880 3 4876 03:07:33,439 --> 03:07:36,560 so you will see the data element 10 will 4877 03:07:34,880 --> 03:07:38,560 be removed from here so now if i'm 4878 03:07:36,560 --> 03:07:41,680 running it 4879 03:07:38,560 --> 03:07:41,680 again i have to write print 4880 03:07:42,319 --> 03:07:45,760 to get the output in the next line 4881 03:07:46,399 --> 03:07:49,840 so if i'm running it you can see that 4882 03:07:47,920 --> 03:07:51,760 the 10 data element has been removed 4883 03:07:49,840 --> 03:07:53,040 because the node has been deleted so we 4884 03:07:51,760 --> 03:07:56,000 have seen here the insertion at 4885 03:07:53,040 --> 03:07:57,600 beginning and at particular node same 4886 03:07:56,000 --> 03:07:59,439 for the deletion and we have also seen 4887 03:07:57,600 --> 03:08:01,680 the traversal so this was all about 4888 03:07:59,439 --> 03:08:03,439 singly linked list 4889 03:08:01,680 --> 03:08:05,520 so now we will be seeing double english 4890 03:08:03,439 --> 03:08:06,960 concept 4891 03:08:05,520 --> 03:08:09,040 so what is the difference between singly 4892 03:08:06,960 --> 03:08:11,120 linked list and doubly linked list so 4893 03:08:09,040 --> 03:08:13,760 here let's see the node 4894 03:08:11,120 --> 03:08:14,800 of doubling list so in doubly linked 4895 03:08:13,760 --> 03:08:17,680 list 4896 03:08:14,800 --> 03:08:18,720 we are having two pointers so this 4897 03:08:17,680 --> 03:08:20,479 pointer 4898 03:08:18,720 --> 03:08:22,800 will contain the address of the previous 4899 03:08:20,479 --> 03:08:22,800 node 4900 03:08:28,399 --> 03:08:34,439 and the another pointer will contain the 4901 03:08:30,399 --> 03:08:34,439 address of next node 4902 03:08:36,800 --> 03:08:40,000 and here 4903 03:08:37,840 --> 03:08:43,120 this node will contain a data 4904 03:08:40,000 --> 03:08:45,600 so let me give the node name as n1 right 4905 03:08:43,120 --> 03:08:48,000 so this is the way to create a node in 4906 03:08:45,600 --> 03:08:50,000 doubly linked list now w english is a 4907 03:08:48,000 --> 03:08:51,920 collection of nodes in which each node 4908 03:08:50,000 --> 03:08:54,160 contains a data field and having two 4909 03:08:51,920 --> 03:08:56,080 pointers as i already explained you that 4910 03:08:54,160 --> 03:08:56,960 we are having two pointers and data 4911 03:08:56,080 --> 03:08:59,359 field 4912 03:08:56,960 --> 03:09:01,120 so one pointer is for previous node and 4913 03:08:59,359 --> 03:09:02,960 other for the next node 4914 03:09:01,120 --> 03:09:04,720 again the difference between singly 4915 03:09:02,960 --> 03:09:06,960 linked list and doubly linguist is that 4916 03:09:04,720 --> 03:09:08,960 in singly linked list we can traverse 4917 03:09:06,960 --> 03:09:11,120 only in the forward direction whereas in 4918 03:09:08,960 --> 03:09:13,439 doubling list we can traverse forward 4919 03:09:11,120 --> 03:09:15,600 direction as well as backward direction 4920 03:09:13,439 --> 03:09:18,720 so here 4921 03:09:15,600 --> 03:09:18,720 let me create the node 4922 03:09:22,560 --> 03:09:26,560 so these are the three nodes let's 4923 03:09:24,160 --> 03:09:26,560 suppose 4924 03:09:28,000 --> 03:09:32,479 i will give the name here n1 4925 03:09:30,960 --> 03:09:35,040 here as n2 4926 03:09:32,479 --> 03:09:35,840 here is n3 4927 03:09:35,040 --> 03:09:38,479 okay 4928 03:09:35,840 --> 03:09:41,200 so n1 and 2n3 are the node so this will 4929 03:09:38,479 --> 03:09:43,359 contain the address of previous node 4930 03:09:41,200 --> 03:09:44,479 right so do we have previous node here 4931 03:09:43,359 --> 03:09:46,640 no 4932 03:09:44,479 --> 03:09:47,680 so i will write here none 4933 03:09:46,640 --> 03:09:50,160 right 4934 03:09:47,680 --> 03:09:52,000 similarly n1 to n2 let's suppose n1 4935 03:09:50,160 --> 03:09:54,000 address is 1000 4936 03:09:52,000 --> 03:09:56,000 and 2 address is 2000 4937 03:09:54,000 --> 03:09:58,560 and 3 address is 3 000 4938 03:09:56,000 --> 03:10:00,080 so now coming to the end to node this 4939 03:09:58,560 --> 03:10:01,520 pointer will contain the address of 4940 03:10:00,080 --> 03:10:03,520 previous node so what's the address of 4941 03:10:01,520 --> 03:10:06,640 previous one thousand 4942 03:10:03,520 --> 03:10:08,479 so it will write here one thousand 4943 03:10:06,640 --> 03:10:10,000 now coming to the n1 node so this will 4944 03:10:08,479 --> 03:10:11,760 contain the address of the previous node 4945 03:10:10,000 --> 03:10:13,520 but we don't have any previous node here 4946 03:10:11,760 --> 03:10:14,479 so it will contain the address as null 4947 03:10:13,520 --> 03:10:17,040 or none 4948 03:10:14,479 --> 03:10:18,800 similarly now if we see the pointer of 4949 03:10:17,040 --> 03:10:20,560 this n1 node this will contain the 4950 03:10:18,800 --> 03:10:22,479 address of next node so here it will 4951 03:10:20,560 --> 03:10:25,040 contain the address 4952 03:10:22,479 --> 03:10:27,279 2000 4953 03:10:25,040 --> 03:10:28,160 similarly this will contain the address 4954 03:10:27,279 --> 03:10:31,040 of 4955 03:10:28,160 --> 03:10:32,640 next node that is 3000 4956 03:10:31,040 --> 03:10:34,080 similarly n3 node 4957 03:10:32,640 --> 03:10:36,720 this will contain the address of 4958 03:10:34,080 --> 03:10:38,800 previous node that is 2 000 4959 03:10:36,720 --> 03:10:40,399 and here do we have any node no 4960 03:10:38,800 --> 03:10:41,920 so as we know that this 4961 03:10:40,399 --> 03:10:43,920 pointer will contain the address of the 4962 03:10:41,920 --> 03:10:45,520 next node so we don't have any next node 4963 03:10:43,920 --> 03:10:47,120 present here so it will contain the 4964 03:10:45,520 --> 03:10:49,439 address as null 4965 03:10:47,120 --> 03:10:51,040 so this is a basic representation of 4966 03:10:49,439 --> 03:10:52,720 doubly linked list 4967 03:10:51,040 --> 03:10:54,479 and here we can write the data elements 4968 03:10:52,720 --> 03:10:55,439 let's suppose 5 4969 03:10:54,479 --> 03:10:57,359 10 4970 03:10:55,439 --> 03:10:59,600 15 4971 03:10:57,359 --> 03:11:01,680 and obviously my head will be always 4972 03:10:59,600 --> 03:11:04,800 pointing to the first node so this will 4973 03:11:01,680 --> 03:11:06,399 be my head 4974 03:11:04,800 --> 03:11:08,239 so this is the basic representation of 4975 03:11:06,399 --> 03:11:10,399 doubly linked list here the traversal 4976 03:11:08,239 --> 03:11:12,000 can be done in both the directions 4977 03:11:10,399 --> 03:11:14,000 now let's see the operation in doubly 4978 03:11:12,000 --> 03:11:15,600 linked list so like singly linked list 4979 03:11:14,000 --> 03:11:18,239 in doubly linked list also we are having 4980 03:11:15,600 --> 03:11:20,399 three operations insertion deletion and 4981 03:11:18,239 --> 03:11:23,279 traversal so in insertion we are having 4982 03:11:20,399 --> 03:11:25,200 beginning at specified note at end that 4983 03:11:23,279 --> 03:11:27,439 means we can insert the node at 4984 03:11:25,200 --> 03:11:29,439 beginning at a specified node or at the 4985 03:11:27,439 --> 03:11:30,399 end position similarly for the deletion 4986 03:11:29,439 --> 03:11:32,399 also 4987 03:11:30,399 --> 03:11:34,560 now coming to the traversal here we can 4988 03:11:32,399 --> 03:11:36,239 go to the forward direction as well as 4989 03:11:34,560 --> 03:11:38,319 backward direction so we have already 4990 03:11:36,239 --> 03:11:39,680 seen the logic of forward direction so 4991 03:11:38,319 --> 03:11:42,800 here i will explain you the logic of 4992 03:11:39,680 --> 03:11:42,800 backward direction also 4993 03:11:43,520 --> 03:11:47,680 now let's see the pseudo code of doubly 4994 03:11:45,760 --> 03:11:48,960 linked list now coming to the pseudo 4995 03:11:47,680 --> 03:11:51,439 code the first thing is that you have to 4996 03:11:48,960 --> 03:11:54,000 create a node so how to create a node of 4997 03:11:51,439 --> 03:11:56,640 doubly linked list so doubly link list 4998 03:11:54,000 --> 03:11:58,640 if i want to create a node 4999 03:11:56,640 --> 03:12:00,399 first i will write a class and the class 5000 03:11:58,640 --> 03:12:02,880 name is node then i will create init 5001 03:12:00,399 --> 03:12:05,040 method and we know that in a node let me 5002 03:12:02,880 --> 03:12:06,319 give the node name is n1 we are having 5003 03:12:05,040 --> 03:12:07,840 data 5004 03:12:06,319 --> 03:12:11,120 and two pointers 5005 03:12:07,840 --> 03:12:13,200 so here i will give the name as previous 5006 03:12:11,120 --> 03:12:15,120 and here i will give the name as next so 5007 03:12:13,200 --> 03:12:18,160 how to create a node i will write here 5008 03:12:15,120 --> 03:12:21,439 n1 and then node 5009 03:12:18,160 --> 03:12:23,520 and i will write here data 5010 03:12:21,439 --> 03:12:25,680 so my node will create here right so if 5011 03:12:23,520 --> 03:12:28,880 you will see here instead of self n1 5012 03:12:25,680 --> 03:12:31,760 will go so my n1 dot data 5013 03:12:28,880 --> 03:12:35,120 is equal to data 5014 03:12:31,760 --> 03:12:35,120 n1 dot previous 5015 03:12:35,200 --> 03:12:41,439 is equal to none 5016 03:12:37,279 --> 03:12:44,239 and my n1 dot next is equal to none 5017 03:12:41,439 --> 03:12:46,399 so next will assign to none here 5018 03:12:44,239 --> 03:12:48,479 and previous will assign here 5019 03:12:46,399 --> 03:12:50,239 to none so this is my initial condition 5020 03:12:48,479 --> 03:12:52,160 how to create a node here you can see 5021 03:12:50,239 --> 03:12:54,479 that self dot data is equal to data that 5022 03:12:52,160 --> 03:12:56,880 means n1 dot data is equal to data n1 5023 03:12:54,479 --> 03:12:58,800 dot previous is equal to none 5024 03:12:56,880 --> 03:13:01,200 so n1 dot previous is equal to none i am 5025 03:12:58,800 --> 03:13:02,399 getting here and then n1 dot next is 5026 03:13:01,200 --> 03:13:05,200 equal to none 5027 03:13:02,399 --> 03:13:06,640 so this is the way to create a node now 5028 03:13:05,200 --> 03:13:08,960 coming to how to create a class of 5029 03:13:06,640 --> 03:13:10,960 double english so initially when your 5030 03:13:08,960 --> 03:13:12,800 head is empty so we don't have any 5031 03:13:10,960 --> 03:13:15,120 doubly linked list because head is not 5032 03:13:12,800 --> 03:13:17,279 pointing to any node then it will be 5033 03:13:15,120 --> 03:13:19,279 empty right so here i will create a 5034 03:13:17,279 --> 03:13:21,279 class whose name is doubly linked list 5035 03:13:19,279 --> 03:13:23,040 and then i will create a init method and 5036 03:13:21,279 --> 03:13:25,120 then i will write self.head is equal to 5037 03:13:23,040 --> 03:13:27,920 none so that means my head is pointing 5038 03:13:25,120 --> 03:13:28,880 to none so my linked list is empty as of 5039 03:13:27,920 --> 03:13:30,800 now 5040 03:13:28,880 --> 03:13:32,319 now we will see the traversal operation 5041 03:13:30,800 --> 03:13:34,479 in doubly linked list 5042 03:13:32,319 --> 03:13:36,560 so here this is a code for the forward 5043 03:13:34,479 --> 03:13:38,399 traversal that we have already seen in 5044 03:13:36,560 --> 03:13:39,840 the singly linked list 5045 03:13:38,399 --> 03:13:44,160 right 5046 03:13:39,840 --> 03:13:44,160 so let me create the node of w link list 5047 03:13:46,880 --> 03:13:50,239 let's say it's a doubly linked list 5048 03:13:50,399 --> 03:13:57,640 this is the nbn node 5049 03:13:52,239 --> 03:13:57,640 this is the n2 node this is the n3 node 5050 03:13:58,880 --> 03:14:03,200 so here it will contain data as d1 5051 03:14:01,760 --> 03:14:05,680 d2 5052 03:14:03,200 --> 03:14:05,680 d3 5053 03:14:06,080 --> 03:14:10,200 then it is connected like this 5054 03:14:11,200 --> 03:14:15,920 it will be none 5055 03:14:13,439 --> 03:14:18,000 and here it will be also none 5056 03:14:15,920 --> 03:14:19,359 and the head will be pointing to the 5057 03:14:18,000 --> 03:14:20,399 end 5058 03:14:19,359 --> 03:14:22,000 node 5059 03:14:20,399 --> 03:14:24,000 now if i want to go for the forward 5060 03:14:22,000 --> 03:14:25,520 direction then as we know that my head 5061 03:14:24,000 --> 03:14:29,359 is fixed so i will create a temporary 5062 03:14:25,520 --> 03:14:32,000 variable a is equal to self dot head 5063 03:14:29,359 --> 03:14:33,520 then i will create a loop right because 5064 03:14:32,000 --> 03:14:35,200 i don't want to change my a value that's 5065 03:14:33,520 --> 03:14:37,359 why i have created a temporary variable 5066 03:14:35,200 --> 03:14:39,760 so when i'm using a loop here here a 5067 03:14:37,359 --> 03:14:41,120 value will be change right 5068 03:14:39,760 --> 03:14:44,319 so if you see here i have written here 5069 03:14:41,120 --> 03:14:44,319 while a is not none 5070 03:14:45,760 --> 03:14:50,160 so initially my a is assigning to here a 5071 03:14:47,760 --> 03:14:52,399 is equal to self dot head that means it 5072 03:14:50,160 --> 03:14:55,040 is assigning to n1 node now what i will 5073 03:14:52,399 --> 03:14:57,439 do here while a is not none so this is 5074 03:14:55,040 --> 03:14:57,439 my a 5075 03:14:57,760 --> 03:15:01,760 and a is self dot head right it is not 5076 03:14:59,840 --> 03:15:03,920 none then what i will do i will traverse 5077 03:15:01,760 --> 03:15:06,160 it so i am writing here the logic while 5078 03:15:03,920 --> 03:15:09,439 a is not none so what is my a a is equal 5079 03:15:06,160 --> 03:15:11,439 to self dot head so initially 5080 03:15:09,439 --> 03:15:13,279 i have assigned my head value inside a 5081 03:15:11,439 --> 03:15:16,000 so now what i will do i will write here 5082 03:15:13,279 --> 03:15:17,920 print a dot data 5083 03:15:16,000 --> 03:15:19,120 so it will print the value of 5084 03:15:17,920 --> 03:15:21,040 d1 right 5085 03:15:19,120 --> 03:15:23,520 now after that i have a sign here a is 5086 03:15:21,040 --> 03:15:25,760 equal to a dot next 5087 03:15:23,520 --> 03:15:28,000 so what is a dot next a dot next will 5088 03:15:25,760 --> 03:15:30,080 contain the address of n2 node right so 5089 03:15:28,000 --> 03:15:33,040 my a will shift here right and this is a 5090 03:15:30,080 --> 03:15:34,880 loop so this loop will run till my a is 5091 03:15:33,040 --> 03:15:36,720 not end then again the a value will be 5092 03:15:34,880 --> 03:15:38,399 go up to here so in this way whatever 5093 03:15:36,720 --> 03:15:40,239 the data elements are there i will write 5094 03:15:38,399 --> 03:15:42,160 here inside this while loop and i will 5095 03:15:40,239 --> 03:15:43,840 print it out so now after forward 5096 03:15:42,160 --> 03:15:46,239 traversal we will see the logic of 5097 03:15:43,840 --> 03:15:48,720 backward traversal so let me write here 5098 03:15:46,239 --> 03:15:51,279 backward 5099 03:15:48,720 --> 03:15:51,279 traversal 5100 03:15:52,800 --> 03:15:56,479 let me create a waiting list so this is 5101 03:15:54,560 --> 03:16:00,600 my doubly linked list 5102 03:15:56,479 --> 03:16:00,600 which will be having two pointers 5103 03:16:03,359 --> 03:16:10,080 let me write here the name n1 n2 and n3 5104 03:16:07,439 --> 03:16:11,680 so this will be pointing to none 5105 03:16:10,080 --> 03:16:13,680 this will contain the address of the 5106 03:16:11,680 --> 03:16:15,439 next node this will also contain the 5107 03:16:13,680 --> 03:16:17,200 address of next node 5108 03:16:15,439 --> 03:16:18,239 n2 will contain the address of previous 5109 03:16:17,200 --> 03:16:20,319 node 5110 03:16:18,239 --> 03:16:22,080 n3 will also contain the address of 5111 03:16:20,319 --> 03:16:23,680 previous node 5112 03:16:22,080 --> 03:16:25,359 so what does it mean that n3 will 5113 03:16:23,680 --> 03:16:28,239 contain the address of previous node 5114 03:16:25,359 --> 03:16:29,760 that means n3 this pointer 5115 03:16:28,239 --> 03:16:31,840 will contain the address of previous 5116 03:16:29,760 --> 03:16:31,840 node 5117 03:16:32,000 --> 03:16:36,000 now 5118 03:16:34,239 --> 03:16:38,160 after this p node head will be pointing 5119 03:16:36,000 --> 03:16:40,479 to here and we don't have any previous 5120 03:16:38,160 --> 03:16:43,520 node so the pointer of this n1 node will 5121 03:16:40,479 --> 03:16:45,600 contain the address as none 5122 03:16:43,520 --> 03:16:48,160 now i want to go for the backward 5123 03:16:45,600 --> 03:16:51,040 traversal so we know that in linked list 5124 03:16:48,160 --> 03:16:53,680 if you want to go to n3 node then you 5125 03:16:51,040 --> 03:16:55,680 have to go to n1 n2 then only you can go 5126 03:16:53,680 --> 03:16:58,399 to n3 right so we have to traverse 5127 03:16:55,680 --> 03:17:00,080 basically from n1 to n2 then only we can 5128 03:16:58,399 --> 03:17:02,399 get to n3 5129 03:17:00,080 --> 03:17:04,960 so here first what i will do once again 5130 03:17:02,399 --> 03:17:06,560 i will create a temporary variable a 5131 03:17:04,960 --> 03:17:08,319 and i will assign self dot head into 5132 03:17:06,560 --> 03:17:11,680 this because i don't want to change my 5133 03:17:08,319 --> 03:17:13,760 head value and i will be using iteration 5134 03:17:11,680 --> 03:17:16,160 so what i will do here while a dot next 5135 03:17:13,760 --> 03:17:18,880 is not none my a is equal to self dot 5136 03:17:16,160 --> 03:17:20,800 head right a dot next is this right a 5137 03:17:18,880 --> 03:17:23,520 dot next is pointing to n2 that means a 5138 03:17:20,800 --> 03:17:24,479 dot next will contain the address of n2 5139 03:17:23,520 --> 03:17:26,720 node 5140 03:17:24,479 --> 03:17:28,479 so here a dot next is not none that 5141 03:17:26,720 --> 03:17:30,319 means a is equal to a dot next so my 5142 03:17:28,479 --> 03:17:32,319 value will be a here similarly once 5143 03:17:30,319 --> 03:17:34,160 again this while loop will run a dot 5144 03:17:32,319 --> 03:17:36,640 next is not none right so once again a 5145 03:17:34,160 --> 03:17:38,080 value will be here so you can see that 5146 03:17:36,640 --> 03:17:41,760 we have traverse 5147 03:17:38,080 --> 03:17:43,520 from n1 to n2 and now n2 to n3 right 5148 03:17:41,760 --> 03:17:45,520 so we know that backward traversal is 5149 03:17:43,520 --> 03:17:48,399 possible only when we are traversing 5150 03:17:45,520 --> 03:17:50,720 from n1 to n2 n2 to n3 right so now what 5151 03:17:48,399 --> 03:17:53,120 will happen here now my a value is here 5152 03:17:50,720 --> 03:17:54,800 now i can do the backward traversal so 5153 03:17:53,120 --> 03:17:57,760 what will be the logic the logic will be 5154 03:17:54,800 --> 03:18:00,239 the same i will write here print 5155 03:17:57,760 --> 03:18:02,319 a dot data 5156 03:18:00,239 --> 03:18:05,439 and this time i will assign here a is 5157 03:18:02,319 --> 03:18:06,960 equal to a dot previous 5158 03:18:05,439 --> 03:18:08,960 so we know that 5159 03:18:06,960 --> 03:18:11,520 this pointer is my previous pointer and 5160 03:18:08,960 --> 03:18:13,359 this pointer is my next pointer 5161 03:18:11,520 --> 03:18:16,239 so this is my a value here if i'm 5162 03:18:13,359 --> 03:18:19,040 writing a dot previous 5163 03:18:16,239 --> 03:18:21,040 right then i can go backward so what i 5164 03:18:19,040 --> 03:18:23,359 will do i will put this condition inside 5165 03:18:21,040 --> 03:18:25,760 a while loop so i'll write here while a 5166 03:18:23,359 --> 03:18:27,680 is not none 5167 03:18:25,760 --> 03:18:30,000 so if you will see here a is not none 5168 03:18:27,680 --> 03:18:32,000 then i'm assigning a dot previous into a 5169 03:18:30,000 --> 03:18:34,080 since this is a while loop so once again 5170 03:18:32,000 --> 03:18:36,000 my a value will change from here to here 5171 03:18:34,080 --> 03:18:37,840 so a is equal to a dot previous and then 5172 03:18:36,000 --> 03:18:38,800 i will go here a is equal to a dot 5173 03:18:37,840 --> 03:18:40,880 previous 5174 03:18:38,800 --> 03:18:43,920 if you see here the condition while a is 5175 03:18:40,880 --> 03:18:46,160 not none so after coming here 5176 03:18:43,920 --> 03:18:47,840 if i'm writing here a dot previous then 5177 03:18:46,160 --> 03:18:49,840 it is pointing to none right so it will 5178 03:18:47,840 --> 03:18:52,640 end here so in this way i can do the 5179 03:18:49,840 --> 03:18:55,840 backward traversal so this was the logic 5180 03:18:52,640 --> 03:18:55,840 for backward traversal 5181 03:18:59,439 --> 03:19:02,880 so now let's see the program of doubly 5182 03:19:01,120 --> 03:19:05,840 linked list so let me write here doubly 5183 03:19:02,880 --> 03:19:05,840 linked list 5184 03:19:10,160 --> 03:19:14,479 so now what i will do here since we know 5185 03:19:12,000 --> 03:19:16,319 that forward traversal will be same as 5186 03:19:14,479 --> 03:19:19,840 in single english so i will copy the 5187 03:19:16,319 --> 03:19:19,840 code from single link list 5188 03:19:20,880 --> 03:19:26,680 so let me copy 5189 03:19:22,960 --> 03:19:26,680 the code up to here 5190 03:19:34,560 --> 03:19:37,680 and i will also copy 5191 03:19:42,080 --> 03:19:47,920 and i will also copy the object of class 5192 03:19:45,040 --> 03:19:51,200 node that is n1 n2 n3 and n4 and i have 5193 03:19:47,920 --> 03:19:51,200 linked here so i will copy it 5194 03:19:58,479 --> 03:20:02,640 okay 5195 03:19:59,279 --> 03:20:02,640 let me remove all this thing 5196 03:20:04,880 --> 03:20:09,279 so i am not writing once again code just 5197 03:20:06,880 --> 03:20:10,800 to save the time 5198 03:20:09,279 --> 03:20:12,880 because i have already explained you in 5199 03:20:10,800 --> 03:20:15,840 singly linked list 5200 03:20:12,880 --> 03:20:15,840 so 5201 03:20:22,560 --> 03:20:26,680 let me remove this thing also 5202 03:20:31,040 --> 03:20:35,279 okay so now let's see the w language so 5203 03:20:33,359 --> 03:20:38,399 first i want to create a node so this is 5204 03:20:35,279 --> 03:20:40,720 my class whose class name is node and 5205 03:20:38,399 --> 03:20:42,720 inside this class node i have created a 5206 03:20:40,720 --> 03:20:44,399 init method so 5207 03:20:42,720 --> 03:20:46,239 what will happen in doubly linked list i 5208 03:20:44,399 --> 03:20:48,239 will be having two pointers 5209 03:20:46,239 --> 03:20:49,760 one pointer name is next another pointer 5210 03:20:48,239 --> 03:20:51,120 name is previous so i will create here 5211 03:20:49,760 --> 03:20:53,279 one more pointer 5212 03:20:51,120 --> 03:20:54,960 self dot 5213 03:20:53,279 --> 03:20:56,800 previous 5214 03:20:54,960 --> 03:20:58,479 is equal to none 5215 03:20:56,800 --> 03:21:00,800 and inside this class of doubly linked 5216 03:20:58,479 --> 03:21:03,520 list once again i will create a init 5217 03:21:00,800 --> 03:21:05,040 method and inside this init method i 5218 03:21:03,520 --> 03:21:07,120 will write self dot head is equal to 5219 03:21:05,040 --> 03:21:08,880 none so that means initially if my head 5220 03:21:07,120 --> 03:21:10,479 is none then my doubly linked list is 5221 03:21:08,880 --> 03:21:12,800 empty 5222 03:21:10,479 --> 03:21:17,200 so let me remove this thing also 5223 03:21:12,800 --> 03:21:17,200 and here i will write forward traversal 5224 03:21:19,920 --> 03:21:22,399 right 5225 03:21:20,960 --> 03:21:24,560 so we have already seen the logic of 5226 03:21:22,399 --> 03:21:26,800 forward reversal right so let me copy 5227 03:21:24,560 --> 03:21:26,800 this 5228 03:21:28,000 --> 03:21:32,160 so inside this class i will create a 5229 03:21:30,319 --> 03:21:35,439 backward traversal function name so i'll 5230 03:21:32,160 --> 03:21:35,439 write here backward reversal 5231 03:21:35,920 --> 03:21:39,600 and here also the same thing will be 5232 03:21:37,359 --> 03:21:43,200 here if self.head is none then i will 5233 03:21:39,600 --> 03:21:43,200 write here w lingus is empty 5234 03:21:44,239 --> 03:21:48,080 let me change the name here also 5235 03:21:50,840 --> 03:21:55,359 right so initially now coming to the 5236 03:21:53,520 --> 03:21:57,600 else condition yeah a is equal to 5237 03:21:55,359 --> 03:21:59,040 self.head right initially this condition 5238 03:21:57,600 --> 03:22:01,680 must be there because i don't want to 5239 03:21:59,040 --> 03:22:04,000 change my head now i'll give a condition 5240 03:22:01,680 --> 03:22:04,800 while 5241 03:22:04,000 --> 03:22:06,720 a 5242 03:22:04,800 --> 03:22:07,520 dot next 5243 03:22:06,720 --> 03:22:10,080 is 5244 03:22:07,520 --> 03:22:10,080 not none 5245 03:22:12,000 --> 03:22:16,399 i'll write here a is equal to a dot next 5246 03:22:15,040 --> 03:22:18,160 so as we know that for backward 5247 03:22:16,399 --> 03:22:20,080 traversal first you have to go through 5248 03:22:18,160 --> 03:22:22,000 each node right so let's suppose if 5249 03:22:20,080 --> 03:22:24,800 there are n4 nodes then you have to go 5250 03:22:22,000 --> 03:22:27,359 from n1 to n2 n3 to n4 so this is the 5251 03:22:24,800 --> 03:22:29,040 logic to go at the last node and after 5252 03:22:27,359 --> 03:22:31,439 that we can perform the backward 5253 03:22:29,040 --> 03:22:33,279 traversal so here i will write instead 5254 03:22:31,439 --> 03:22:35,840 of a dot next i will write here a dot 5255 03:22:33,279 --> 03:22:35,840 previous 5256 03:22:36,560 --> 03:22:40,720 right 5257 03:22:38,239 --> 03:22:43,840 let me change the object name here 5258 03:22:40,720 --> 03:22:43,840 this is a doubly linked list 5259 03:22:44,239 --> 03:22:49,279 and then dll.head 5260 03:22:47,200 --> 03:22:51,520 will be is equal to n1 5261 03:22:49,279 --> 03:22:55,479 and then again the same thing is there 5262 03:22:51,520 --> 03:22:55,479 here it will be dll.traversal 5263 03:22:56,239 --> 03:23:00,000 so what here the name is forward 5264 03:22:57,760 --> 03:23:03,720 traversal and backward traversal right 5265 03:23:00,000 --> 03:23:03,720 so i will write here forward 5266 03:23:05,600 --> 03:23:11,600 and once again i will write here dll dot 5267 03:23:09,040 --> 03:23:11,600 backward 5268 03:23:11,840 --> 03:23:14,560 traversal 5269 03:23:14,960 --> 03:23:18,160 so now if i'm executing it 5270 03:23:19,840 --> 03:23:23,600 you can see that i'm getting error 5271 03:23:25,439 --> 03:23:31,560 why i'm getting error here because i 5272 03:23:27,439 --> 03:23:31,560 have to put here underscore 5273 03:23:31,840 --> 03:23:35,760 and underscore 5274 03:23:34,160 --> 03:23:37,600 but before that we have connected the 5275 03:23:35,760 --> 03:23:39,040 node but it's only for the forward 5276 03:23:37,600 --> 03:23:41,200 traversal right i have to connect the 5277 03:23:39,040 --> 03:23:44,640 node for the previous one also so if 5278 03:23:41,200 --> 03:23:46,239 here i am created n2 node let's suppose 5279 03:23:44,640 --> 03:23:48,080 here i have created the n2 node so after 5280 03:23:46,239 --> 03:23:50,960 that i will write here 5281 03:23:48,080 --> 03:23:53,279 n2 dot previous 5282 03:23:50,960 --> 03:23:55,840 is equal to n1 5283 03:23:53,279 --> 03:23:57,840 and after creating n3 node i will also 5284 03:23:55,840 --> 03:24:00,479 write you 5285 03:23:57,840 --> 03:24:02,640 three dot previous 5286 03:24:00,479 --> 03:24:04,880 is equal to 5287 03:24:02,640 --> 03:24:06,800 n2 now you can see that n2 will be 5288 03:24:04,880 --> 03:24:09,279 having two pointers right so n2 dot 5289 03:24:06,800 --> 03:24:12,239 previous is connected with n1 and n2 dot 5290 03:24:09,279 --> 03:24:13,120 next is connected with n3 5291 03:24:12,239 --> 03:24:16,239 now 5292 03:24:13,120 --> 03:24:18,239 after creating n4 i'll write here 5293 03:24:16,239 --> 03:24:21,439 n4 dot previous 5294 03:24:18,239 --> 03:24:23,600 is connected to n3 5295 03:24:21,439 --> 03:24:26,960 right 5296 03:24:23,600 --> 03:24:29,279 now if i'm executing it on executing you 5297 03:24:26,960 --> 03:24:31,040 can see that i am getting the output but 5298 03:24:29,279 --> 03:24:33,120 in the same line so let me write here 5299 03:24:31,040 --> 03:24:35,439 print and inside print i will not write 5300 03:24:33,120 --> 03:24:37,359 anything and now if i'm executing you 5301 03:24:35,439 --> 03:24:39,040 can see that i am getting backward 5302 03:24:37,359 --> 03:24:40,960 reversal through now let's understand 5303 03:24:39,040 --> 03:24:43,279 the logic how does this backward 5304 03:24:40,960 --> 03:24:44,960 reversal is working so if i'm creating a 5305 03:24:43,279 --> 03:24:47,680 node here 5306 03:24:44,960 --> 03:24:49,920 let's suppose n1 is equal to node 5 5307 03:24:47,680 --> 03:24:52,080 if you see here 5308 03:24:49,920 --> 03:24:55,200 instead of self and 1 will go and the 5309 03:24:52,080 --> 03:24:56,080 data that i have given to n1 node is 5 5310 03:24:55,200 --> 03:24:59,200 right 5311 03:24:56,080 --> 03:25:01,439 so instead of data i will get 5 here 5312 03:24:59,200 --> 03:25:05,680 so what will happen here 5313 03:25:01,439 --> 03:25:05,680 n1.data i will getting s5 5314 03:25:07,600 --> 03:25:10,880 similarly n1 dot 5315 03:25:11,439 --> 03:25:15,120 next i will be getting none 5316 03:25:20,800 --> 03:25:24,880 and then 5317 03:25:22,880 --> 03:25:27,279 n1 dot previous 5318 03:25:24,880 --> 03:25:28,960 i will getting as none 5319 03:25:27,279 --> 03:25:30,800 so this is my node creation right if i'm 5320 03:25:28,960 --> 03:25:33,359 creating an initial node then i'm having 5321 03:25:30,800 --> 03:25:35,040 the data s5 and the next pointer is 5322 03:25:33,359 --> 03:25:36,399 pointing to none and previous pointer is 5323 03:25:35,040 --> 03:25:38,160 also pointing to none 5324 03:25:36,399 --> 03:25:39,760 right and after that you can see that i 5325 03:25:38,160 --> 03:25:42,239 have created an object of doubly linked 5326 03:25:39,760 --> 03:25:44,080 list that is a small dll so what will 5327 03:25:42,239 --> 03:25:46,319 happen here as soon as you are creating 5328 03:25:44,080 --> 03:25:48,479 the object the init method inside a 5329 03:25:46,319 --> 03:25:50,720 class will run so here 5330 03:25:48,479 --> 03:25:53,040 instead of self dll will go so i will 5331 03:25:50,720 --> 03:25:55,520 getting here dll.head 5332 03:25:53,040 --> 03:25:57,520 is equal to none 5333 03:25:55,520 --> 03:25:59,279 so when my dll.head is equal to none 5334 03:25:57,520 --> 03:26:01,279 that means my doubly linked list is 5335 03:25:59,279 --> 03:26:02,800 empty right so what i will do here i 5336 03:26:01,279 --> 03:26:06,160 will assign here 5337 03:26:02,800 --> 03:26:08,720 dll.head is equal to n1 so now my head 5338 03:26:06,160 --> 03:26:10,560 is assigning to n1 right 5339 03:26:08,720 --> 03:26:12,880 so after this 5340 03:26:10,560 --> 03:26:14,880 once again i have created a node n2 you 5341 03:26:12,880 --> 03:26:17,200 can see that so here once again the node 5342 03:26:14,880 --> 03:26:18,720 n2 will be created and n2 dot data will 5343 03:26:17,200 --> 03:26:20,080 be is equal to 10 because i have given 5344 03:26:18,720 --> 03:26:22,239 the data here 10 5345 03:26:20,080 --> 03:26:24,560 and then i have written here n2 dot 5346 03:26:22,239 --> 03:26:26,960 previous is equal to n1 so after taking 5347 03:26:24,560 --> 03:26:29,680 the data s10 what will happen here 5348 03:26:26,960 --> 03:26:29,680 let me show you 5349 03:26:30,239 --> 03:26:35,520 n2 will be here and then 5350 03:26:32,880 --> 03:26:38,800 instead of data 10 will be here 5351 03:26:35,520 --> 03:26:40,479 and now my n2 dot data 5352 03:26:38,800 --> 03:26:42,640 will be is equal to 5353 03:26:40,479 --> 03:26:42,640 10 5354 03:26:45,120 --> 03:26:50,239 my n2 dot 5355 03:26:47,840 --> 03:26:51,040 next will be also equal to 5356 03:26:50,239 --> 03:26:52,800 none 5357 03:26:51,040 --> 03:26:56,479 because as of now i didn't connect this 5358 03:26:52,800 --> 03:26:59,279 node i have just created this node right 5359 03:26:56,479 --> 03:27:00,880 n2 dot previous will be also is equal to 5360 03:26:59,279 --> 03:27:03,359 none 5361 03:27:00,880 --> 03:27:04,800 so as soon as i have created n2 node 5362 03:27:03,359 --> 03:27:06,560 what i will do here 5363 03:27:04,800 --> 03:27:07,520 i will write here n2 dot previous is 5364 03:27:06,560 --> 03:27:10,479 equal to 5365 03:27:07,520 --> 03:27:13,200 n1 so if you see here n2 dot previous 5366 03:27:10,479 --> 03:27:15,760 was none but right now i will write here 5367 03:27:13,200 --> 03:27:18,560 n2 dot previous is equal to n1 so that 5368 03:27:15,760 --> 03:27:19,520 means i am connecting n1 node to n2 5369 03:27:18,560 --> 03:27:21,520 right 5370 03:27:19,520 --> 03:27:23,920 so the previous pointer of the n2 node 5371 03:27:21,520 --> 03:27:26,160 will be connected with n1 similarly n1 5372 03:27:23,920 --> 03:27:28,160 dot next is equal to n2 5373 03:27:26,160 --> 03:27:30,239 so initially if you see my n1 dot next 5374 03:27:28,160 --> 03:27:32,560 is equal to none right but my next 5375 03:27:30,239 --> 03:27:34,880 pointer will be connected to the n2 that 5376 03:27:32,560 --> 03:27:37,200 means my next pointer of n1 will contain 5377 03:27:34,880 --> 03:27:39,920 the address of n2 so this is the logic i 5378 03:27:37,200 --> 03:27:39,920 hope you understand 5379 03:27:40,399 --> 03:27:43,600 now i've already shown you the forward 5380 03:27:41,680 --> 03:27:46,000 traversal now coming to the 5381 03:27:43,600 --> 03:27:47,680 backward reversal what will happen here 5382 03:27:46,000 --> 03:27:50,000 let me show you 5383 03:27:47,680 --> 03:27:52,080 so initially my dll.head 5384 03:27:50,000 --> 03:27:53,279 will be 5385 03:27:52,080 --> 03:27:56,399 assigned as 5386 03:27:53,279 --> 03:27:59,359 n1 right initially if you see dll.head 5387 03:27:56,399 --> 03:28:01,920 was none but i have written here 5388 03:27:59,359 --> 03:28:04,399 dll.head is equal to n1 so it will be 5389 03:28:01,920 --> 03:28:07,120 the dll.head is equal to n1 because in 5390 03:28:04,399 --> 03:28:09,040 python the statement will be executed 5391 03:28:07,120 --> 03:28:10,399 line by line so whatever the statement 5392 03:28:09,040 --> 03:28:12,720 you have written here it will execute 5393 03:28:10,399 --> 03:28:15,040 first then it will execute then it will 5394 03:28:12,720 --> 03:28:17,040 execute so that's why so if you'll see 5395 03:28:15,040 --> 03:28:19,279 here dll.head is equal to n1 so the 5396 03:28:17,040 --> 03:28:21,680 value will be n1 now let's see the 5397 03:28:19,279 --> 03:28:24,560 condition while a dot next is not none 5398 03:28:21,680 --> 03:28:26,479 so what is a here so if you see a here 5399 03:28:24,560 --> 03:28:29,520 so a is nothing but n1 so i'll write 5400 03:28:26,479 --> 03:28:29,520 here n1 dot next 5401 03:28:30,640 --> 03:28:35,760 so is my n1 dot next is equal to none no 5402 03:28:32,880 --> 03:28:38,000 n1 dot next is pointing to the n2 right 5403 03:28:35,760 --> 03:28:39,120 because n1.next will contain the address 5404 03:28:38,000 --> 03:28:42,080 of the 5405 03:28:39,120 --> 03:28:45,279 n2 node so what will happen here 5406 03:28:42,080 --> 03:28:45,279 my new a value will be 5407 03:28:48,239 --> 03:28:52,800 a is equal to n2 5408 03:28:51,040 --> 03:28:54,479 so here you can see that i am going 5409 03:28:52,800 --> 03:28:56,800 through each node right 5410 03:28:54,479 --> 03:29:00,319 so what will happen here at last my a 5411 03:28:56,800 --> 03:29:02,800 value is equal to n4 so now n4 will go 5412 03:29:00,319 --> 03:29:04,160 here and n4 dot data is what if you see 5413 03:29:02,800 --> 03:29:06,960 here 5414 03:29:04,160 --> 03:29:09,359 n4 5415 03:29:06,960 --> 03:29:12,080 i have given the data as 20 right so 5416 03:29:09,359 --> 03:29:14,880 n4.data 5417 03:29:12,080 --> 03:29:17,040 so n4.data i will be getting as 20 so it 5418 03:29:14,880 --> 03:29:18,239 will print the data as 20 right and now 5419 03:29:17,040 --> 03:29:19,680 after that what will happen after 5420 03:29:18,239 --> 03:29:21,840 printing 20 5421 03:29:19,680 --> 03:29:21,840 here 5422 03:29:21,920 --> 03:29:26,640 a is equal to a dot previous so n4 dot 5423 03:29:24,399 --> 03:29:29,040 previous is what 5424 03:29:26,640 --> 03:29:30,960 and 4 dot previous is nothing but n3 5425 03:29:29,040 --> 03:29:32,960 here you can see that 5426 03:29:30,960 --> 03:29:34,880 so once again now a value is n3 so 5427 03:29:32,960 --> 03:29:38,239 n3.data it will print and three dot data 5428 03:29:34,880 --> 03:29:40,000 is 15 after printing the data of n3 it 5429 03:29:38,239 --> 03:29:42,000 will go a is equal to a dot previous 5430 03:29:40,000 --> 03:29:44,479 that is n3 dot previous if you see here 5431 03:29:42,000 --> 03:29:46,479 n3 dot previous it's n2 5432 03:29:44,479 --> 03:29:49,040 so once again it will go n2 so n2 dot 5433 03:29:46,479 --> 03:29:51,600 data is 10 so it will print 10 5434 03:29:49,040 --> 03:29:54,000 so once again a will take the value as 5435 03:29:51,600 --> 03:29:57,359 n2 dot previous so n2 dot previous is 5436 03:29:54,000 --> 03:30:00,160 nothing but n1 so while n1 is not none 5437 03:29:57,359 --> 03:30:00,800 it will print a dot data so n1 dot data 5438 03:30:00,160 --> 03:30:01,760 is 5439 03:30:00,800 --> 03:30:05,120 5 5440 03:30:01,760 --> 03:30:08,000 and after printing the data 5 if you see 5441 03:30:05,120 --> 03:30:12,319 a is equal to n1 dot previous what is my 5442 03:30:08,000 --> 03:30:12,319 n1 dot previous if you will see here 5443 03:30:14,319 --> 03:30:18,720 my n1 dot previous is none 5444 03:30:16,640 --> 03:30:20,800 so 5445 03:30:18,720 --> 03:30:23,040 my a value is none so this while loop 5446 03:30:20,800 --> 03:30:23,760 will end so it will display 5447 03:30:23,040 --> 03:30:26,160 me 5448 03:30:23,760 --> 03:30:28,840 20 15 10 and 5. so this is the basic 5449 03:30:26,160 --> 03:30:31,760 logic of traversal in a doubly linked 5450 03:30:28,840 --> 03:30:33,439 list now let's see the insertion at 5451 03:30:31,760 --> 03:30:35,680 doubly linked list so we will see the 5452 03:30:33,439 --> 03:30:37,600 insertion at beginning first so this is 5453 03:30:35,680 --> 03:30:41,600 my doubly linked list so doubly linked 5454 03:30:37,600 --> 03:30:41,600 list is having two pointers right 5455 03:30:42,080 --> 03:30:47,040 so let's suppose that 5456 03:30:44,640 --> 03:30:48,080 i'm having three nodes here n1 5457 03:30:47,040 --> 03:30:49,760 n2 5458 03:30:48,080 --> 03:30:52,000 and n3 5459 03:30:49,760 --> 03:30:54,239 so n1 next pointer will contain the 5460 03:30:52,000 --> 03:30:57,040 address of n2 node 5461 03:30:54,239 --> 03:30:58,720 similarly end to previous pointer will 5462 03:30:57,040 --> 03:31:00,319 contain the address of 5463 03:30:58,720 --> 03:31:03,120 and one node 5464 03:31:00,319 --> 03:31:05,600 the same goes for n2 and n3 5465 03:31:03,120 --> 03:31:07,920 and here n3 next pointer will contain 5466 03:31:05,600 --> 03:31:10,479 the address as none because there is no 5467 03:31:07,920 --> 03:31:13,359 node available after n3 similarly here 5468 03:31:10,479 --> 03:31:16,000 n1 previous will be none 5469 03:31:13,359 --> 03:31:18,000 so this is my doubly linked list 5470 03:31:16,000 --> 03:31:20,000 now if i want to insert a node at 5471 03:31:18,000 --> 03:31:20,960 beginning so let's suppose this is my 5472 03:31:20,000 --> 03:31:22,800 node 5473 03:31:20,960 --> 03:31:24,800 so how to create a node we have already 5474 03:31:22,800 --> 03:31:27,680 seen it so if i am writing let's suppose 5475 03:31:24,800 --> 03:31:29,680 here nb node at beginning and if i'm 5476 03:31:27,680 --> 03:31:30,880 writing a node 5477 03:31:29,680 --> 03:31:32,800 and inside that i am passing the 5478 03:31:30,880 --> 03:31:34,000 parameter data so this will contain here 5479 03:31:32,800 --> 03:31:34,800 a data 5480 03:31:34,000 --> 03:31:37,520 and 5481 03:31:34,800 --> 03:31:39,920 this pointer will be 5482 03:31:37,520 --> 03:31:40,960 nb dot next right and this pointer will 5483 03:31:39,920 --> 03:31:43,760 be 5484 03:31:40,960 --> 03:31:45,439 nb dot previous so nb dot next is equal 5485 03:31:43,760 --> 03:31:48,319 to none and nb dot previous is equal to 5486 03:31:45,439 --> 03:31:51,200 none initially now what will happen here 5487 03:31:48,319 --> 03:31:52,960 as we know that initially 5488 03:31:51,200 --> 03:31:54,720 my head will be pointing to the n1 right 5489 03:31:52,960 --> 03:31:57,359 because this is my doubly linked list so 5490 03:31:54,720 --> 03:31:58,720 if i want to insert this node then my 5491 03:31:57,359 --> 03:32:00,960 head will be changed right because head 5492 03:31:58,720 --> 03:32:02,479 will always point to the first node so 5493 03:32:00,960 --> 03:32:04,239 what i will do here 5494 03:32:02,479 --> 03:32:06,720 so let me connect this node i will keep 5495 03:32:04,239 --> 03:32:09,840 the name here as nb so let's suppose 5496 03:32:06,720 --> 03:32:09,840 this is my nb here 5497 03:32:10,000 --> 03:32:14,000 this is by nb and initially it is not 5498 03:32:12,560 --> 03:32:16,640 linked so 5499 03:32:14,000 --> 03:32:18,720 if it is not linked then n1 dot previous 5500 03:32:16,640 --> 03:32:21,200 is initially none right 5501 03:32:18,720 --> 03:32:23,200 now i want to link it so let's see first 5502 03:32:21,200 --> 03:32:26,239 what i will do i will create a temporary 5503 03:32:23,200 --> 03:32:27,920 variable a is equal to self dot head 5504 03:32:26,239 --> 03:32:30,000 and i know that my head is pointing to 5505 03:32:27,920 --> 03:32:32,479 n1 as of now but i want to change this 5506 03:32:30,000 --> 03:32:35,359 head and i want a link established 5507 03:32:32,479 --> 03:32:37,840 between nv and n1 so what i will do here 5508 03:32:35,359 --> 03:32:40,960 after creating a temporary variable a i 5509 03:32:37,840 --> 03:32:44,000 will assign self.head into this and now 5510 03:32:40,960 --> 03:32:46,399 this is my a right so my a is equal to 5511 03:32:44,000 --> 03:32:47,439 self.head so i will write here a dot 5512 03:32:46,399 --> 03:32:50,000 previous 5513 03:32:47,439 --> 03:32:52,399 so if i'm writing here a dot previous is 5514 03:32:50,000 --> 03:32:54,160 equal to nb so what is a 5515 03:32:52,399 --> 03:32:55,680 a is nothing but head so if i'm writing 5516 03:32:54,160 --> 03:32:57,920 a dot previous this is the previous 5517 03:32:55,680 --> 03:33:00,960 right initially previous is equal to 5518 03:32:57,920 --> 03:33:03,040 none so a dot previous 5519 03:33:00,960 --> 03:33:05,040 is equal to nb that means now this 5520 03:33:03,040 --> 03:33:07,760 previous will contain the address of nb 5521 03:33:05,040 --> 03:33:09,680 so now it it will not be none so i will 5522 03:33:07,760 --> 03:33:10,960 establish a link here right 5523 03:33:09,680 --> 03:33:13,200 now after this 5524 03:33:10,960 --> 03:33:14,800 i will write here nb dot next so this is 5525 03:33:13,200 --> 03:33:16,640 my nv 5526 03:33:14,800 --> 03:33:19,840 this is my next 5527 03:33:16,640 --> 03:33:19,840 and this is my previous 5528 03:33:19,920 --> 03:33:27,439 right so here if i'm writing nb dot next 5529 03:33:24,319 --> 03:33:29,920 is equal to a that means 5530 03:33:27,439 --> 03:33:31,920 again i have created a link so here you 5531 03:33:29,920 --> 03:33:34,160 can see that nb dot next is equal to a 5532 03:33:31,920 --> 03:33:37,200 that means my link has been 5533 03:33:34,160 --> 03:33:40,000 created so it means that nb dot next 5534 03:33:37,200 --> 03:33:42,880 will contain the address of my n1 node 5535 03:33:40,000 --> 03:33:44,800 now after this this is my head right so 5536 03:33:42,880 --> 03:33:46,800 once the link has been established my 5537 03:33:44,800 --> 03:33:48,960 head will change so for that what i will 5538 03:33:46,800 --> 03:33:51,200 write here i will write here self 5539 03:33:48,960 --> 03:33:54,640 dot head 5540 03:33:51,200 --> 03:33:55,600 self dot head will be equal to 5541 03:33:54,640 --> 03:33:57,520 nb 5542 03:33:55,600 --> 03:34:00,239 so this head will be changing here and 5543 03:33:57,520 --> 03:34:01,920 this head will come here 5544 03:34:00,239 --> 03:34:04,399 so you can see that now head is pointing 5545 03:34:01,920 --> 03:34:07,040 to nb and b dot next contains the 5546 03:34:04,399 --> 03:34:09,200 address of n1 n1 dot previous will 5547 03:34:07,040 --> 03:34:10,960 contain the address of nb 5548 03:34:09,200 --> 03:34:13,120 and we know that nb dot previous is 5549 03:34:10,960 --> 03:34:14,720 already done so this will be done right 5550 03:34:13,120 --> 03:34:16,880 while creating the node so this is the 5551 03:34:14,720 --> 03:34:18,479 basic idea about insertion of node at 5552 03:34:16,880 --> 03:34:20,000 beginning now let's see it in coding 5553 03:34:18,479 --> 03:34:24,239 example 5554 03:34:20,000 --> 03:34:24,239 so this is my doubly linked list and 5555 03:34:24,479 --> 03:34:28,399 in the double english class itself i 5556 03:34:26,319 --> 03:34:30,640 create a function def and i will write 5557 03:34:28,399 --> 03:34:32,960 here insertion 5558 03:34:30,640 --> 03:34:37,319 at beginning 5559 03:34:32,960 --> 03:34:37,319 insertion at beginning 5560 03:34:42,640 --> 03:34:47,760 so what i will do here i'll pass the 5561 03:34:44,720 --> 03:34:47,760 parameter data right 5562 03:34:48,000 --> 03:34:52,399 because data value will be given by 5563 03:34:50,720 --> 03:34:54,080 me 5564 03:34:52,399 --> 03:34:55,520 so after this creating function first i 5565 03:34:54,080 --> 03:34:57,279 have to create a node so let me write 5566 03:34:55,520 --> 03:34:58,560 the node name that suppose node at 5567 03:34:57,279 --> 03:34:59,760 starting ns 5568 03:34:58,560 --> 03:35:02,000 is equal to 5569 03:34:59,760 --> 03:35:02,000 node 5570 03:35:02,720 --> 03:35:05,760 and here i am giving 5571 03:35:05,840 --> 03:35:11,040 data and after creating a node 5572 03:35:09,040 --> 03:35:13,760 i will create a temporary variable a 5573 03:35:11,040 --> 03:35:15,840 inside that i will assign self.head 5574 03:35:13,760 --> 03:35:18,080 so initially myself.head is pointing to 5575 03:35:15,840 --> 03:35:20,560 n1 so after creating a temporary 5576 03:35:18,080 --> 03:35:23,200 variable a and assigning the self.head 5577 03:35:20,560 --> 03:35:24,560 value inside a temporary variable i will 5578 03:35:23,200 --> 03:35:27,520 write here 5579 03:35:24,560 --> 03:35:29,520 a dot previous 5580 03:35:27,520 --> 03:35:33,120 so what will a dot previous will give me 5581 03:35:29,520 --> 03:35:34,000 a dot previous will give me here an s 5582 03:35:33,120 --> 03:35:37,120 right 5583 03:35:34,000 --> 03:35:40,800 so that means i am linking the n node 5584 03:35:37,120 --> 03:35:44,720 with the ns node node at starting 5585 03:35:40,800 --> 03:35:44,720 and then i will write here ns dot next 5586 03:35:45,120 --> 03:35:50,080 so node that i've created right its next 5587 03:35:47,600 --> 03:35:51,840 pointer will assign to a 5588 03:35:50,080 --> 03:35:55,760 and after that i have to change the head 5589 03:35:51,840 --> 03:35:55,760 so i'll write here self dot head 5590 03:35:56,239 --> 03:36:00,399 is equal to 5591 03:35:58,399 --> 03:36:03,840 ns right because ns will be my first 5592 03:36:00,399 --> 03:36:05,439 node so head must assign to ns 5593 03:36:03,840 --> 03:36:08,479 now after this once again i will write 5594 03:36:05,439 --> 03:36:08,479 here the function name 5595 03:36:13,920 --> 03:36:19,520 so i will write here dll 5596 03:36:16,160 --> 03:36:22,239 dot insertion at beginning 5597 03:36:19,520 --> 03:36:24,720 and let's suppose i'm inserting a value 5598 03:36:22,239 --> 03:36:26,080 here too 5599 03:36:24,720 --> 03:36:27,920 and if i want to do the forward 5600 03:36:26,080 --> 03:36:30,560 traversal then i will just copy and 5601 03:36:27,920 --> 03:36:30,560 paste this 5602 03:36:30,960 --> 03:36:34,479 so on running 5603 03:36:32,239 --> 03:36:37,359 you can see that i am getting the output 5604 03:36:34,479 --> 03:36:38,399 but once again i have to write the print 5605 03:36:37,359 --> 03:36:41,680 to see 5606 03:36:38,399 --> 03:36:43,279 the output into the next line 5607 03:36:41,680 --> 03:36:45,279 so if you see here you can see that a 5608 03:36:43,279 --> 03:36:47,279 creation of new node has been done and 5609 03:36:45,279 --> 03:36:49,120 the data element is two now what if i 5610 03:36:47,279 --> 03:36:50,560 want to do the backward traversal you 5611 03:36:49,120 --> 03:36:53,040 will see that my logic will work for 5612 03:36:50,560 --> 03:36:54,640 backward traversal also 5613 03:36:53,040 --> 03:36:56,160 so if i'm writing here then you can see 5614 03:36:54,640 --> 03:36:57,840 that i can do the backward traversal 5615 03:36:56,160 --> 03:37:00,000 also so you can see that i can also do 5616 03:36:57,840 --> 03:37:01,439 the backward traversal with this logic 5617 03:37:00,000 --> 03:37:03,359 so this is the basic idea about 5618 03:37:01,439 --> 03:37:05,279 insertion at the beginning 5619 03:37:03,359 --> 03:37:07,200 now let's see the insertion at end so 5620 03:37:05,279 --> 03:37:09,200 let's suppose 5621 03:37:07,200 --> 03:37:12,160 this is my n1 node 5622 03:37:09,200 --> 03:37:16,279 this is my n2 node 5623 03:37:12,160 --> 03:37:16,279 and this is my n3 node 5624 03:37:17,680 --> 03:37:23,439 and this is the node that i will create 5625 03:37:20,239 --> 03:37:24,880 let me give the name as any here 5626 03:37:23,439 --> 03:37:27,760 so how to create a node so when i'm 5627 03:37:24,880 --> 03:37:28,560 writing here any is equal to node 5628 03:37:27,760 --> 03:37:30,560 and 5629 03:37:28,560 --> 03:37:32,319 i'm passing data inside this node so 5630 03:37:30,560 --> 03:37:34,720 what will happen here any node will be 5631 03:37:32,319 --> 03:37:34,720 created 5632 03:37:37,359 --> 03:37:42,239 and any dot next will be none 5633 03:37:40,160 --> 03:37:44,000 and any dot previous will be none 5634 03:37:42,239 --> 03:37:45,760 initially when i am creating this node i 5635 03:37:44,000 --> 03:37:48,560 am not linking this node to any other 5636 03:37:45,760 --> 03:37:50,479 node now after this let's see understand 5637 03:37:48,560 --> 03:37:51,760 the logic so before understanding the 5638 03:37:50,479 --> 03:37:55,720 logic 5639 03:37:51,760 --> 03:37:55,720 let me connect these nodes 5640 03:37:59,840 --> 03:38:04,160 so initially my n1 dot previous will be 5641 03:38:02,160 --> 03:38:05,200 none and n3 dot previous will be also 5642 03:38:04,160 --> 03:38:07,120 none 5643 03:38:05,200 --> 03:38:08,640 and this is my any node right i am not 5644 03:38:07,120 --> 03:38:10,880 connecting this any node with this 5645 03:38:08,640 --> 03:38:12,640 doubly linked list as of now so let's 5646 03:38:10,880 --> 03:38:14,319 see the logic here so initially once 5647 03:38:12,640 --> 03:38:16,239 again i will create a temporary variable 5648 03:38:14,319 --> 03:38:18,479 a and inside that i will assign 5649 03:38:16,239 --> 03:38:20,239 self.head 5650 03:38:18,479 --> 03:38:22,160 once again the same logic because i will 5651 03:38:20,239 --> 03:38:25,120 use this a for the iteration so i don't 5652 03:38:22,160 --> 03:38:27,279 want to change the head value now 5653 03:38:25,120 --> 03:38:29,680 i will use a while loop so i will write 5654 03:38:27,279 --> 03:38:32,399 here a dot next is not none so a dot 5655 03:38:29,680 --> 03:38:34,160 next is what this is my head right a is 5656 03:38:32,399 --> 03:38:37,520 equal to self dot head this is my head 5657 03:38:34,160 --> 03:38:40,720 so a dot next is pointing to n2 so it's 5658 03:38:37,520 --> 03:38:43,279 not none so this is my a dot next so a 5659 03:38:40,720 --> 03:38:47,279 dot next is not none right so my a is 5660 03:38:43,279 --> 03:38:48,560 equal to a dot next so my a will 5661 03:38:47,279 --> 03:38:50,000 jump here 5662 03:38:48,560 --> 03:38:52,640 similarly once again this is a while 5663 03:38:50,000 --> 03:38:54,800 loop again a dot next is not none right 5664 03:38:52,640 --> 03:38:57,120 because a dot next is 5665 03:38:54,800 --> 03:38:59,520 containing the address of n3 node so 5666 03:38:57,120 --> 03:39:02,160 once again this while loop will run a is 5667 03:38:59,520 --> 03:39:04,399 equal to a dot next so my new a will be 5668 03:39:02,160 --> 03:39:06,800 coming here now once again if you see a 5669 03:39:04,399 --> 03:39:08,960 dot next is not none but here a dot next 5670 03:39:06,800 --> 03:39:11,040 is none right because as of now this is 5671 03:39:08,960 --> 03:39:13,760 not linked with any node 5672 03:39:11,040 --> 03:39:15,920 so this while loop will end here 5673 03:39:13,760 --> 03:39:18,239 so after that what i will do here a dot 5674 03:39:15,920 --> 03:39:20,960 next is none my here right i will assign 5675 03:39:18,239 --> 03:39:24,560 any into a dot next so here instead of 5676 03:39:20,960 --> 03:39:26,000 none any will be connected right this is 5677 03:39:24,560 --> 03:39:28,640 my any right 5678 03:39:26,000 --> 03:39:30,880 now any now coming to the any any dot 5679 03:39:28,640 --> 03:39:33,359 previous is none right so what i will do 5680 03:39:30,880 --> 03:39:35,840 here once again i will here write any 5681 03:39:33,359 --> 03:39:37,520 dot previous is equal to a when i'm 5682 03:39:35,840 --> 03:39:40,000 writing here any dot previous is equal 5683 03:39:37,520 --> 03:39:42,160 to a that means any dot previous this is 5684 03:39:40,000 --> 03:39:44,560 the previous pointer of any so it is 5685 03:39:42,160 --> 03:39:46,800 connecting with n3 that means any dot 5686 03:39:44,560 --> 03:39:48,640 previous will contain the address of n3 5687 03:39:46,800 --> 03:39:50,319 node so this is the basic logic for 5688 03:39:48,640 --> 03:39:52,479 insertion at end now let's see the 5689 03:39:50,319 --> 03:39:54,080 coding example so let's see the coding 5690 03:39:52,479 --> 03:39:56,640 part so 5691 03:39:54,080 --> 03:39:58,560 here just like i have created insertion 5692 03:39:56,640 --> 03:40:00,239 at the beginning function once again i 5693 03:39:58,560 --> 03:40:02,880 will create a function 5694 03:40:00,239 --> 03:40:02,880 df 5695 03:40:03,760 --> 03:40:07,520 and i will write here insertion 5696 03:40:08,800 --> 03:40:16,600 at end 5697 03:40:11,600 --> 03:40:16,600 and i will pass the parameter here data 5698 03:40:18,239 --> 03:40:22,000 now what will happen here 5699 03:40:19,920 --> 03:40:24,319 i will create a node any is equal to 5700 03:40:22,000 --> 03:40:24,319 node 5701 03:40:25,439 --> 03:40:28,399 and i will pass data here right inside a 5702 03:40:27,359 --> 03:40:30,080 node 5703 03:40:28,399 --> 03:40:31,520 now after creating node once again i 5704 03:40:30,080 --> 03:40:33,120 have to create a temporary variable so i 5705 03:40:31,520 --> 03:40:34,080 will create temporary variable a is 5706 03:40:33,120 --> 03:40:36,960 equal to 5707 03:40:34,080 --> 03:40:36,960 self.head 5708 03:40:39,199 --> 03:40:43,199 now 5709 03:40:40,960 --> 03:40:44,720 since i have to insert at the end that 5710 03:40:43,199 --> 03:40:46,640 means i have to traverse through each 5711 03:40:44,720 --> 03:40:48,720 node right so for that i will using a 5712 03:40:46,640 --> 03:40:50,560 while loop so i'll write here while a 5713 03:40:48,720 --> 03:40:53,279 dot next 5714 03:40:50,560 --> 03:40:53,279 is not none 5715 03:40:57,920 --> 03:41:03,359 i will assign a value is equal to a dot 5716 03:41:01,920 --> 03:41:04,960 next 5717 03:41:03,359 --> 03:41:07,120 now after this once i'm traversing 5718 03:41:04,960 --> 03:41:09,279 through each node what i will do i have 5719 03:41:07,120 --> 03:41:11,040 to connect the last node so after this 5720 03:41:09,279 --> 03:41:14,000 how to connect the last node so i'll 5721 03:41:11,040 --> 03:41:16,560 write here a dot next 5722 03:41:14,000 --> 03:41:18,399 is equal to 5723 03:41:16,560 --> 03:41:21,600 n of e 5724 03:41:18,399 --> 03:41:23,359 right and i will also write here any dot 5725 03:41:21,600 --> 03:41:24,560 previous 5726 03:41:23,359 --> 03:41:26,800 is equal to 5727 03:41:24,560 --> 03:41:28,960 a so the connection has been done now 5728 03:41:26,800 --> 03:41:30,960 now let me execute this 5729 03:41:28,960 --> 03:41:33,920 so for executing 5730 03:41:30,960 --> 03:41:33,920 what i will do here 5731 03:41:34,080 --> 03:41:39,520 let me copy this 5732 03:41:36,479 --> 03:41:41,760 and i will paste here 5733 03:41:39,520 --> 03:41:42,880 and what's the name here insertion at 5734 03:41:41,760 --> 03:41:44,000 end 5735 03:41:42,880 --> 03:41:45,600 so 5736 03:41:44,000 --> 03:41:47,520 instead of beginning i will write here 5737 03:41:45,600 --> 03:41:49,920 end 5738 03:41:47,520 --> 03:41:52,000 and instead of two value let's suppose 5739 03:41:49,920 --> 03:41:55,840 at end i want to do the insertion right 5740 03:41:52,000 --> 03:41:58,319 so let me give the value as 25 here 5741 03:41:55,840 --> 03:41:59,680 if i'm executing it 5742 03:41:58,319 --> 03:42:02,080 then you can see that i'm getting the 5743 03:41:59,680 --> 03:42:02,960 output 5744 03:42:02,080 --> 03:42:05,359 since 5745 03:42:02,960 --> 03:42:07,040 i have to give the space here to get the 5746 03:42:05,359 --> 03:42:08,560 output in the next line so i'll use 5747 03:42:07,040 --> 03:42:10,960 print function 5748 03:42:08,560 --> 03:42:14,560 so you can see that a node has been 5749 03:42:10,960 --> 03:42:16,960 inserted whose data is 25 so this is the 5750 03:42:14,560 --> 03:42:19,920 logic for insertion at end now let's see 5751 03:42:16,960 --> 03:42:23,720 the insertion at any specified node so 5752 03:42:19,920 --> 03:42:23,720 this is my node n1 5753 03:42:24,319 --> 03:42:27,840 this is my node n2 5754 03:42:31,279 --> 03:42:33,520 and 5755 03:42:34,080 --> 03:42:39,840 this is my node n3 5756 03:42:35,920 --> 03:42:39,840 and this is my node n4 5757 03:42:41,760 --> 03:42:46,640 right so if you see here then 5758 03:42:44,319 --> 03:42:49,840 n1 dot previous will be pointing to none 5759 03:42:46,640 --> 03:42:51,680 and then n1 dot next will be equal to n2 5760 03:42:49,840 --> 03:42:53,920 similarly n2 dot previous will be equal 5761 03:42:51,680 --> 03:42:56,399 to n1 so in this way there will be a 5762 03:42:53,920 --> 03:42:56,399 connection 5763 03:42:59,040 --> 03:43:04,160 and here n4 dot next will be equal to 5764 03:43:00,960 --> 03:43:05,600 none so here i want to insert the node 5765 03:43:04,160 --> 03:43:06,399 so let's suppose this is the position 5766 03:43:05,600 --> 03:43:07,439 one 5767 03:43:06,399 --> 03:43:08,319 two 5768 03:43:07,439 --> 03:43:10,560 three 5769 03:43:08,319 --> 03:43:12,640 and four so what i will do here i will 5770 03:43:10,560 --> 03:43:15,760 insert a node at position three so my 5771 03:43:12,640 --> 03:43:15,760 position is equal to three 5772 03:43:16,399 --> 03:43:20,160 so for inserting a node at particular 5773 03:43:18,479 --> 03:43:21,760 position first i have to create a node 5774 03:43:20,160 --> 03:43:24,560 so how to create a node so i'll write 5775 03:43:21,760 --> 03:43:26,000 here nib is equal to node 5776 03:43:24,560 --> 03:43:27,600 and then i will pass 5777 03:43:26,000 --> 03:43:30,319 data inside this 5778 03:43:27,600 --> 03:43:33,359 so my node has been created this is my 5779 03:43:30,319 --> 03:43:34,880 node which will be having a data 5780 03:43:33,359 --> 03:43:37,199 and 5781 03:43:34,880 --> 03:43:38,080 here an ib dot previous will be equal to 5782 03:43:37,199 --> 03:43:40,000 none 5783 03:43:38,080 --> 03:43:42,319 and an ib dot next will be also equal to 5784 03:43:40,000 --> 03:43:44,239 none initially right 5785 03:43:42,319 --> 03:43:45,680 so this is my creation of node now my 5786 03:43:44,239 --> 03:43:48,239 head will be always 5787 03:43:45,680 --> 03:43:50,479 pointing to the first node so this is my 5788 03:43:48,239 --> 03:43:52,560 head so i write here a is equal to self 5789 03:43:50,479 --> 03:43:55,840 dot head 5790 03:43:52,560 --> 03:43:58,000 which will be pointing to n1 initially 5791 03:43:55,840 --> 03:44:00,319 so after this what i have to do so if i 5792 03:43:58,000 --> 03:44:03,040 want to insert a node at any particular 5793 03:44:00,319 --> 03:44:05,279 position so this is a linked list i have 5794 03:44:03,040 --> 03:44:07,040 to traverse right if i want to insert a 5795 03:44:05,279 --> 03:44:09,279 node at position three so i have to go 5796 03:44:07,040 --> 03:44:11,600 through position one two two then only i 5797 03:44:09,279 --> 03:44:13,359 can jump to two to three right so that's 5798 03:44:11,600 --> 03:44:15,040 why i've used a for loop here and i've 5799 03:44:13,359 --> 03:44:17,600 written here position minus one so 5800 03:44:15,040 --> 03:44:20,720 position is here 3 so 3 minus 1 will be 5801 03:44:17,600 --> 03:44:22,319 2 so for i in range 1 comma 2 so as we 5802 03:44:20,720 --> 03:44:25,600 know that in python this 2 will be 5803 03:44:22,319 --> 03:44:27,439 excluded that means for i is equal to 1 5804 03:44:25,600 --> 03:44:28,720 my value will be a is equal to a dot 5805 03:44:27,439 --> 03:44:30,239 next 5806 03:44:28,720 --> 03:44:31,760 that means iteration will be done only 5807 03:44:30,239 --> 03:44:34,160 for one time 5808 03:44:31,760 --> 03:44:35,760 so what will happen here so a dot next 5809 03:44:34,160 --> 03:44:37,520 is this right 5810 03:44:35,760 --> 03:44:39,359 so what will happen here 5811 03:44:37,520 --> 03:44:41,439 for i is equal to 1 5812 03:44:39,359 --> 03:44:42,960 my a is equal to a dot next so this will 5813 03:44:41,439 --> 03:44:45,520 be my a 5814 03:44:42,960 --> 03:44:47,680 now after this if you see here i have 5815 03:44:45,520 --> 03:44:48,720 written here nib dot previous is equal 5816 03:44:47,680 --> 03:44:50,640 to a 5817 03:44:48,720 --> 03:44:52,800 so this is my nib 5818 03:44:50,640 --> 03:44:55,120 dot previous 5819 03:44:52,800 --> 03:44:56,720 which is equal to none initially when i 5820 03:44:55,120 --> 03:45:00,640 am creating a node because i didn't link 5821 03:44:56,720 --> 03:45:02,560 at that time right so if you see here 5822 03:45:00,640 --> 03:45:03,520 i have written an ib dot previous is 5823 03:45:02,560 --> 03:45:05,800 equal to 5824 03:45:03,520 --> 03:45:08,160 a so that means i will connect this 5825 03:45:05,800 --> 03:45:10,640 nib.previous with this 5826 03:45:08,160 --> 03:45:13,279 n2 so after this i will create a 5827 03:45:10,640 --> 03:45:14,880 connection between the nodes so how will 5828 03:45:13,279 --> 03:45:16,800 we connect 5829 03:45:14,880 --> 03:45:18,640 so this is my node 5830 03:45:16,800 --> 03:45:21,960 whose name is nib 5831 03:45:18,640 --> 03:45:21,960 and nib.next 5832 03:45:22,080 --> 03:45:24,560 is 5833 03:45:22,880 --> 03:45:27,359 none 5834 03:45:24,560 --> 03:45:29,760 and an dot tvs 5835 03:45:27,359 --> 03:45:31,920 is also known initially right but here 5836 03:45:29,760 --> 03:45:32,880 if i'm writing and ib dot previous is 5837 03:45:31,920 --> 03:45:33,680 equal to 5838 03:45:32,880 --> 03:45:35,920 a 5839 03:45:33,680 --> 03:45:38,080 so an ib dot previous was none initially 5840 03:45:35,920 --> 03:45:40,000 but if i'm writing an ib dot previous is 5841 03:45:38,080 --> 03:45:41,359 equal to a that means what will happen 5842 03:45:40,000 --> 03:45:43,279 here 5843 03:45:41,359 --> 03:45:45,920 this link will 5844 03:45:43,279 --> 03:45:45,920 be like this 5845 03:45:47,439 --> 03:45:51,120 so the link will be 5846 03:45:48,880 --> 03:45:53,520 generated like this right and ib dot 5847 03:45:51,120 --> 03:45:55,920 previous 5848 03:45:53,520 --> 03:45:58,160 and ib dot previous 5849 03:45:55,920 --> 03:46:00,319 is equal to a so now this link has been 5850 03:45:58,160 --> 03:46:02,239 generated with 5851 03:46:00,319 --> 03:46:04,399 n2 right 5852 03:46:02,239 --> 03:46:06,880 so that means an ib dot previous will 5853 03:46:04,399 --> 03:46:09,439 contain the address of n2 node let's 5854 03:46:06,880 --> 03:46:12,319 suppose that address of n2 is 1000 so it 5855 03:46:09,439 --> 03:46:14,640 will contain the address of 1000 5856 03:46:12,319 --> 03:46:17,439 now coming to the next and ib dot next 5857 03:46:14,640 --> 03:46:19,600 is equal to a dot next so my nib.next is 5858 03:46:17,439 --> 03:46:21,680 equal to none right what is a dot next 5859 03:46:19,600 --> 03:46:23,279 my a is here right so a dot next is 5860 03:46:21,680 --> 03:46:25,359 nothing but it contains the address of 5861 03:46:23,279 --> 03:46:27,600 n3 right so if i'm writing here an ib 5862 03:46:25,359 --> 03:46:32,600 dot next is equal to a dot next that 5863 03:46:27,600 --> 03:46:32,600 means i am creating the link here 5864 03:46:36,239 --> 03:46:39,600 so let's suppose this n3 is containing a 5865 03:46:38,080 --> 03:46:42,960 address of 2000 5866 03:46:39,600 --> 03:46:45,359 then an ib dot next will contain address 5867 03:46:42,960 --> 03:46:47,680 now 2000 year 5868 03:46:45,359 --> 03:46:49,840 what it now after this what will happen 5869 03:46:47,680 --> 03:46:51,920 here if i'm writing here a dot next dot 5870 03:46:49,840 --> 03:46:54,319 previous so this is my a dot next so if 5871 03:46:51,920 --> 03:46:56,880 i'm writing here a dot next dot previous 5872 03:46:54,319 --> 03:46:58,720 is equal to nib so that means another 5873 03:46:56,880 --> 03:47:01,040 link has been generated 5874 03:46:58,720 --> 03:47:03,279 let me remove this 5875 03:47:01,040 --> 03:47:05,439 so another link will be generated from 5876 03:47:03,279 --> 03:47:07,120 here and it will contain the address of 5877 03:47:05,439 --> 03:47:09,040 this node so let's suppose the address 5878 03:47:07,120 --> 03:47:11,199 of this node is 1500 so this will 5879 03:47:09,040 --> 03:47:13,680 contain 1500 here 5880 03:47:11,199 --> 03:47:16,239 now if i'm writing here a dot next is 5881 03:47:13,680 --> 03:47:17,920 equal to nib so this is my a right a dot 5882 03:47:16,239 --> 03:47:19,840 next will be here so now what will 5883 03:47:17,920 --> 03:47:23,520 happen here if i'm writing here a dot 5884 03:47:19,840 --> 03:47:25,439 next is equal to nib so that means 5885 03:47:23,520 --> 03:47:28,080 so that means a dot next will contain 5886 03:47:25,439 --> 03:47:30,319 the address of nib that is 1500 so 1500 5887 03:47:28,080 --> 03:47:32,880 will be here so in this way you can see 5888 03:47:30,319 --> 03:47:34,640 that i've inserted a specified node in 5889 03:47:32,880 --> 03:47:36,239 doubly linked list so this is the basic 5890 03:47:34,640 --> 03:47:37,920 logic now let's see in the practical 5891 03:47:36,239 --> 03:47:40,640 example 5892 03:47:37,920 --> 03:47:43,120 so now we'll see the logic encoding so i 5893 03:47:40,640 --> 03:47:45,680 will create one more function here def 5894 03:47:43,120 --> 03:47:47,760 and let's say insertion 5895 03:47:45,680 --> 03:47:50,000 at specified node i will give the name 5896 03:47:47,760 --> 03:47:50,000 here 5897 03:47:53,840 --> 03:47:57,600 and here i will give two parameters i 5898 03:47:55,439 --> 03:47:59,359 will also give position because at which 5899 03:47:57,600 --> 03:48:01,680 particular position i want to insert the 5900 03:47:59,359 --> 03:48:04,080 node so i'll write here self 5901 03:48:01,680 --> 03:48:06,800 position 5902 03:48:04,080 --> 03:48:06,800 and then data 5903 03:48:07,439 --> 03:48:11,040 now what will happen here first i will 5904 03:48:09,199 --> 03:48:14,000 create a node so i'll write an ib that 5905 03:48:11,040 --> 03:48:15,760 means node in between 5906 03:48:14,000 --> 03:48:18,560 you can take any node name 5907 03:48:15,760 --> 03:48:20,239 so i'll write here node data 5908 03:48:18,560 --> 03:48:21,600 now after creating node what i will do 5909 03:48:20,239 --> 03:48:25,359 once again i will create a temporary 5910 03:48:21,600 --> 03:48:27,279 variable a is equal to self.head 5911 03:48:25,359 --> 03:48:28,880 so that my head value shouldn't be 5912 03:48:27,279 --> 03:48:30,960 changed because i don't want to change 5913 03:48:28,880 --> 03:48:32,880 my head value but i will use it in the 5914 03:48:30,960 --> 03:48:35,040 iteration 5915 03:48:32,880 --> 03:48:39,279 so after creating a i'll create a loop 5916 03:48:35,040 --> 03:48:39,279 so i'll write here for i in range 5917 03:48:40,560 --> 03:48:45,760 and then i will write here one 5918 03:48:42,399 --> 03:48:45,760 comma position minus one 5919 03:48:47,120 --> 03:48:52,239 and inside that i will write a is equal 5920 03:48:49,040 --> 03:48:52,239 to a dot next 5921 03:48:53,600 --> 03:48:57,680 so what does this mean so let's suppose 5922 03:48:55,359 --> 03:48:59,680 that if i want to insert a node at any 5923 03:48:57,680 --> 03:49:01,279 specified position so before that i have 5924 03:48:59,680 --> 03:49:03,359 to traverse through each node right so 5925 03:49:01,279 --> 03:49:05,439 this is the logic that i've used using 5926 03:49:03,359 --> 03:49:08,239 for loop so after that what i will do 5927 03:49:05,439 --> 03:49:12,239 here i'll write here an ib 5928 03:49:08,239 --> 03:49:13,920 dot previous is equal to a so i am 5929 03:49:12,239 --> 03:49:15,920 writing this because i want a link 5930 03:49:13,920 --> 03:49:17,680 connection between the nodes and after 5931 03:49:15,920 --> 03:49:20,000 writing this i will write here an ib dot 5932 03:49:17,680 --> 03:49:20,000 next 5933 03:49:20,479 --> 03:49:25,199 is equal to 5934 03:49:22,000 --> 03:49:28,960 a dot next 5935 03:49:25,199 --> 03:49:28,960 then i will write here a dot next 5936 03:49:29,120 --> 03:49:34,640 dot previous 5937 03:49:31,359 --> 03:49:36,399 is equal to nib 5938 03:49:34,640 --> 03:49:38,160 since this is a doubly linked list so we 5939 03:49:36,399 --> 03:49:41,600 have to connect the previous pointer 5940 03:49:38,160 --> 03:49:44,160 also and the next pointer also 5941 03:49:41,600 --> 03:49:45,439 now i will write after this a dot next 5942 03:49:44,160 --> 03:49:48,319 is equal to 5943 03:49:45,439 --> 03:49:52,080 nib so this is my connection 5944 03:49:48,319 --> 03:49:54,479 now once the connection has been done 5945 03:49:52,080 --> 03:49:54,479 let me 5946 03:49:55,120 --> 03:49:58,960 print it out so what's the name of this 5947 03:49:57,680 --> 03:50:02,399 function 5948 03:49:58,960 --> 03:50:05,040 insertion at specified node 5949 03:50:02,399 --> 03:50:07,840 so i'll write here at specie 5950 03:50:05,040 --> 03:50:07,840 fight 5951 03:50:08,640 --> 03:50:10,960 node 5952 03:50:11,600 --> 03:50:16,080 and let's suppose that at position 3 i 5953 03:50:13,920 --> 03:50:18,080 want to insert right 5954 03:50:16,080 --> 03:50:20,479 so at position 3 what value i want to 5955 03:50:18,080 --> 03:50:22,000 give let's suppose at position 3 i want 5956 03:50:20,479 --> 03:50:24,399 to give the value as 7 so i'll write 5957 03:50:22,000 --> 03:50:26,720 here 7 5958 03:50:24,399 --> 03:50:28,560 and i'll write here print so that i can 5959 03:50:26,720 --> 03:50:30,479 get the output this time 5960 03:50:28,560 --> 03:50:32,560 in the next line 5961 03:50:30,479 --> 03:50:34,080 so let me execute it 5962 03:50:32,560 --> 03:50:35,600 so on execution you can see that at 5963 03:50:34,080 --> 03:50:37,920 position three 5964 03:50:35,600 --> 03:50:39,840 my new node has been created fourth data 5965 03:50:37,920 --> 03:50:41,439 element is seven so this is the basic 5966 03:50:39,840 --> 03:50:44,080 logic for the instruction at different 5967 03:50:41,439 --> 03:50:46,000 positions in doubly linked list 5968 03:50:44,080 --> 03:50:46,880 so now let's see the deletion in w link 5969 03:50:46,000 --> 03:50:49,840 list 5970 03:50:46,880 --> 03:50:53,120 so my doubly linked list 5971 03:50:49,840 --> 03:50:53,120 let's suppose look like this 5972 03:50:57,279 --> 03:51:01,760 and here i'm having three nodes 5973 03:51:00,720 --> 03:51:02,720 n1 5974 03:51:01,760 --> 03:51:03,680 n2 5975 03:51:02,720 --> 03:51:05,520 n3 5976 03:51:03,680 --> 03:51:08,239 and each node will contain a data and 5977 03:51:05,520 --> 03:51:08,239 two pointers 5978 03:51:08,800 --> 03:51:13,040 so this pointer that is n1 dot previous 5979 03:51:10,800 --> 03:51:15,040 will be pointing to none because i don't 5980 03:51:13,040 --> 03:51:17,680 have any node before this right 5981 03:51:15,040 --> 03:51:19,920 similarly for n3.next this will be also 5982 03:51:17,680 --> 03:51:19,920 none 5983 03:51:20,479 --> 03:51:26,479 now this pointer that is n1.next 5984 03:51:24,160 --> 03:51:29,040 will contain the address of n2 5985 03:51:26,479 --> 03:51:31,760 right similarly n2.previous will contain 5986 03:51:29,040 --> 03:51:34,160 the address of n1 so we know all these 5987 03:51:31,760 --> 03:51:36,000 things and we have already discussed it 5988 03:51:34,160 --> 03:51:38,399 now let's see the logic if you want to 5989 03:51:36,000 --> 03:51:40,960 delete this node at beginning then what 5990 03:51:38,399 --> 03:51:42,640 will be the logic so this is my n1 and 5991 03:51:40,960 --> 03:51:45,040 this is my first node so head will be 5992 03:51:42,640 --> 03:51:46,720 assigned you so what i will write here i 5993 03:51:45,040 --> 03:51:48,640 will write here a is equal to self dot 5994 03:51:46,720 --> 03:51:49,840 head first i will create a temporary 5995 03:51:48,640 --> 03:51:52,560 variable a 5996 03:51:49,840 --> 03:51:55,520 and inside that i will assign self dot 5997 03:51:52,560 --> 03:51:57,920 head now what will happen here 5998 03:51:55,520 --> 03:52:00,800 after that i will write self dot head is 5999 03:51:57,920 --> 03:52:03,359 equal to a dot next i know this is my a 6000 03:52:00,800 --> 03:52:06,160 so a dot next i am assigning into self 6001 03:52:03,359 --> 03:52:07,359 dot head right so that means now my self 6002 03:52:06,160 --> 03:52:09,600 dot head 6003 03:52:07,359 --> 03:52:11,600 will be this 6004 03:52:09,600 --> 03:52:13,439 because i am assigning a dot next into 6005 03:52:11,600 --> 03:52:16,160 self dot head and my self dot head was 6006 03:52:13,439 --> 03:52:18,080 initially at here right but inside self 6007 03:52:16,160 --> 03:52:20,560 dot head i am assigning a dot next and 6008 03:52:18,080 --> 03:52:22,399 this is my a dot next so my new self dot 6009 03:52:20,560 --> 03:52:26,160 head will be here 6010 03:52:22,399 --> 03:52:28,399 so i will remove this 6011 03:52:26,160 --> 03:52:30,880 now after this i will write here a dot 6012 03:52:28,399 --> 03:52:33,680 next is equal to none so if i'm writing 6013 03:52:30,880 --> 03:52:36,000 here a dot next is equal to none 6014 03:52:33,680 --> 03:52:38,319 so this is my a dot next right so if i'm 6015 03:52:36,000 --> 03:52:41,359 writing my a dot next is equal to none 6016 03:52:38,319 --> 03:52:43,680 then this link will disconnect yeah so i 6017 03:52:41,359 --> 03:52:45,760 will remove this link 6018 03:52:43,680 --> 03:52:48,080 now if you see here i have written self 6019 03:52:45,760 --> 03:52:50,560 dot head dot previous is equal to none 6020 03:52:48,080 --> 03:52:52,239 so now what is my self dot head self dot 6021 03:52:50,560 --> 03:52:53,279 head is here right so if i am writing 6022 03:52:52,239 --> 03:52:55,120 here self 6023 03:52:53,279 --> 03:52:57,520 dot head 6024 03:52:55,120 --> 03:52:59,600 dot previous that means i'm talking 6025 03:52:57,520 --> 03:53:01,600 about this one right so self dot head 6026 03:52:59,600 --> 03:53:03,279 dot previous if i'm assigning here none 6027 03:53:01,600 --> 03:53:06,239 that means there is a disconnection 6028 03:53:03,279 --> 03:53:08,640 between n1 and n2 6029 03:53:06,239 --> 03:53:10,560 so this link will be disconnected 6030 03:53:08,640 --> 03:53:12,080 so let me disconnect it so once the link 6031 03:53:10,560 --> 03:53:14,399 has been disconnected so you can see 6032 03:53:12,080 --> 03:53:16,239 that this is the deletion of a n1 node 6033 03:53:14,399 --> 03:53:18,560 that is deletion at the beginning and 6034 03:53:16,239 --> 03:53:20,160 here this is my head position this is my 6035 03:53:18,560 --> 03:53:23,680 n2 node and 6036 03:53:20,160 --> 03:53:25,439 this is pointing to none 6037 03:53:23,680 --> 03:53:27,040 right so this is the basic logic of 6038 03:53:25,439 --> 03:53:29,040 deletion at the beginning now let's see 6039 03:53:27,040 --> 03:53:31,279 it into the coding part 6040 03:53:29,040 --> 03:53:34,640 so now let's start with the deletion at 6041 03:53:31,279 --> 03:53:34,640 the beginning so i'll write here 6042 03:53:34,960 --> 03:53:40,080 def and then i will write here deletion 6043 03:53:38,720 --> 03:53:42,560 at 6044 03:53:40,080 --> 03:53:42,560 beginning 6045 03:53:44,399 --> 03:53:49,120 and then i will write here self 6046 03:53:46,880 --> 03:53:50,800 i don't have to pass the parameter here 6047 03:53:49,120 --> 03:53:52,000 as data because i have to delete the 6048 03:53:50,800 --> 03:53:54,640 node right 6049 03:53:52,000 --> 03:53:56,160 so inside this i'll write first 6050 03:53:54,640 --> 03:53:58,960 a is equal to 6051 03:53:56,160 --> 03:53:58,960 self.head 6052 03:54:00,399 --> 03:54:04,720 right so here i've created a temporary 6053 03:54:02,479 --> 03:54:06,880 variable in which i have assigned head 6054 03:54:04,720 --> 03:54:09,120 right as self.head now i'll write here 6055 03:54:06,880 --> 03:54:11,199 self dot head 6056 03:54:09,120 --> 03:54:13,840 is equal to a dot next 6057 03:54:11,199 --> 03:54:15,199 so i'm changing my head value from the 6058 03:54:13,840 --> 03:54:17,439 first node to the second node because 6059 03:54:15,199 --> 03:54:19,840 first node i have to remove it then i 6060 03:54:17,439 --> 03:54:21,439 will write here a dot next 6061 03:54:19,840 --> 03:54:23,279 is equal to none 6062 03:54:21,439 --> 03:54:25,760 that means i am disconnecting the link 6063 03:54:23,279 --> 03:54:26,720 between nodes and then i'll write here 6064 03:54:25,760 --> 03:54:28,479 self 6065 03:54:26,720 --> 03:54:30,319 dot head 6066 03:54:28,479 --> 03:54:31,520 dot previous 6067 03:54:30,319 --> 03:54:33,840 is equal to 6068 03:54:31,520 --> 03:54:33,840 none 6069 03:54:34,080 --> 03:54:40,080 so let me execute this 6070 03:54:37,120 --> 03:54:40,080 i'll copy this thing 6071 03:54:45,760 --> 03:54:49,760 so i'll paste here and then after that i 6072 03:54:47,760 --> 03:54:50,720 will write here dll 6073 03:54:49,760 --> 03:54:53,120 dot 6074 03:54:50,720 --> 03:54:55,120 deletion at beginning and 6075 03:54:53,120 --> 03:54:56,960 after this 6076 03:54:55,120 --> 03:54:59,199 i'll write dll 6077 03:54:56,960 --> 03:54:59,199 dot 6078 03:54:59,279 --> 03:55:03,359 forward 6079 03:55:00,720 --> 03:55:03,359 traversal 6080 03:55:08,239 --> 03:55:15,160 and i want the output to be in the next 6081 03:55:10,160 --> 03:55:15,160 line so let me write before itself print 6082 03:55:15,199 --> 03:55:18,800 so on executing you can see that the 6083 03:55:16,880 --> 03:55:20,800 first node was deleted and in the first 6084 03:55:18,800 --> 03:55:23,520 node i was having a data element s2 so 6085 03:55:20,800 --> 03:55:25,680 it has already been deleted now what if 6086 03:55:23,520 --> 03:55:28,319 i want to do the backward traversal so 6087 03:55:25,680 --> 03:55:29,520 once again i can do it i'll just copy 6088 03:55:28,319 --> 03:55:32,319 here 6089 03:55:29,520 --> 03:55:32,319 and paste here 6090 03:55:33,760 --> 03:55:37,040 let me write here backward 6091 03:55:39,680 --> 03:55:44,239 and after running you can see that i am 6092 03:55:41,840 --> 03:55:45,920 doing my backward traversal so this is 6093 03:55:44,239 --> 03:55:47,040 the basic idea for the deletion at the 6094 03:55:45,920 --> 03:55:48,640 beginning 6095 03:55:47,040 --> 03:55:50,880 now after deletion at beginning we will 6096 03:55:48,640 --> 03:55:54,680 see the deletion at end so let me create 6097 03:55:50,880 --> 03:55:54,680 my doubly linked list 6098 03:55:56,000 --> 03:56:01,279 so let's say i'm creating a doubly 6099 03:55:57,920 --> 03:56:05,840 linked list of four nodes 6100 03:56:01,279 --> 03:56:05,840 so each node will be having two pointers 6101 03:56:08,239 --> 03:56:13,920 that is previous and the next so i'm 6102 03:56:11,040 --> 03:56:15,040 having four nodes starting from n1 n2 n3 6103 03:56:13,920 --> 03:56:16,880 and n4 6104 03:56:15,040 --> 03:56:18,399 and 6105 03:56:16,880 --> 03:56:20,960 let me connect 6106 03:56:18,399 --> 03:56:20,960 with the link 6107 03:56:21,840 --> 03:56:25,760 so as we know that n1 dot previous will 6108 03:56:24,000 --> 03:56:29,279 be none initially 6109 03:56:25,760 --> 03:56:32,080 and n4 dot next will be none 6110 03:56:29,279 --> 03:56:33,439 so here i want to delete the last node 6111 03:56:32,080 --> 03:56:35,359 so one thing is clear if you want to 6112 03:56:33,439 --> 03:56:37,199 delete the last node then you have to go 6113 03:56:35,359 --> 03:56:39,920 through each node so i have to create a 6114 03:56:37,199 --> 03:56:41,120 logic so for that i will be using a loop 6115 03:56:39,920 --> 03:56:42,880 now 6116 03:56:41,120 --> 03:56:45,120 if you want to delete the last node then 6117 03:56:42,880 --> 03:56:47,279 we need two variables also so i will 6118 03:56:45,120 --> 03:56:49,040 create here two variables 6119 03:56:47,279 --> 03:56:52,479 so the first variable i will create here 6120 03:56:49,040 --> 03:56:54,160 is previous is equal to self dot head 6121 03:56:52,479 --> 03:56:56,640 as we know that head will always point 6122 03:56:54,160 --> 03:56:59,520 to the first node so this will be my 6123 03:56:56,640 --> 03:56:59,520 previous variable 6124 03:56:59,760 --> 03:57:03,439 now i will create one more variable a is 6125 03:57:02,000 --> 03:57:04,840 equal to self 6126 03:57:03,439 --> 03:57:06,399 dot head dot 6127 03:57:04,840 --> 03:57:08,479 next 6128 03:57:06,399 --> 03:57:11,439 so self.head is this right if i'm 6129 03:57:08,479 --> 03:57:13,920 writing self.head.next that means 6130 03:57:11,439 --> 03:57:16,080 i'm coming to here 6131 03:57:13,920 --> 03:57:18,080 so this will be my a 6132 03:57:16,080 --> 03:57:20,399 right now as i told you that if i want 6133 03:57:18,080 --> 03:57:21,600 to delete the end node then i have to go 6134 03:57:20,399 --> 03:57:23,439 through the each node that means 6135 03:57:21,600 --> 03:57:27,199 traversing through each node so for that 6136 03:57:23,439 --> 03:57:29,760 i will be using a loop while loop 6137 03:57:27,199 --> 03:57:32,000 and i will write while a dot next is not 6138 03:57:29,760 --> 03:57:32,000 none 6139 03:57:32,239 --> 03:57:37,199 so my a is this right so a dot next is 6140 03:57:34,960 --> 03:57:39,040 it none no it's not none a dot next 6141 03:57:37,199 --> 03:57:41,120 contains the address of n3 node so it's 6142 03:57:39,040 --> 03:57:43,040 not none right let's suppose the address 6143 03:57:41,120 --> 03:57:44,720 of n3 node is one thousand so it will 6144 03:57:43,040 --> 03:57:46,000 contain the address 1000 here so it's 6145 03:57:44,720 --> 03:57:48,399 not none 6146 03:57:46,000 --> 03:57:51,199 so if it's not none then a is equal to a 6147 03:57:48,399 --> 03:57:53,359 dot next then my new a will be here 6148 03:57:51,199 --> 03:57:56,000 similarly previous equal to previous dot 6149 03:57:53,359 --> 03:57:58,000 next so my previous will come here so 6150 03:57:56,000 --> 03:58:00,399 once again this loop will work it will 6151 03:57:58,000 --> 03:58:04,080 say a dot next is not none so here a dot 6152 03:58:00,399 --> 03:58:06,640 next is not none right so once again 6153 03:58:04,080 --> 03:58:08,160 my new a will be here and the previous 6154 03:58:06,640 --> 03:58:10,479 will be here 6155 03:58:08,160 --> 03:58:12,880 now why i have created two variables you 6156 03:58:10,479 --> 03:58:14,960 will get to know now if you see here i 6157 03:58:12,880 --> 03:58:17,920 have written here previous 6158 03:58:14,960 --> 03:58:20,160 dot next is equal to none 6159 03:58:17,920 --> 03:58:22,399 this is my previous right if i am 6160 03:58:20,160 --> 03:58:24,319 writing here previous dot next is equal 6161 03:58:22,399 --> 03:58:25,920 to none that means i am disconnecting 6162 03:58:24,319 --> 03:58:28,720 this link right 6163 03:58:25,920 --> 03:58:30,479 and this is my a here right so if i am 6164 03:58:28,720 --> 03:58:31,840 writing here a dot previous is equal to 6165 03:58:30,479 --> 03:58:33,279 none 6166 03:58:31,840 --> 03:58:36,080 once again the link has been 6167 03:58:33,279 --> 03:58:38,080 disconnected that means this node has 6168 03:58:36,080 --> 03:58:40,399 been deleted so this is the basic logic 6169 03:58:38,080 --> 03:58:42,720 of deletion at end in doubly linked list 6170 03:58:40,399 --> 03:58:44,479 so let's see the coding part so once 6171 03:58:42,720 --> 03:58:46,479 again i will create a here function and 6172 03:58:44,479 --> 03:58:47,439 i will write here function name deletion 6173 03:58:46,479 --> 03:58:49,680 at 6174 03:58:47,439 --> 03:58:49,680 end 6175 03:58:51,359 --> 03:58:54,960 and 6176 03:58:53,199 --> 03:58:56,560 after this what i will do let me write 6177 03:58:54,960 --> 03:58:58,560 print here first so that i can get the 6178 03:58:56,560 --> 03:59:01,680 output in the next line now i will 6179 03:58:58,560 --> 03:59:01,680 create two variable here 6180 03:59:02,080 --> 03:59:06,560 first let me create the variable as head 6181 03:59:04,000 --> 03:59:09,439 so i'll write here previous is equal to 6182 03:59:06,560 --> 03:59:09,439 self.head 6183 03:59:10,080 --> 03:59:13,760 and then i locate a is equal to 6184 03:59:12,720 --> 03:59:15,520 self 6185 03:59:13,760 --> 03:59:17,600 dot head 6186 03:59:15,520 --> 03:59:19,199 dot next 6187 03:59:17,600 --> 03:59:21,040 now what i will do since i have to 6188 03:59:19,199 --> 03:59:23,199 delete the last node then i have to go 6189 03:59:21,040 --> 03:59:25,359 through the each node so i'll create a 6190 03:59:23,199 --> 03:59:26,800 while loop here and i will write while a 6191 03:59:25,359 --> 03:59:28,399 dot next 6192 03:59:26,800 --> 03:59:30,960 is 6193 03:59:28,399 --> 03:59:30,960 not none 6194 03:59:36,160 --> 03:59:43,040 i'll assign a is equal to a dot next 6195 03:59:40,479 --> 03:59:45,600 and i will also write previous 6196 03:59:43,040 --> 03:59:47,920 is equal to previous dot 6197 03:59:45,600 --> 03:59:47,920 next 6198 03:59:48,319 --> 03:59:53,040 after this once the loop is over what i 6199 03:59:50,640 --> 03:59:55,840 will do my previous dot next is none if 6200 03:59:53,040 --> 03:59:55,840 i'm writing here 6201 03:59:57,439 --> 04:00:00,800 that means the link has been 6202 03:59:58,720 --> 04:00:02,960 disconnected now if i'm writing a dot 6203 04:00:00,800 --> 04:00:04,800 previous that means the last node if i'm 6204 04:00:02,960 --> 04:00:07,520 writing a dot previous 6205 04:00:04,800 --> 04:00:10,239 is equal to none 6206 04:00:07,520 --> 04:00:12,479 then my the node has been deleted now if 6207 04:00:10,239 --> 04:00:13,439 i want to execute it 6208 04:00:12,479 --> 04:00:15,920 then 6209 04:00:13,439 --> 04:00:17,600 let me execute this 6210 04:00:15,920 --> 04:00:19,359 i will copy this 6211 04:00:17,600 --> 04:00:20,960 ctrl c 6212 04:00:19,359 --> 04:00:22,560 and then ctrl v 6213 04:00:20,960 --> 04:00:24,720 and here i will write instead of 6214 04:00:22,560 --> 04:00:25,840 beginning i'll write here deletion at 6215 04:00:24,720 --> 04:00:27,439 end 6216 04:00:25,840 --> 04:00:30,560 let me check the function name is it 6217 04:00:27,439 --> 04:00:32,479 correct yeah now if i'm executing it so 6218 04:00:30,560 --> 04:00:35,199 on executing if you see that i was 6219 04:00:32,479 --> 04:00:38,319 having earlier six nodes but now i am 6220 04:00:35,199 --> 04:00:40,399 having 5 node and this 25 data element 6221 04:00:38,319 --> 04:00:42,160 has been deleted why because if node has 6222 04:00:40,399 --> 04:00:44,720 been deleted then data also inside that 6223 04:00:42,160 --> 04:00:47,120 node will be deleted now here once again 6224 04:00:44,720 --> 04:00:48,000 i have done the backward traversal 6225 04:00:47,120 --> 04:00:49,680 so 6226 04:00:48,000 --> 04:00:51,920 so while backward traversal i am getting 6227 04:00:49,680 --> 04:00:54,399 the data values from the last that is 20 6228 04:00:51,920 --> 04:00:56,000 15 10 7 and 5 so this is the logic for 6229 04:00:54,399 --> 04:00:57,600 deletion at end now we will see the 6230 04:00:56,000 --> 04:01:00,239 logic if you want to delete any 6231 04:00:57,600 --> 04:01:01,840 specified node then how can you delete 6232 04:01:00,239 --> 04:01:03,600 so after seeing the deletion at 6233 04:01:01,840 --> 04:01:05,439 beginning as well as end we will see the 6234 04:01:03,600 --> 04:01:08,319 relation of a node at a specified 6235 04:01:05,439 --> 04:01:10,319 position so let me create here a doubly 6236 04:01:08,319 --> 04:01:11,760 linked list 6237 04:01:10,319 --> 04:01:14,800 n1 6238 04:01:11,760 --> 04:01:14,800 this is n2 6239 04:01:15,359 --> 04:01:21,199 this is 6240 04:01:16,840 --> 04:01:21,199 n3 and this is n4 6241 04:01:22,960 --> 04:01:27,760 so we know that each node is having two 6242 04:01:25,439 --> 04:01:30,080 pointers that is previous as well as 6243 04:01:27,760 --> 04:01:30,080 next 6244 04:01:31,439 --> 04:01:37,439 so let's suppose 1000 is the address of 6245 04:01:33,680 --> 04:01:37,439 this node 2000 of n2 6246 04:01:38,160 --> 04:01:44,720 and for n3 i'm having 3000 address 6247 04:01:41,840 --> 04:01:47,199 4000 is the address of n4 node so we 6248 04:01:44,720 --> 04:01:49,120 know that n3 dot next will contain the 6249 04:01:47,199 --> 04:01:51,279 address of n4 that is 4000 right 6250 04:01:49,120 --> 04:01:52,960 similarly n4 dot previous it will 6251 04:01:51,279 --> 04:01:54,160 contain the address of n3 node that is 6252 04:01:52,960 --> 04:01:56,560 3000 6253 04:01:54,160 --> 04:01:59,840 that's already understandable 6254 04:01:56,560 --> 04:02:02,239 none so n4.next will be none and n1 dot 6255 04:01:59,840 --> 04:02:04,399 previous will be also none 6256 04:02:02,239 --> 04:02:06,080 so like the previous case we have seen 6257 04:02:04,399 --> 04:02:08,000 deletion at end 6258 04:02:06,080 --> 04:02:09,840 here also we will take two variables the 6259 04:02:08,000 --> 04:02:12,319 first variable name is 6260 04:02:09,840 --> 04:02:15,040 a and another variable here name will be 6261 04:02:12,319 --> 04:02:17,279 b so what i will do here this will be my 6262 04:02:15,040 --> 04:02:18,560 b which is nothing but a head self dot 6263 04:02:17,279 --> 04:02:21,520 head 6264 04:02:18,560 --> 04:02:23,040 right and my a will be self 6265 04:02:21,520 --> 04:02:24,479 dot head 6266 04:02:23,040 --> 04:02:26,560 dot next 6267 04:02:24,479 --> 04:02:28,720 so what is self dot head dot next self 6268 04:02:26,560 --> 04:02:31,840 dot head dot next is nothing but this 6269 04:02:28,720 --> 04:02:32,640 one right so that means this is my 6270 04:02:31,840 --> 04:02:35,040 a 6271 04:02:32,640 --> 04:02:37,199 right and this is my b so what i will do 6272 04:02:35,040 --> 04:02:38,560 here i will use for loop but previously 6273 04:02:37,199 --> 04:02:40,960 we have seen the while loop for the 6274 04:02:38,560 --> 04:02:43,120 deletion at the end right but why here 6275 04:02:40,960 --> 04:02:44,560 we are using for loop because let's see 6276 04:02:43,120 --> 04:02:46,160 the position this is the position one 6277 04:02:44,560 --> 04:02:48,399 this is the position two this is the 6278 04:02:46,160 --> 04:02:50,960 position three and this is the position 6279 04:02:48,399 --> 04:02:52,960 four so if i want to delete the node at 6280 04:02:50,960 --> 04:02:57,199 third position right 6281 04:02:52,960 --> 04:02:58,880 so i will write here for i in range 6282 04:02:57,199 --> 04:03:00,960 and this will be one comma position 6283 04:02:58,880 --> 04:03:02,560 minus one 6284 04:03:00,960 --> 04:03:04,960 so my position is three three minus one 6285 04:03:02,560 --> 04:03:07,439 will be two so here i will getting one 6286 04:03:04,960 --> 04:03:09,279 comma two so as we know that in python 6287 04:03:07,439 --> 04:03:11,520 if one comma two is there then two will 6288 04:03:09,279 --> 04:03:12,479 be excluded that means for i is equal to 6289 04:03:11,520 --> 04:03:14,479 1 6290 04:03:12,479 --> 04:03:17,120 my loop will run a is equal to a dot 6291 04:03:14,479 --> 04:03:19,359 next and b is equal to b dot next so a 6292 04:03:17,120 --> 04:03:21,439 is here right so a dot next is nothing 6293 04:03:19,359 --> 04:03:24,239 but a dot next will contain the address 6294 04:03:21,439 --> 04:03:26,640 of 3000 right 6295 04:03:24,239 --> 04:03:29,120 so this will be my a 6296 04:03:26,640 --> 04:03:31,520 similarly b dot next is nothing 6297 04:03:29,120 --> 04:03:33,600 it will contain the address of 6298 04:03:31,520 --> 04:03:34,960 and to know that is 2 000 so this will 6299 04:03:33,600 --> 04:03:37,279 be my b 6300 04:03:34,960 --> 04:03:39,040 so for i is equal to 1 6301 04:03:37,279 --> 04:03:41,359 my a will be here my b will be here 6302 04:03:39,040 --> 04:03:42,880 right now what's the point of doing the 6303 04:03:41,359 --> 04:03:44,880 iteration here 6304 04:03:42,880 --> 04:03:45,840 if you see here i have written b dot 6305 04:03:44,880 --> 04:03:48,560 next 6306 04:03:45,840 --> 04:03:51,600 is equal to a dot next 6307 04:03:48,560 --> 04:03:53,760 b dot next is this right and inside b 6308 04:03:51,600 --> 04:03:56,560 dot next i am assigning a dot next and 6309 04:03:53,760 --> 04:03:58,880 this is my a dot next 6310 04:03:56,560 --> 04:04:00,000 right so what i will do so i will remove 6311 04:03:58,880 --> 04:04:02,960 this 6312 04:04:00,000 --> 04:04:03,760 and i will connect this b dot next 6313 04:04:02,960 --> 04:04:05,680 with 6314 04:04:03,760 --> 04:04:07,760 a dot next so there is a connection 6315 04:04:05,680 --> 04:04:08,640 after that i have written here a dot 6316 04:04:07,760 --> 04:04:10,800 next 6317 04:04:08,640 --> 04:04:13,680 dot previous 6318 04:04:10,800 --> 04:04:15,600 is equal to b so what will happen so a 6319 04:04:13,680 --> 04:04:18,000 dot next is this right so i'm coming 6320 04:04:15,600 --> 04:04:20,560 here now if i'm writing a dot next dot 6321 04:04:18,000 --> 04:04:22,800 previous so i am going backward so if i 6322 04:04:20,560 --> 04:04:24,960 am writing a dot next dot previous and 6323 04:04:22,800 --> 04:04:27,680 in that if i am assigning b then you 6324 04:04:24,960 --> 04:04:30,960 will see so this will be removed 6325 04:04:27,680 --> 04:04:33,600 and this will be connected to here 6326 04:04:30,960 --> 04:04:35,760 right to n2 node now what will happen 6327 04:04:33,600 --> 04:04:37,040 here after this if i'm writing here a 6328 04:04:35,760 --> 04:04:38,560 dot next 6329 04:04:37,040 --> 04:04:41,279 is equal to none 6330 04:04:38,560 --> 04:04:43,120 so this is my a dot next right so if i'm 6331 04:04:41,279 --> 04:04:45,439 writing a dot next is equal to none that 6332 04:04:43,120 --> 04:04:47,680 means inside a dot next i'm assigning 6333 04:04:45,439 --> 04:04:50,160 none so this link will be disconnected 6334 04:04:47,680 --> 04:04:52,080 so i will erase this link similarly if 6335 04:04:50,160 --> 04:04:54,000 i'm writing here a dot previous is equal 6336 04:04:52,080 --> 04:04:54,880 to none this is a a dot previous is this 6337 04:04:54,000 --> 04:04:56,640 right 6338 04:04:54,880 --> 04:04:59,520 so i will remove this so here you can 6339 04:04:56,640 --> 04:05:01,439 see that this is a deletion of a node at 6340 04:04:59,520 --> 04:05:03,359 the position three so this is the basic 6341 04:05:01,439 --> 04:05:05,520 idea about deletion now we will see the 6342 04:05:03,359 --> 04:05:08,000 concept in coding 6343 04:05:05,520 --> 04:05:09,920 so now we will see the deletion at any 6344 04:05:08,000 --> 04:05:12,319 particular node so what i will write 6345 04:05:09,920 --> 04:05:16,080 here i will create a function and i will 6346 04:05:12,319 --> 04:05:17,199 write function name deletion 6347 04:05:16,080 --> 04:05:20,080 at 6348 04:05:17,199 --> 04:05:20,080 particular node 6349 04:05:24,800 --> 04:05:29,120 and i will give here a parameter as 6350 04:05:26,399 --> 04:05:32,160 position because i want to delete a node 6351 04:05:29,120 --> 04:05:32,160 at particular position 6352 04:05:32,479 --> 04:05:35,840 so now what i will do here once again i 6353 04:05:34,160 --> 04:05:38,000 will write here print so that i can get 6354 04:05:35,840 --> 04:05:39,520 the output in the next line after that i 6355 04:05:38,000 --> 04:05:41,520 will create a variable let's suppose 6356 04:05:39,520 --> 04:05:43,199 here i'm creating a variable a is equal 6357 04:05:41,520 --> 04:05:44,800 to self 6358 04:05:43,199 --> 04:05:47,040 dot head 6359 04:05:44,800 --> 04:05:49,279 dot next 6360 04:05:47,040 --> 04:05:50,800 then i'm creating a variable b which is 6361 04:05:49,279 --> 04:05:51,600 equal to self 6362 04:05:50,800 --> 04:05:53,199 dot 6363 04:05:51,600 --> 04:05:55,199 head 6364 04:05:53,199 --> 04:05:57,359 so if i'm creating a variable a so that 6365 04:05:55,199 --> 04:05:58,960 means i'm assigning self dot head dot 6366 04:05:57,359 --> 04:06:00,720 next into the variable a similarly if 6367 04:05:58,960 --> 04:06:03,120 i'm creating a variable b so that means 6368 04:06:00,720 --> 04:06:05,120 i'm assigning self.head into this now 6369 04:06:03,120 --> 04:06:06,640 after creating this i'll create a loop 6370 04:06:05,120 --> 04:06:09,680 so i'll write here for 6371 04:06:06,640 --> 04:06:09,680 i in range 6372 04:06:09,920 --> 04:06:14,640 and then i will write here one comma 6373 04:06:11,600 --> 04:06:14,640 position minus one 6374 04:06:18,560 --> 04:06:24,239 and after this what i will do i will 6375 04:06:20,960 --> 04:06:27,840 write here a is equal to a dot next 6376 04:06:24,239 --> 04:06:27,840 and my b is equal to b dot next 6377 04:06:29,920 --> 04:06:32,479 now 6378 04:06:30,880 --> 04:06:34,000 i'll write here b 6379 04:06:32,479 --> 04:06:35,040 dot 6380 04:06:34,000 --> 04:06:36,880 next 6381 04:06:35,040 --> 04:06:37,760 is equal to a 6382 04:06:36,880 --> 04:06:40,960 dot 6383 04:06:37,760 --> 04:06:43,279 next after that i will write a dot next 6384 04:06:40,960 --> 04:06:46,239 dot previous 6385 04:06:43,279 --> 04:06:48,640 and in which i will assign here b 6386 04:06:46,239 --> 04:06:50,960 then i will write here a dot next 6387 04:06:48,640 --> 04:06:53,199 is equal to none 6388 04:06:50,960 --> 04:06:55,359 and then i will write here a dot 6389 04:06:53,199 --> 04:06:58,000 previous 6390 04:06:55,359 --> 04:06:59,040 is equal to none 6391 04:06:58,000 --> 04:07:00,960 and now 6392 04:06:59,040 --> 04:07:03,960 i will call this function with function 6393 04:07:00,960 --> 04:07:03,960 name 6394 04:07:08,080 --> 04:07:12,239 so let me write here dll dot 6395 04:07:13,199 --> 04:07:16,800 i will paste this function so let's 6396 04:07:14,960 --> 04:07:18,640 suppose that if i want to delete at 6397 04:07:16,800 --> 04:07:20,880 position three right so i'll write here 6398 04:07:18,640 --> 04:07:22,479 position three so we'll see that this 6399 04:07:20,880 --> 04:07:25,359 node will be deleted and the data 6400 04:07:22,479 --> 04:07:28,720 elements will be also deleted so my new 6401 04:07:25,359 --> 04:07:31,120 doubly linked list will be 5 7 15 20. so 6402 04:07:28,720 --> 04:07:33,560 now let me call here 6403 04:07:31,120 --> 04:07:36,560 so for calling it i will copy this 6404 04:07:33,560 --> 04:07:38,720 dll.forward traversal let me copy and 6405 04:07:36,560 --> 04:07:40,880 paste here 6406 04:07:38,720 --> 04:07:42,399 and now if i'm running it you can see 6407 04:07:40,880 --> 04:07:45,840 that 6408 04:07:42,399 --> 04:07:48,000 i am getting 5 7 15 20 this 10 has been 6409 04:07:45,840 --> 04:07:50,399 deleted similarly if you want it for the 6410 04:07:48,000 --> 04:07:53,439 backward traversal you can do it 6411 04:07:50,399 --> 04:07:53,439 i will just copy this 6412 04:07:54,880 --> 04:07:58,880 and then i will paste 6413 04:07:57,040 --> 04:08:00,479 so you can see that 6414 04:07:58,880 --> 04:08:03,040 we are getting the node value from 6415 04:08:00,479 --> 04:08:05,359 backward so this was the logic of 6416 04:08:03,040 --> 04:08:07,199 deletion so next we will see what is 6417 04:08:05,359 --> 04:08:09,120 circular linked list 6418 04:08:07,199 --> 04:08:11,600 so circular linked list is a linked list 6419 04:08:09,120 --> 04:08:14,399 where last node contains the address of 6420 04:08:11,600 --> 04:08:17,199 first node so let's suppose this is a n1 6421 04:08:14,399 --> 04:08:19,920 node this is the endo node this is the 6422 04:08:17,199 --> 04:08:22,000 n3 node right this will contain a data 6423 04:08:19,920 --> 04:08:23,840 10 let's suppose and this will contain 6424 04:08:22,000 --> 04:08:25,520 20 this is 30 6425 04:08:23,840 --> 04:08:27,040 and 6426 04:08:25,520 --> 04:08:29,199 let me give the address here so let's 6427 04:08:27,040 --> 04:08:32,880 suppose the address of this node is 1000 6428 04:08:29,199 --> 04:08:34,640 here is 2 000 and then here is 3000 6429 04:08:32,880 --> 04:08:38,640 so we know that n1 will contain the 6430 04:08:34,640 --> 04:08:40,640 address of end to node that is 2000 here 6431 04:08:38,640 --> 04:08:41,760 similarly n2 will contain the address of 6432 04:08:40,640 --> 04:08:43,199 n3 6433 04:08:41,760 --> 04:08:44,560 that is 6434 04:08:43,199 --> 04:08:47,040 3000 6435 04:08:44,560 --> 04:08:49,359 now this is the last node n3 is the last 6436 04:08:47,040 --> 04:08:51,040 node now this will contain the address 6437 04:08:49,359 --> 04:08:53,760 of first node what is the address of 6438 04:08:51,040 --> 04:08:55,520 first node 1000 6439 04:08:53,760 --> 04:08:57,600 so this is a basic idea of circular 6440 04:08:55,520 --> 04:08:59,040 linked list now circular linked list can 6441 04:08:57,600 --> 04:09:01,680 be of two types 6442 04:08:59,040 --> 04:09:04,319 let me write here circular 6443 04:09:01,680 --> 04:09:04,319 linked list 6444 04:09:04,880 --> 04:09:09,520 so in circular linked list i can have 6445 04:09:07,279 --> 04:09:13,800 singly 6446 04:09:09,520 --> 04:09:13,800 circular linked list 6447 04:09:15,840 --> 04:09:19,680 and then i can have also doubly 6448 04:09:20,479 --> 04:09:25,279 circular 6449 04:09:22,720 --> 04:09:25,279 linked list 6450 04:09:26,239 --> 04:09:31,439 now we have seen the operations like 6451 04:09:28,479 --> 04:09:33,439 insertion deletion as well as traversal 6452 04:09:31,439 --> 04:09:35,520 in singly linked list the same will go 6453 04:09:33,439 --> 04:09:37,920 for singly circular linked list 6454 04:09:35,520 --> 04:09:40,479 similarly the same goes for doubly 6455 04:09:37,920 --> 04:09:41,680 circular linked list so at insertion we 6456 04:09:40,479 --> 04:09:44,080 have seen that we have done the 6457 04:09:41,680 --> 04:09:46,239 insertion at beginning then we have done 6458 04:09:44,080 --> 04:09:48,399 the insertion at end then we have done 6459 04:09:46,239 --> 04:09:50,080 the insertion and specified node so the 6460 04:09:48,399 --> 04:09:52,319 same goes for the singly circular linked 6461 04:09:50,080 --> 04:09:54,399 list as well as for doubly circular 6462 04:09:52,319 --> 04:09:56,640 linked list right so this was all about 6463 04:09:54,399 --> 04:09:59,040 circular linked list now let's talk 6464 04:09:56,640 --> 04:10:00,960 about the advantages and disadvantages 6465 04:09:59,040 --> 04:10:03,359 of linked lists first let's talk about 6466 04:10:00,960 --> 04:10:04,720 the advantages so obviously as compared 6467 04:10:03,359 --> 04:10:06,479 to array 6468 04:10:04,720 --> 04:10:07,920 right when we talked about arrays they 6469 04:10:06,479 --> 04:10:10,800 were fixed 6470 04:10:07,920 --> 04:10:13,600 in size right we need to specify the 6471 04:10:10,800 --> 04:10:16,080 size in the subscript so fixed in size 6472 04:10:13,600 --> 04:10:18,880 which is not in the case of arrays it is 6473 04:10:16,080 --> 04:10:21,279 dynamic in nature and you can add as 6474 04:10:18,880 --> 04:10:24,159 many nodes as you want depending upon 6475 04:10:21,279 --> 04:10:25,840 your requirement right so obviously when 6476 04:10:24,159 --> 04:10:26,720 you have that 6477 04:10:25,840 --> 04:10:29,120 uh 6478 04:10:26,720 --> 04:10:31,120 independency over the size 6479 04:10:29,120 --> 04:10:33,199 you will never there's no chance of 6480 04:10:31,120 --> 04:10:35,040 wasting any memory right so if you 6481 04:10:33,199 --> 04:10:36,880 require four nodes you will use four 6482 04:10:35,040 --> 04:10:38,800 nodes if you require three nodes you 6483 04:10:36,880 --> 04:10:40,960 will require you will add three nodes 6484 04:10:38,800 --> 04:10:43,680 only so in this way the size of the 6485 04:10:40,960 --> 04:10:45,520 linked list is entirely dependent upon 6486 04:10:43,680 --> 04:10:48,159 your application what are the things 6487 04:10:45,520 --> 04:10:50,080 that you require you make them that's it 6488 04:10:48,159 --> 04:10:51,279 so you will not be wasting your memory 6489 04:10:50,080 --> 04:10:54,239 and you will be 6490 04:10:51,279 --> 04:10:56,000 utilizing your memory very efficiently 6491 04:10:54,239 --> 04:10:58,000 so this is the reason why this is 6492 04:10:56,000 --> 04:11:00,319 written over here the efficient memory 6493 04:10:58,000 --> 04:11:02,159 allocation how you are doing efficient 6494 04:11:00,319 --> 04:11:03,920 memory allocation let's suppose you have 6495 04:11:02,159 --> 04:11:06,479 chunks of memory available here and 6496 04:11:03,920 --> 04:11:08,159 there you can link them together and 6497 04:11:06,479 --> 04:11:10,720 allocate 6498 04:11:08,159 --> 04:11:13,840 the memory and use that use those uh 6499 04:11:10,720 --> 04:11:17,520 memory location for storing the data 6500 04:11:13,840 --> 04:11:20,560 right so memory can be used efficiently 6501 04:11:17,520 --> 04:11:23,600 memory is not wasted it is dynamic in 6502 04:11:20,560 --> 04:11:24,640 size and it is very easy to insert and 6503 04:11:23,600 --> 04:11:25,520 delete 6504 04:11:24,640 --> 04:11:27,279 a 6505 04:11:25,520 --> 04:11:29,439 node in a linked list just by 6506 04:11:27,279 --> 04:11:31,520 manipulating a single link as we have 6507 04:11:29,439 --> 04:11:34,399 seen earlier just if you want to delete 6508 04:11:31,520 --> 04:11:37,359 this you just manipulate this link and 6509 04:11:34,399 --> 04:11:40,080 just point this link to the next node 6510 04:11:37,359 --> 04:11:41,840 which is after this node that's it you 6511 04:11:40,080 --> 04:11:44,159 are done with the deletion operation 6512 04:11:41,840 --> 04:11:46,159 just by manipulating a single link 6513 04:11:44,159 --> 04:11:49,760 similarly when you're talking about 6514 04:11:46,159 --> 04:11:51,600 insertion at that point you just you if 6515 04:11:49,760 --> 04:11:54,000 you want to insert at this location you 6516 04:11:51,600 --> 04:11:56,720 remove this link point it to this and 6517 04:11:54,000 --> 04:11:57,840 then remove and point this new node to 6518 04:11:56,720 --> 04:11:59,600 this 6519 04:11:57,840 --> 04:12:02,080 node that is there that was already 6520 04:11:59,600 --> 04:12:05,040 there again we are manipulating a single 6521 04:12:02,080 --> 04:12:07,439 link okay just a manipulation of single 6522 04:12:05,040 --> 04:12:09,439 link here that is required for insertion 6523 04:12:07,439 --> 04:12:10,960 and a single link here that is required 6524 04:12:09,439 --> 04:12:13,920 for deletion 6525 04:12:10,960 --> 04:12:16,720 now when we talk about disadvantages one 6526 04:12:13,920 --> 04:12:19,359 of the major disadvantage is that you 6527 04:12:16,720 --> 04:12:21,199 cannot lose your head right the head 6528 04:12:19,359 --> 04:12:23,040 node that is pointing always to the 6529 04:12:21,199 --> 04:12:25,279 first node either in the singly linked 6530 04:12:23,040 --> 04:12:27,760 list or a doubly linked list or a 6531 04:12:25,279 --> 04:12:31,920 circular linked list this head node 6532 04:12:27,760 --> 04:12:34,960 cannot be lost if this head node is lost 6533 04:12:31,920 --> 04:12:37,920 or if it is manipulated right if the 6534 04:12:34,960 --> 04:12:40,399 head node is lost or manipulated the 6535 04:12:37,920 --> 04:12:42,319 whole linked list will be lost right 6536 04:12:40,399 --> 04:12:43,120 let's suppose this is your linked list 6537 04:12:42,319 --> 04:12:45,840 and 6538 04:12:43,120 --> 04:12:47,199 somehow right if this is the head node 6539 04:12:45,840 --> 04:12:50,159 and somehow you 6540 04:12:47,199 --> 04:12:52,000 you made it travels to this particular 6541 04:12:50,159 --> 04:12:54,000 point right now here node is pointing to 6542 04:12:52,000 --> 04:12:56,560 this in a singly linked list now there 6543 04:12:54,000 --> 04:12:59,760 is no way you can access these nodes and 6544 04:12:56,560 --> 04:13:03,760 you will get undesired results another 6545 04:12:59,760 --> 04:13:06,239 disadvantage is that you cannot access 6546 04:13:03,760 --> 04:13:07,680 randomly so random access is not 6547 04:13:06,239 --> 04:13:09,680 possible because 6548 04:13:07,680 --> 04:13:12,479 if you want to go at this particular 6549 04:13:09,680 --> 04:13:14,640 node you can you have to traverse 6550 04:13:12,479 --> 04:13:17,279 through the first three nodes first why 6551 04:13:14,640 --> 04:13:19,680 because this node nodes the address of 6552 04:13:17,279 --> 04:13:21,920 this node this node knows the address of 6553 04:13:19,680 --> 04:13:24,479 this node this node knows the address of 6554 04:13:21,920 --> 04:13:27,439 this node there is no way you can 6555 04:13:24,479 --> 04:13:30,000 directly go to this particular node 6556 04:13:27,439 --> 04:13:32,080 random access that was there in the 6557 04:13:30,000 --> 04:13:34,720 arrays with the help of indexing you can 6558 04:13:32,080 --> 04:13:37,840 randomly access the elements that is not 6559 04:13:34,720 --> 04:13:40,880 possible in terms of linked list and the 6560 04:13:37,840 --> 04:13:42,880 address of the first node is 6561 04:13:40,880 --> 04:13:44,800 known to the 6562 04:13:42,880 --> 04:13:46,319 head of the linked list or the root of 6563 04:13:44,800 --> 04:13:50,800 the linked list okay 6564 04:13:46,319 --> 04:13:52,000 so that head is pointing to this first 6565 04:13:50,800 --> 04:13:54,960 so you cannot 6566 04:13:52,000 --> 04:13:57,279 change or manipulate or loss or you 6567 04:13:54,960 --> 04:13:59,760 cannot lose your head because the whole 6568 04:13:57,279 --> 04:14:02,560 linked list will be lost okay and the 6569 04:13:59,760 --> 04:14:03,680 random access is not possible in linked 6570 04:14:02,560 --> 04:14:06,399 list 6571 04:14:03,680 --> 04:14:09,279 binary tree now let's talk about what is 6572 04:14:06,399 --> 04:14:11,520 binary tree so it is a hierarchical data 6573 04:14:09,279 --> 04:14:14,080 structure as we have seen in earlier 6574 04:14:11,520 --> 04:14:16,560 data structures like arrays linked lists 6575 04:14:14,080 --> 04:14:18,720 stacks and queues all of these data 6576 04:14:16,560 --> 04:14:21,520 structures were linear data structures 6577 04:14:18,720 --> 04:14:23,760 but when you talk about tree 6578 04:14:21,520 --> 04:14:26,640 it is a hierarchical data structure that 6579 04:14:23,760 --> 04:14:29,120 means it can have one or more than one 6580 04:14:26,640 --> 04:14:30,399 children's doesn't have to be explicitly 6581 04:14:29,120 --> 04:14:33,040 two children 6582 04:14:30,399 --> 04:14:35,040 but when you talk about binary tree 6583 04:14:33,040 --> 04:14:37,680 therein you can have 6584 04:14:35,040 --> 04:14:40,080 at most two children that means 6585 04:14:37,680 --> 04:14:42,319 if this is the node and then it can have 6586 04:14:40,080 --> 04:14:45,040 at most two children's or if this is a 6587 04:14:42,319 --> 04:14:47,520 node it can have either left child or 6588 04:14:45,040 --> 04:14:50,720 the right child or it can have 6589 04:14:47,520 --> 04:14:53,920 no children's at all okay so this is a 6590 04:14:50,720 --> 04:14:56,239 binary tree and now this is known as the 6591 04:14:53,920 --> 04:14:59,199 root of the tree that means the first 6592 04:14:56,239 --> 04:15:01,680 node is known as the root of the tree 6593 04:14:59,199 --> 04:15:03,600 and you cannot access elements or these 6594 04:15:01,680 --> 04:15:05,040 nodes directly okay 6595 04:15:03,600 --> 04:15:07,279 so 6596 04:15:05,040 --> 04:15:09,359 now uh this is the structure whole 6597 04:15:07,279 --> 04:15:12,159 structure of a binary tree now there are 6598 04:15:09,359 --> 04:15:15,439 some common traversals right how we can 6599 04:15:12,159 --> 04:15:17,760 traverse through all these nodes so one 6600 04:15:15,439 --> 04:15:20,080 of the common traversal is preorder 6601 04:15:17,760 --> 04:15:22,080 wherein you always whenever you are on 6602 04:15:20,080 --> 04:15:24,080 this note you always print this node 6603 04:15:22,080 --> 04:15:26,399 first what is the value of this node 6604 04:15:24,080 --> 04:15:27,520 then you go on to the left side and do 6605 04:15:26,399 --> 04:15:29,199 the same 6606 04:15:27,520 --> 04:15:31,040 then again you go on to the left side 6607 04:15:29,199 --> 04:15:33,600 then do the same print go on the left 6608 04:15:31,040 --> 04:15:36,399 side print go on the left side and so on 6609 04:15:33,600 --> 04:15:39,040 unless and until there is no left then 6610 04:15:36,399 --> 04:15:40,319 what you do is print that 6611 04:15:39,040 --> 04:15:42,560 thing okay 6612 04:15:40,319 --> 04:15:43,520 then after that you will go on right 6613 04:15:42,560 --> 04:15:46,880 side 6614 04:15:43,520 --> 04:15:49,279 so pre-order follows print first then go 6615 04:15:46,880 --> 04:15:51,680 on to the left then go on to the right 6616 04:15:49,279 --> 04:15:53,520 similarly post thread post is saying 6617 04:15:51,680 --> 04:15:55,920 that whatever is you 6618 04:15:53,520 --> 04:15:58,239 have print it at the last so go on the 6619 04:15:55,920 --> 04:15:59,600 left go on the right print at last and 6620 04:15:58,239 --> 04:16:02,080 in order to say 6621 04:15:59,600 --> 04:16:04,399 left print and right so these are all 6622 04:16:02,080 --> 04:16:06,880 traversals we will also see one by one 6623 04:16:04,399 --> 04:16:08,880 how we can implement it and how we will 6624 04:16:06,880 --> 04:16:10,800 also see examples based on these 6625 04:16:08,880 --> 04:16:13,279 traversals 6626 04:16:10,800 --> 04:16:15,600 now let's talk about the applications it 6627 04:16:13,279 --> 04:16:17,520 if you have seen the find hierarchy 6628 04:16:15,600 --> 04:16:19,120 system wherein 6629 04:16:17,520 --> 04:16:21,279 we have different we have the root 6630 04:16:19,120 --> 04:16:23,040 folder and inside that root folder we 6631 04:16:21,279 --> 04:16:24,960 have subfolders like 6632 04:16:23,040 --> 04:16:27,359 music 6633 04:16:24,960 --> 04:16:30,560 and we have bin 6634 04:16:27,359 --> 04:16:32,880 we have files we have program files and 6635 04:16:30,560 --> 04:16:36,000 all those things right so it is a it is 6636 04:16:32,880 --> 04:16:38,479 a kind of a hierarchical data structures 6637 04:16:36,000 --> 04:16:41,840 right and explicitly here we are not 6638 04:16:38,479 --> 04:16:43,760 talking about binary tree but generalize 6639 04:16:41,840 --> 04:16:45,840 now if you have binary tree it has 6640 04:16:43,760 --> 04:16:48,800 multiple variations right if you talk 6641 04:16:45,840 --> 04:16:51,520 about this binary tree so here you 6642 04:16:48,800 --> 04:16:53,840 always have either or the left child or 6643 04:16:51,520 --> 04:16:56,080 the right child when you talk about bst 6644 04:16:53,840 --> 04:16:58,159 the childs are 6645 04:16:56,080 --> 04:17:00,640 inserted in the tree in such a way that 6646 04:16:58,159 --> 04:17:02,880 on the left side it will be always 6647 04:17:00,640 --> 04:17:04,800 less than the root and on the right side 6648 04:17:02,880 --> 04:17:06,479 it will be always greater than the root 6649 04:17:04,800 --> 04:17:09,840 so this is a variation it is known as 6650 04:17:06,479 --> 04:17:12,479 bst so you have av entry wherein you 6651 04:17:09,840 --> 04:17:14,239 don't exceed the height more than two so 6652 04:17:12,479 --> 04:17:16,159 that is one more variation then we have 6653 04:17:14,239 --> 04:17:19,279 red black trees so these are all 6654 04:17:16,159 --> 04:17:21,840 variations of binary tree 6655 04:17:19,279 --> 04:17:24,000 so after knowing what is binary tree in 6656 04:17:21,840 --> 04:17:26,319 theoretical part let's quickly implement 6657 04:17:24,000 --> 04:17:28,479 the binary tree in practicality on 6658 04:17:26,319 --> 04:17:31,120 google collab in python 6659 04:17:28,479 --> 04:17:33,120 so before doing that process so let's 6660 04:17:31,120 --> 04:17:35,520 learn what are the different functions 6661 04:17:33,120 --> 04:17:38,399 we are going to perform in a binary tree 6662 04:17:35,520 --> 04:17:40,720 concept it's generally by default 6663 04:17:38,399 --> 04:17:43,279 everyone will start learning with 6664 04:17:40,720 --> 04:17:46,159 inorder traversal pre-order traversal 6665 04:17:43,279 --> 04:17:48,560 and post order traffic so i'll quickly 6666 04:17:46,159 --> 04:17:50,800 brief you what is in order pre-order and 6667 04:17:48,560 --> 04:17:54,800 post order and then we shall go to 6668 04:17:50,800 --> 04:17:57,520 implementation part right so 6669 04:17:54,800 --> 04:17:58,640 let me give you a brief explanation over 6670 04:17:57,520 --> 04:18:00,960 this 6671 04:17:58,640 --> 04:18:03,279 first we'll start with what is binary 6672 04:18:00,960 --> 04:18:05,040 tree right i'm taking a simple example 6673 04:18:03,279 --> 04:18:08,080 not nothing related to coding or 6674 04:18:05,040 --> 04:18:10,720 anything just a simple example so binary 6675 04:18:08,080 --> 04:18:14,399 tree in the sense it should have at 6676 04:18:10,720 --> 04:18:16,880 least one node and two different 6677 04:18:14,399 --> 04:18:19,680 sub nodes i call it as left and right 6678 04:18:16,880 --> 04:18:22,319 side right it should have one data item 6679 04:18:19,680 --> 04:18:25,199 i am putting it to 1 here and it should 6680 04:18:22,319 --> 04:18:26,479 also have left child and it should have 6681 04:18:25,199 --> 04:18:29,760 the right child 6682 04:18:26,479 --> 04:18:32,840 so again i'm trying to put 2 here 6683 04:18:29,760 --> 04:18:36,239 i'm putting 3 here 6684 04:18:32,840 --> 04:18:37,760 and extending with four so we'll be 6685 04:18:36,239 --> 04:18:41,680 working on understanding with this 6686 04:18:37,760 --> 04:18:41,680 simple example how does 6687 04:18:41,760 --> 04:18:45,840 binary tree will work so the first thing 6688 04:18:44,560 --> 04:18:49,520 you have to know 6689 04:18:45,840 --> 04:18:51,840 i'm talking about in order 6690 04:18:49,520 --> 04:18:54,960 so what is in order here there is a 6691 04:18:51,840 --> 04:18:57,120 simple basic rule that you can remember 6692 04:18:54,960 --> 04:18:59,359 when you traverse throughout the nodes 6693 04:18:57,120 --> 04:19:01,199 which you have in a binary tree you have 6694 04:18:59,359 --> 04:19:03,840 to start from the left 6695 04:19:01,199 --> 04:19:07,840 visit root and then right 6696 04:19:03,840 --> 04:19:11,680 so i'm writing it here for you so left 6697 04:19:07,840 --> 04:19:14,159 first root next and then the right node 6698 04:19:11,680 --> 04:19:15,680 right side node 6699 04:19:14,159 --> 04:19:16,880 okay 6700 04:19:15,680 --> 04:19:20,000 then 6701 04:19:16,880 --> 04:19:22,560 we have the next one pre-order how does 6702 04:19:20,000 --> 04:19:25,520 this pre-order will work it has the same 6703 04:19:22,560 --> 04:19:28,000 rule but in a shuffled manner first you 6704 04:19:25,520 --> 04:19:29,760 are visiting the root 6705 04:19:28,000 --> 04:19:32,000 then you are visiting 6706 04:19:29,760 --> 04:19:35,120 left side of the 6707 04:19:32,000 --> 04:19:37,840 tree and then you are going towards the 6708 04:19:35,120 --> 04:19:40,880 right side hope that's clear right and 6709 04:19:37,840 --> 04:19:43,199 the last one which is left is post order 6710 04:19:40,880 --> 04:19:45,760 so what is post order again you have a 6711 04:19:43,199 --> 04:19:48,319 shuffled way of this three combination 6712 04:19:45,760 --> 04:19:49,760 only the first thing is it will go to 6713 04:19:48,319 --> 04:19:51,840 left 6714 04:19:49,760 --> 04:19:53,040 that means left side of the 6715 04:19:51,840 --> 04:19:54,960 particular 6716 04:19:53,040 --> 04:19:57,120 diagram or the binary tree which you 6717 04:19:54,960 --> 04:20:01,199 have taken and then it will go to the 6718 04:19:57,120 --> 04:20:03,600 right after that it will go to root 6719 04:20:01,199 --> 04:20:05,840 so this is the basic 6720 04:20:03,600 --> 04:20:07,680 things you have to know on 6721 04:20:05,840 --> 04:20:10,159 these three functions or operations 6722 04:20:07,680 --> 04:20:13,439 which you do it generally in binary tree 6723 04:20:10,159 --> 04:20:16,640 so in order pre-order and post order so 6724 04:20:13,439 --> 04:20:18,479 let's quickly start with in order right 6725 04:20:16,640 --> 04:20:21,279 so in order 6726 04:20:18,479 --> 04:20:24,000 it says we have to visit to the left 6727 04:20:21,279 --> 04:20:26,960 first then go towards the root and then 6728 04:20:24,000 --> 04:20:30,880 come back to right so i'm implementing 6729 04:20:26,960 --> 04:20:33,120 the same so here what is the left most 6730 04:20:30,880 --> 04:20:35,760 node we have for this particular example 6731 04:20:33,120 --> 04:20:39,120 simple example we have taken so that is 6732 04:20:35,760 --> 04:20:42,399 4 if you could see right so this is the 6733 04:20:39,120 --> 04:20:44,800 most and this is the right most and this 6734 04:20:42,399 --> 04:20:45,600 is the root just for reference 6735 04:20:44,800 --> 04:20:47,920 right 6736 04:20:45,600 --> 04:20:50,960 so at the left what do you have 6737 04:20:47,920 --> 04:20:52,000 for so for in order i'm writing it here 6738 04:20:50,960 --> 04:20:54,960 as well 6739 04:20:52,000 --> 04:20:57,199 so in order you will put up 6740 04:20:54,960 --> 04:20:59,279 4 as the first element because the rule 6741 04:20:57,199 --> 04:21:02,880 says you have to go towards the left of 6742 04:20:59,279 --> 04:21:03,760 the binary tree then after 4 what do you 6743 04:21:02,880 --> 04:21:05,279 have 6744 04:21:03,760 --> 04:21:08,479 this direction 6745 04:21:05,279 --> 04:21:09,359 you have 2 you have to first complete 6746 04:21:08,479 --> 04:21:11,840 the 6747 04:21:09,359 --> 04:21:14,640 left side part then you have to go to 6748 04:21:11,840 --> 04:21:16,640 the root and next 6749 04:21:14,640 --> 04:21:19,840 on the way what do you find you find 6750 04:21:16,640 --> 04:21:22,399 root so what is the number in the root 6751 04:21:19,840 --> 04:21:24,800 is one and then after coming back 6752 04:21:22,399 --> 04:21:28,319 towards right side so what is the number 6753 04:21:24,800 --> 04:21:28,319 we have we have 3 6754 04:21:29,920 --> 04:21:33,840 so 6755 04:21:30,720 --> 04:21:36,960 this is how in order will work so hope 6756 04:21:33,840 --> 04:21:39,840 that's clear we are starting from left 6757 04:21:36,960 --> 04:21:41,359 we are going towards the root and we are 6758 04:21:39,840 --> 04:21:43,760 coming back 6759 04:21:41,359 --> 04:21:45,439 towards the right side 6760 04:21:43,760 --> 04:21:47,760 so again 6761 04:21:45,439 --> 04:21:49,359 how do you do it for pre-order the next 6762 04:21:47,760 --> 04:21:51,680 one 6763 04:21:49,359 --> 04:21:55,600 so what does preorder say you have to 6764 04:21:51,680 --> 04:21:57,600 first visit root then go to the left and 6765 04:21:55,600 --> 04:21:59,199 then go to right 6766 04:21:57,600 --> 04:22:00,960 so 6767 04:21:59,199 --> 04:22:03,040 i'm going to root here in the same 6768 04:22:00,960 --> 04:22:05,920 diagram i'm drawing one more here for 6769 04:22:03,040 --> 04:22:08,000 your reference so this is 2 again this 6770 04:22:05,920 --> 04:22:09,920 will be 4 and this is 3. hope that's 6771 04:22:08,000 --> 04:22:10,880 clear so we are working for pre-order 6772 04:22:09,920 --> 04:22:13,120 here 6773 04:22:10,880 --> 04:22:16,399 it says we have to go for root first so 6774 04:22:13,120 --> 04:22:17,600 what is the element the node that is 6775 04:22:16,399 --> 04:22:19,840 1 6776 04:22:17,600 --> 04:22:22,319 okay so next it says you have to go 6777 04:22:19,840 --> 04:22:23,600 towards the left so what is there in the 6778 04:22:22,319 --> 04:22:26,159 left 6779 04:22:23,600 --> 04:22:28,399 you the side you have two first and then 6780 04:22:26,159 --> 04:22:30,159 four so what i'm writing it's two here 6781 04:22:28,399 --> 04:22:31,120 and four next 6782 04:22:30,159 --> 04:22:34,000 then 6783 04:22:31,120 --> 04:22:36,479 you have to visit right side so 6784 04:22:34,000 --> 04:22:39,439 at writing you have three 6785 04:22:36,479 --> 04:22:41,600 so this is how pre-order traversal will 6786 04:22:39,439 --> 04:22:43,359 work in binary tree 6787 04:22:41,600 --> 04:22:46,239 so quickly we shall see the last one 6788 04:22:43,359 --> 04:22:47,279 that is post order 6789 04:22:46,239 --> 04:22:49,359 right 6790 04:22:47,279 --> 04:22:51,439 post order as per the rules what does it 6791 04:22:49,359 --> 04:22:54,479 say we have to go towards the left first 6792 04:22:51,439 --> 04:22:57,040 then right and then root at the last so 6793 04:22:54,479 --> 04:22:57,760 writing down a simple diagram again here 6794 04:22:57,040 --> 04:23:00,560 for 6795 04:22:57,760 --> 04:23:01,439 avoiding the confusion 6796 04:23:00,560 --> 04:23:04,880 for 6797 04:23:01,439 --> 04:23:08,399 this is 3 okay so we are doing that 6798 04:23:04,880 --> 04:23:10,640 left side what do you have now we have 4 6799 04:23:08,399 --> 04:23:13,120 again continuing 6800 04:23:10,640 --> 04:23:13,120 you have 6801 04:23:13,279 --> 04:23:17,120 towards the right side 6802 04:23:14,880 --> 04:23:17,120 3 6803 04:23:19,199 --> 04:23:25,199 right after having 3 what do you have 6804 04:23:22,399 --> 04:23:26,080 you have to go to root so what does it 6805 04:23:25,199 --> 04:23:27,840 say 6806 04:23:26,080 --> 04:23:29,920 you have to first complete whatever 6807 04:23:27,840 --> 04:23:31,520 there in the left so what we have left 6808 04:23:29,920 --> 04:23:33,600 out here we have left the two because it 6809 04:23:31,520 --> 04:23:36,159 is also on the left side so what we'll 6810 04:23:33,600 --> 04:23:39,359 do here right we'll try to put up this 6811 04:23:36,159 --> 04:23:42,960 two here finish it off that is four two 6812 04:23:39,359 --> 04:23:45,279 three and then you have to go to one so 6813 04:23:42,960 --> 04:23:47,600 first what did i do i went to the left 6814 04:23:45,279 --> 04:23:48,720 path that is four so 6815 04:23:47,600 --> 04:23:51,840 after that 6816 04:23:48,720 --> 04:23:53,680 you have two and then you have to go to 6817 04:23:51,840 --> 04:23:54,560 right and then root 6818 04:23:53,680 --> 04:23:56,800 so 6819 04:23:54,560 --> 04:23:58,880 this is the root we can't go there until 6820 04:23:56,800 --> 04:24:01,040 unless we finish towards the right side 6821 04:23:58,880 --> 04:24:04,080 towards the right we had 3 and then we 6822 04:24:01,040 --> 04:24:08,399 went to root back so that makes 4 6823 04:24:04,080 --> 04:24:10,960 2 3 1 as a post order hope this was 6824 04:24:08,399 --> 04:24:13,680 clear for you and now let's quickly hop 6825 04:24:10,960 --> 04:24:15,920 into the coding part and check out the 6826 04:24:13,680 --> 04:24:17,439 same elements the same functions how 6827 04:24:15,920 --> 04:24:20,080 does that work 6828 04:24:17,439 --> 04:24:22,000 so what is happening here is the same 6829 04:24:20,080 --> 04:24:23,279 example i've taken in a coding format 6830 04:24:22,000 --> 04:24:24,960 that is where i'm taking the four 6831 04:24:23,279 --> 04:24:27,199 different notes that is one two three 6832 04:24:24,960 --> 04:24:29,600 four and applying all the three 6833 04:24:27,199 --> 04:24:32,239 functions we have that is inorder post 6834 04:24:29,600 --> 04:24:35,359 order and pre-order with the same 6835 04:24:32,239 --> 04:24:37,040 way of working but in a coding manner so 6836 04:24:35,359 --> 04:24:39,439 what has happened here the first if you 6837 04:24:37,040 --> 04:24:41,680 could see they have put up a class that 6838 04:24:39,439 --> 04:24:44,399 is node right so 6839 04:24:41,680 --> 04:24:47,680 after creating a class node we have to 6840 04:24:44,399 --> 04:24:50,640 initialize the values value is v l is 6841 04:24:47,680 --> 04:24:53,520 nothing but root right and left so we 6842 04:24:50,640 --> 04:24:55,920 have to have one single root node and we 6843 04:24:53,520 --> 04:24:58,479 have to have left and right for the same 6844 04:24:55,920 --> 04:24:59,920 root so then we will create some 6845 04:24:58,479 --> 04:25:02,479 functions for 6846 04:24:59,920 --> 04:25:04,880 traversing towards pre-order traverse it 6847 04:25:02,479 --> 04:25:06,800 towards inorder traverse it towards post 6848 04:25:04,880 --> 04:25:09,199 order to make it clear and precise 6849 04:25:06,800 --> 04:25:11,120 enough i have kept related name to the 6850 04:25:09,199 --> 04:25:13,199 functions and classes here 6851 04:25:11,120 --> 04:25:15,279 so what happens here is we'll be using 6852 04:25:13,199 --> 04:25:16,000 the functions called preorder post order 6853 04:25:15,279 --> 04:25:18,159 and 6854 04:25:16,000 --> 04:25:19,760 in order so 6855 04:25:18,159 --> 04:25:22,640 first it will visit the nodes 6856 04:25:19,760 --> 04:25:24,880 accordingly as i told you so first for 6857 04:25:22,640 --> 04:25:27,439 pre-order it will go to root left right 6858 04:25:24,880 --> 04:25:29,520 and for in order it will go for left 6859 04:25:27,439 --> 04:25:31,520 root right and again for post order it 6860 04:25:29,520 --> 04:25:34,239 will go for left 6861 04:25:31,520 --> 04:25:37,040 right and root so let's check out how 6862 04:25:34,239 --> 04:25:38,960 does this program work in the manner so 6863 04:25:37,040 --> 04:25:41,439 here you could see i've used the left 6864 04:25:38,960 --> 04:25:43,439 and right accordingly so as per the 6865 04:25:41,439 --> 04:25:46,159 rules i've told you that has been used 6866 04:25:43,439 --> 04:25:48,000 here as well so let me quickly run the 6867 04:25:46,159 --> 04:25:51,199 program for you and check out what is 6868 04:25:48,000 --> 04:25:51,199 the output we get 6869 04:25:54,000 --> 04:26:00,720 right if you could see here pre-order we 6870 04:25:57,279 --> 04:26:03,920 got it as one two four three the reason 6871 04:26:00,720 --> 04:26:06,560 is because it is going from root left 6872 04:26:03,920 --> 04:26:09,279 and right and for in order we are going 6873 04:26:06,560 --> 04:26:12,239 with left root and right so left side we 6874 04:26:09,279 --> 04:26:14,479 had 4 2 and again 6875 04:26:12,239 --> 04:26:17,040 1 was the root node and 3 was at the 6876 04:26:14,479 --> 04:26:20,000 right so left root and right if you go 6877 04:26:17,040 --> 04:26:23,439 for post order traversal then it will 6878 04:26:20,000 --> 04:26:26,800 start from left right and root so left 6879 04:26:23,439 --> 04:26:30,159 most was 4 2 and it went to right right 6880 04:26:26,800 --> 04:26:33,199 side was 3 and root is 1 so this is how 6881 04:26:30,159 --> 04:26:36,000 the traversal will work in binary tree 6882 04:26:33,199 --> 04:26:38,399 in python now let's talk about advantage 6883 04:26:36,000 --> 04:26:40,880 and disadvantage of binary tree if you 6884 04:26:38,399 --> 04:26:42,960 talk about advantages we can represent 6885 04:26:40,880 --> 04:26:44,800 data with some relationship right so 6886 04:26:42,960 --> 04:26:46,880 this is the root node or this is the 6887 04:26:44,800 --> 04:26:48,560 parent node and this is the child node 6888 04:26:46,880 --> 04:26:50,800 so that relationship parent chat 6889 04:26:48,560 --> 04:26:53,040 relationship is always there and while 6890 04:26:50,800 --> 04:26:56,239 you are inserting or searching an 6891 04:26:53,040 --> 04:26:59,040 element or a node in a 6892 04:26:56,239 --> 04:27:01,359 by imagery it is much more efficient as 6893 04:26:59,040 --> 04:27:03,199 compared to other data structures which 6894 04:27:01,359 --> 04:27:04,960 we have seen 6895 04:27:03,199 --> 04:27:08,000 now if you talk about disadvantage if 6896 04:27:04,960 --> 04:27:11,120 you want to sort this binary tree it is 6897 04:27:08,000 --> 04:27:13,040 much more difficult right because we can 6898 04:27:11,120 --> 04:27:14,239 obviously we can either go on the left 6899 04:27:13,040 --> 04:27:16,159 side 6900 04:27:14,239 --> 04:27:18,239 this can be a tree that means this is 6901 04:27:16,159 --> 04:27:20,159 the left skew tree or this can be the 6902 04:27:18,239 --> 04:27:22,159 scenario where we have all the elements 6903 04:27:20,159 --> 04:27:24,399 on the right side of the node so this 6904 04:27:22,159 --> 04:27:27,040 can be another scenario wherein we have 6905 04:27:24,399 --> 04:27:29,520 this right skewed binary tree so sorting 6906 04:27:27,040 --> 04:27:31,840 is difficult and it is not much more 6907 04:27:29,520 --> 04:27:34,560 much flexible right if you talk about 6908 04:27:31,840 --> 04:27:36,880 this binary tree we always have either 6909 04:27:34,560 --> 04:27:39,760 the two children or one children or no 6910 04:27:36,880 --> 04:27:42,000 children there is no flexibility in 6911 04:27:39,760 --> 04:27:44,399 terms of how you want to store it can we 6912 04:27:42,000 --> 04:27:46,319 store another node on the 6913 04:27:44,399 --> 04:27:48,560 with this root node no that is not the 6914 04:27:46,319 --> 04:27:51,120 case right and we don't follow any 6915 04:27:48,560 --> 04:27:53,600 restrictions like okay we always find 6916 04:27:51,120 --> 04:27:56,399 this node that is if this is a root note 6917 04:27:53,600 --> 04:27:58,319 we always find uh an element which is 6918 04:27:56,399 --> 04:28:00,560 less than this node on the left side or 6919 04:27:58,319 --> 04:28:03,120 on the right side that is of the greater 6920 04:28:00,560 --> 04:28:04,960 element so that is that flexibility is 6921 04:28:03,120 --> 04:28:07,120 not there 6922 04:28:04,960 --> 04:28:09,520 binary surgery 6923 04:28:07,120 --> 04:28:12,239 now what is binary search tree so binary 6924 04:28:09,520 --> 04:28:14,560 tree is an additional restriction right 6925 04:28:12,239 --> 04:28:17,120 this bst is nothing but the additional 6926 04:28:14,560 --> 04:28:19,120 restriction on binary tree like we have 6927 04:28:17,120 --> 04:28:21,439 already seen the evaluatory where at 6928 04:28:19,120 --> 04:28:23,120 most we can have two children's right it 6929 04:28:21,439 --> 04:28:24,960 can be on the left side or it can be on 6930 04:28:23,120 --> 04:28:26,880 the right side now what are the 6931 04:28:24,960 --> 04:28:29,520 restrictions on this tree so the 6932 04:28:26,880 --> 04:28:31,840 restrictions are that data that is let's 6933 04:28:29,520 --> 04:28:33,359 suppose this is five and this is these 6934 04:28:31,840 --> 04:28:35,199 are another two nodes now the 6935 04:28:33,359 --> 04:28:37,680 restriction is that if this is a root 6936 04:28:35,199 --> 04:28:40,239 node then you always will have the 6937 04:28:37,680 --> 04:28:44,000 element that is less than your root node 6938 04:28:40,239 --> 04:28:46,159 so you can have four three two one 6939 04:28:44,000 --> 04:28:48,640 on these sides but you cannot have six 6940 04:28:46,159 --> 04:28:51,279 on the left side similarly you can have 6941 04:28:48,640 --> 04:28:53,279 six seven eight nine and so on on the 6942 04:28:51,279 --> 04:28:55,600 right hand side you cannot have one two 6943 04:28:53,279 --> 04:28:58,000 three on the right hand side so these 6944 04:28:55,600 --> 04:29:00,239 are the two restrictions that are there 6945 04:28:58,000 --> 04:29:02,399 in binary search tree now if you talk 6946 04:29:00,239 --> 04:29:05,040 about insertion deletion and while you 6947 04:29:02,399 --> 04:29:08,239 are searching the element in the binary 6948 04:29:05,040 --> 04:29:12,159 search tree it is much more efficient as 6949 04:29:08,239 --> 04:29:14,080 compared to binary tree because we can 6950 04:29:12,159 --> 04:29:16,399 obviously when we are trying to search 6951 04:29:14,080 --> 04:29:18,399 for an element we can obviously let's 6952 04:29:16,399 --> 04:29:20,479 suppose we are searching for eight so 6953 04:29:18,399 --> 04:29:23,120 will it make sense to go on the left 6954 04:29:20,479 --> 04:29:25,439 hand side no we can easily neglect this 6955 04:29:23,120 --> 04:29:27,359 part similarly we can do it on the rest 6956 04:29:25,439 --> 04:29:29,760 of the element this is one scenario 6957 04:29:27,359 --> 04:29:31,920 wherein searching is much more efficient 6958 04:29:29,760 --> 04:29:34,080 than binary tree because in binary tree 6959 04:29:31,920 --> 04:29:36,479 we cannot neglect any elements or we 6960 04:29:34,080 --> 04:29:38,560 cannot say okay now we cannot find an 6961 04:29:36,479 --> 04:29:41,279 element on this portion on that portion 6962 04:29:38,560 --> 04:29:44,000 similarly when we are inserting now if 6963 04:29:41,279 --> 04:29:45,040 the element is greater than 8 so we can 6964 04:29:44,000 --> 04:29:46,960 easily 6965 04:29:45,040 --> 04:29:48,560 in this scenario right we can easily go 6966 04:29:46,960 --> 04:29:50,720 on the right hand side rather than 6967 04:29:48,560 --> 04:29:52,800 searching where we want to insert 6968 04:29:50,720 --> 04:29:54,800 and similarly when we are deleting a 6969 04:29:52,800 --> 04:29:55,600 little bit of ordering is 6970 04:29:54,800 --> 04:29:58,319 done 6971 04:29:55,600 --> 04:30:01,760 in order to get the exact same situation 6972 04:29:58,319 --> 04:30:03,920 wherein your root is less than 6973 04:30:01,760 --> 04:30:07,120 the right hand side elements and wherein 6974 04:30:03,920 --> 04:30:10,479 your root is greater than the left hand 6975 04:30:07,120 --> 04:30:13,120 side elements so that scenario is there 6976 04:30:10,479 --> 04:30:15,920 in the binary search tree now let's talk 6977 04:30:13,120 --> 04:30:18,239 about the application it is suitable for 6978 04:30:15,920 --> 04:30:20,159 applications which require sorted 6979 04:30:18,239 --> 04:30:22,159 hierarchical data let's suppose you have 6980 04:30:20,159 --> 04:30:26,000 some data and you want to store them in 6981 04:30:22,159 --> 04:30:28,560 such a way that always you want your 6982 04:30:26,000 --> 04:30:31,439 elements that are greater than the root 6983 04:30:28,560 --> 04:30:32,960 on the left on the right hand side 6984 04:30:31,439 --> 04:30:34,720 right you want them on the right hand 6985 04:30:32,960 --> 04:30:36,720 side and 6986 04:30:34,720 --> 04:30:38,880 for the elements which are less than 6987 04:30:36,720 --> 04:30:41,040 this root you want them on the right 6988 04:30:38,880 --> 04:30:43,199 hand side okay so that you can easily 6989 04:30:41,040 --> 04:30:45,359 neglect okay you can now let's suppose 6990 04:30:43,199 --> 04:30:46,720 not if you want don't want this scenario 6991 04:30:45,359 --> 04:30:48,399 where you are just dealing with the 6992 04:30:46,720 --> 04:30:50,239 greater than or the less than you can 6993 04:30:48,399 --> 04:30:52,399 have a similar kind of a situation 6994 04:30:50,239 --> 04:30:54,560 wherein you are saying that okay on this 6995 04:30:52,399 --> 04:30:56,800 side i will always have some files that 6996 04:30:54,560 --> 04:30:59,040 are related to coding okay let's say an 6997 04:30:56,800 --> 04:31:00,640 example and in this we i will always 6998 04:30:59,040 --> 04:31:02,800 have files which are related to 6999 04:31:00,640 --> 04:31:04,720 entertainment so you can do that so that 7000 04:31:02,800 --> 04:31:06,319 you can easily neglect these files 7001 04:31:04,720 --> 04:31:08,319 because these are always going to be 7002 04:31:06,319 --> 04:31:11,120 your coding files and these are always 7003 04:31:08,319 --> 04:31:13,279 going to be name what your entertainment 7004 04:31:11,120 --> 04:31:15,600 files so wherever you require sorting 7005 04:31:13,279 --> 04:31:18,239 right or you want your help you want 7006 04:31:15,600 --> 04:31:20,319 your data to be in such a manner that it 7007 04:31:18,239 --> 04:31:21,359 is easy for you to find insert and 7008 04:31:20,319 --> 04:31:23,680 delete 7009 04:31:21,359 --> 04:31:26,239 so there in your binary search tree 7010 04:31:23,680 --> 04:31:28,319 comes into picture after learning what 7011 04:31:26,239 --> 04:31:31,439 is binary search tree right we have to 7012 04:31:28,319 --> 04:31:34,080 implement that particular topic here in 7013 04:31:31,439 --> 04:31:35,840 python on google collab so before that 7014 04:31:34,080 --> 04:31:38,080 let's understand what are the different 7015 04:31:35,840 --> 04:31:40,399 function you'll see in binary search 7016 04:31:38,080 --> 04:31:41,680 tree so it is very basic function that 7017 04:31:40,399 --> 04:31:44,319 is insert 7018 04:31:41,680 --> 04:31:46,720 search and delete so before going to 7019 04:31:44,319 --> 04:31:47,520 these functions i need to let you know 7020 04:31:46,720 --> 04:31:49,920 that 7021 04:31:47,520 --> 04:31:52,080 how do you draw a binary search tree you 7022 04:31:49,920 --> 04:31:54,960 have a specific condition to be followed 7023 04:31:52,080 --> 04:31:57,760 in order to have a binary search tree so 7024 04:31:54,960 --> 04:32:00,640 number one you have to have the root 7025 04:31:57,760 --> 04:32:02,319 node should be always greater whatever 7026 04:32:00,640 --> 04:32:04,239 the sub nodes which is lesser than the 7027 04:32:02,319 --> 04:32:06,399 root node will be towards the left side 7028 04:32:04,239 --> 04:32:08,080 and if it is greater than the root node 7029 04:32:06,399 --> 04:32:09,199 it will be on the right side you have to 7030 04:32:08,080 --> 04:32:12,479 consider 7031 04:32:09,199 --> 04:32:15,359 these two conditions before writing 7032 04:32:12,479 --> 04:32:18,479 binary search tree right so i'll just 7033 04:32:15,359 --> 04:32:20,640 give you a simple example of a binary 7034 04:32:18,479 --> 04:32:24,319 search tree keeping these conditions in 7035 04:32:20,640 --> 04:32:26,399 point the first condition i told you is 7036 04:32:24,319 --> 04:32:29,120 the root 7037 04:32:26,399 --> 04:32:32,319 node should be taken into consideration 7038 04:32:29,120 --> 04:32:34,399 whatever the root node is for example 7039 04:32:32,319 --> 04:32:37,120 the root node is 10 7040 04:32:34,399 --> 04:32:38,479 okay anything below 10 it should come 7041 04:32:37,120 --> 04:32:41,600 towards the 7042 04:32:38,479 --> 04:32:44,640 left side of the root node anything 7043 04:32:41,600 --> 04:32:46,880 about 10 11 12 13 and so on it should 7044 04:32:44,640 --> 04:32:49,199 come towards the right side so whatever 7045 04:32:46,880 --> 04:32:51,199 it is greater should come here towards 7046 04:32:49,199 --> 04:32:52,560 the right what is lesser should come 7047 04:32:51,199 --> 04:32:54,239 towards the 7048 04:32:52,560 --> 04:32:56,640 left side so keeping this into 7049 04:32:54,239 --> 04:32:59,359 consideration 10 is the root note i'm 7050 04:32:56,640 --> 04:33:02,239 taking and anything you want to add on 7051 04:32:59,359 --> 04:33:04,000 right so i'm adding 5 when you're adding 7052 04:33:02,239 --> 04:33:06,320 lesser than the root node that should be 7053 04:33:04,000 --> 04:33:08,639 towards the left side and if you are 7054 04:33:06,320 --> 04:33:10,799 adding anything greater than to the root 7055 04:33:08,639 --> 04:33:16,080 node that should be towards the 7056 04:33:10,799 --> 04:33:19,039 right side so i'm adding 12 here then 7057 04:33:16,080 --> 04:33:21,039 after that what you have to do is 7058 04:33:19,039 --> 04:33:23,840 if you want to continue to the next 7059 04:33:21,039 --> 04:33:25,039 level right you have to follow the same 7060 04:33:23,840 --> 04:33:27,840 condition 7061 04:33:25,039 --> 04:33:30,480 for now just for reference 5 is the root 7062 04:33:27,840 --> 04:33:32,959 node anything lesser than 5 7063 04:33:30,480 --> 04:33:34,959 should be towards the left side anything 7064 04:33:32,959 --> 04:33:37,119 greater than 5 should be 7065 04:33:34,959 --> 04:33:40,480 towards the 7066 04:33:37,119 --> 04:33:42,959 right side right again if you take 7067 04:33:40,480 --> 04:33:45,760 7 into consideration right as a root 7068 04:33:42,959 --> 04:33:47,920 node anything towards the 7069 04:33:45,760 --> 04:33:50,320 left side will be having the lesser 7070 04:33:47,920 --> 04:33:50,320 value 7071 04:33:50,561 --> 04:33:54,160 you can keep it as six 7072 04:33:52,240 --> 04:33:56,400 anything towards the right side will be 7073 04:33:54,160 --> 04:33:57,680 having the greater value you can keep it 7074 04:33:56,400 --> 04:33:58,639 as eight 7075 04:33:57,680 --> 04:34:00,000 so 7076 04:33:58,639 --> 04:34:04,000 this is how 7077 04:34:00,000 --> 04:34:06,959 particular bst tree will work so this is 7078 04:34:04,000 --> 04:34:09,119 a simple example for binary search tree 7079 04:34:06,959 --> 04:34:11,279 so let's understand how does the 7080 04:34:09,119 --> 04:34:14,000 searching will happen with the help of 7081 04:34:11,279 --> 04:34:17,039 pseudocode and proceeding with 7082 04:34:14,000 --> 04:34:21,199 insert and then delete right 7083 04:34:17,039 --> 04:34:23,760 so let's see search operation first 7084 04:34:21,199 --> 04:34:26,480 so what happens in search 7085 04:34:23,760 --> 04:34:29,600 the first thing is 7086 04:34:26,480 --> 04:34:31,119 root node should be considered if 7087 04:34:29,600 --> 04:34:33,279 root node 7088 04:34:31,119 --> 04:34:36,000 is equal to null 7089 04:34:33,279 --> 04:34:38,320 it is not having any value 7090 04:34:36,000 --> 04:34:39,920 then we will be returning 7091 04:34:38,320 --> 04:34:41,119 null 7092 04:34:39,920 --> 04:34:44,400 okay 7093 04:34:41,119 --> 04:34:46,719 if the same thing again 7094 04:34:44,400 --> 04:34:48,799 number or the value or the data i'm 7095 04:34:46,719 --> 04:34:51,039 taking number here whatever the user 7096 04:34:48,799 --> 04:34:55,119 input is right if the number which is 7097 04:34:51,039 --> 04:34:58,400 given is equal to root the values of 7098 04:34:55,119 --> 04:35:00,959 root and the input number is same then 7099 04:34:58,400 --> 04:35:05,840 you have to return 7100 04:35:00,959 --> 04:35:06,879 root only okay the next condition if 7101 04:35:05,840 --> 04:35:09,760 number 7102 04:35:06,879 --> 04:35:12,719 is less than root as i told you 7103 04:35:09,760 --> 04:35:13,680 whatever it is less than root value the 7104 04:35:12,719 --> 04:35:16,639 number 7105 04:35:13,680 --> 04:35:18,719 should go to the left side of the tree 7106 04:35:16,639 --> 04:35:20,080 so what do you write and you can write 7107 04:35:18,719 --> 04:35:22,879 it as 7108 04:35:20,080 --> 04:35:24,320 return 7109 04:35:22,879 --> 04:35:26,240 search 7110 04:35:24,320 --> 04:35:28,320 i mean search will traverse towards the 7111 04:35:26,240 --> 04:35:30,719 left side any element you are finding 7112 04:35:28,320 --> 04:35:36,000 over a tree right you are finding uh for 7113 04:35:30,719 --> 04:35:39,359 example for number four right so in our 7114 04:35:36,000 --> 04:35:41,439 tree here like this anything like this 7115 04:35:39,359 --> 04:35:43,439 say you have different 7116 04:35:41,439 --> 04:35:45,920 values 7117 04:35:43,439 --> 04:35:48,561 here i am not writing values as of now 7118 04:35:45,920 --> 04:35:51,840 so the 4 is situated here so what does 7119 04:35:48,561 --> 04:35:54,959 it say it it goes in comparison first it 7120 04:35:51,840 --> 04:35:57,279 will search for root value is it equal 7121 04:35:54,959 --> 04:35:59,760 is it null no then it will search 7122 04:35:57,279 --> 04:36:02,959 whether it is lesser then the next thing 7123 04:35:59,760 --> 04:36:06,959 which it has to search is whether it is 7124 04:36:02,959 --> 04:36:10,240 greater if the number is greater than 7125 04:36:06,959 --> 04:36:11,039 the root value right then it has to go 7126 04:36:10,240 --> 04:36:13,840 to 7127 04:36:11,039 --> 04:36:16,400 the right side of the tree here it is 7128 04:36:13,840 --> 04:36:18,561 left side and it has to go to right side 7129 04:36:16,400 --> 04:36:21,359 if you want to search anything so that 7130 04:36:18,561 --> 04:36:24,320 is how a simple pseudo code presents 7131 04:36:21,359 --> 04:36:27,439 here how do you search an element inside 7132 04:36:24,320 --> 04:36:29,439 a binary search tree so the next part 7133 04:36:27,439 --> 04:36:30,240 which you have to keep in consideration 7134 04:36:29,439 --> 04:36:32,561 is 7135 04:36:30,240 --> 04:36:34,561 how do you insert element again the 7136 04:36:32,561 --> 04:36:36,561 searching follows the same manner 7137 04:36:34,561 --> 04:36:38,000 likewise the incision will also follow 7138 04:36:36,561 --> 04:36:40,240 some 7139 04:36:38,000 --> 04:36:42,799 similar manner so in order to insert 7140 04:36:40,240 --> 04:36:45,439 anything inside to bst that is binary 7141 04:36:42,799 --> 04:36:49,680 search tree what do you do first you 7142 04:36:45,439 --> 04:36:49,680 have to check for the condition again if 7143 04:36:50,639 --> 04:36:56,400 node is equal to is equal to null 7144 04:36:53,199 --> 04:36:58,639 right if node is equal to null and then 7145 04:36:56,400 --> 04:36:59,840 it will return whatever the data it is 7146 04:36:58,639 --> 04:37:00,719 as it is 7147 04:36:59,840 --> 04:37:01,760 if 7148 04:37:00,719 --> 04:37:04,639 again 7149 04:37:01,760 --> 04:37:06,719 whatever the data which is given right 7150 04:37:04,639 --> 04:37:09,359 by the user is 7151 04:37:06,719 --> 04:37:11,840 lesser than node 7152 04:37:09,359 --> 04:37:12,879 then it will go towards the left side of 7153 04:37:11,840 --> 04:37:15,760 it 7154 04:37:12,879 --> 04:37:18,561 if there's one more condition the data 7155 04:37:15,760 --> 04:37:20,240 or the value which is given by the 7156 04:37:18,561 --> 04:37:22,719 user is 7157 04:37:20,240 --> 04:37:25,439 greater than the node value 7158 04:37:22,719 --> 04:37:27,199 then what it will do it goes towards the 7159 04:37:25,439 --> 04:37:30,240 right side and we 7160 04:37:27,199 --> 04:37:34,160 insert element accordingly as per the 7161 04:37:30,240 --> 04:37:36,719 conditions it is satisfying right so 7162 04:37:34,160 --> 04:37:40,240 this is about search and incision so 7163 04:37:36,719 --> 04:37:42,879 coming back to deletion right 7164 04:37:40,240 --> 04:37:45,680 let's see how do you delete an element 7165 04:37:42,879 --> 04:37:48,400 from the binary search tree 7166 04:37:45,680 --> 04:37:50,561 deleting has three different cases to be 7167 04:37:48,400 --> 04:37:53,600 considered case one 7168 04:37:50,561 --> 04:37:54,799 okay say for example you have a tree 7169 04:37:53,600 --> 04:37:58,799 here 7170 04:37:54,799 --> 04:38:02,561 which is having certain value right 7171 04:37:58,799 --> 04:38:02,561 i'm just giving a simple example 7172 04:38:03,039 --> 04:38:07,359 this is 8 again it has to be less 7173 04:38:06,000 --> 04:38:09,279 whatever it is less will be here 7174 04:38:07,359 --> 04:38:11,680 whatever it is greater than will be here 7175 04:38:09,279 --> 04:38:14,160 so if you are taking this as an example 7176 04:38:11,680 --> 04:38:15,840 right the case one if you want to delete 7177 04:38:14,160 --> 04:38:19,520 nine okay 7178 04:38:15,840 --> 04:38:20,799 what it is the case one states that 7179 04:38:19,520 --> 04:38:22,320 directly 7180 04:38:20,799 --> 04:38:25,359 delete 7181 04:38:22,320 --> 04:38:28,080 need not do anything go to that node if 7182 04:38:25,359 --> 04:38:29,840 you want to delete nine get it back and 7183 04:38:28,080 --> 04:38:31,439 delete it right 7184 04:38:29,840 --> 04:38:33,920 case two 7185 04:38:31,439 --> 04:38:37,039 you have one more condition to consider 7186 04:38:33,920 --> 04:38:39,039 so we have to replace something 7187 04:38:37,039 --> 04:38:39,920 we have to replace 7188 04:38:39,039 --> 04:38:42,160 the 7189 04:38:39,920 --> 04:38:43,760 node 7190 04:38:42,160 --> 04:38:45,920 with 7191 04:38:43,760 --> 04:38:48,320 its child 7192 04:38:45,920 --> 04:38:50,639 we have to replace the node with this 7193 04:38:48,320 --> 04:38:52,639 child whatever it is there accordingly 7194 04:38:50,639 --> 04:38:54,719 say for example if you want to delete 8 7195 04:38:52,639 --> 04:38:58,160 you have to replace 8 with 7196 04:38:54,719 --> 04:38:59,199 9 or 6 i think you have to 7197 04:38:58,160 --> 04:39:01,199 use 7198 04:38:59,199 --> 04:39:03,439 in order method in order to find which 7199 04:39:01,199 --> 04:39:04,959 one you have to replace because 8 has 2 7200 04:39:03,439 --> 04:39:08,439 children 7201 04:39:04,959 --> 04:39:10,000 coming up to the third case 7202 04:39:08,439 --> 04:39:12,879 particularly 7203 04:39:10,000 --> 04:39:15,920 we have to follow in order method 7204 04:39:12,879 --> 04:39:17,840 right so in order traversal should be 7205 04:39:15,920 --> 04:39:20,959 followed and we have to check 7206 04:39:17,840 --> 04:39:22,320 accordingly which one to replace and 7207 04:39:20,959 --> 04:39:24,000 what to delete 7208 04:39:22,320 --> 04:39:26,320 right without replacement we cannot 7209 04:39:24,000 --> 04:39:28,719 delete and leave that particular node 7210 04:39:26,320 --> 04:39:31,520 empty so these are the three cases you 7211 04:39:28,719 --> 04:39:34,719 have in order to delete an element from 7212 04:39:31,520 --> 04:39:36,080 a binary search tree so let's quickly 7213 04:39:34,719 --> 04:39:38,160 see 7214 04:39:36,080 --> 04:39:41,520 how do you implement this particular 7215 04:39:38,160 --> 04:39:41,520 code on google 7216 04:39:41,760 --> 04:39:48,000 now let's implement the same bsd 7217 04:39:44,718 --> 04:39:50,480 functions in python on google collab so 7218 04:39:48,000 --> 04:39:53,120 the first thing which you have to do is 7219 04:39:50,480 --> 04:39:55,760 you have to initialize a class so i'm 7220 04:39:53,120 --> 04:39:57,840 initializing a class called node here 7221 04:39:55,760 --> 04:40:00,480 and i'm considering three different 7222 04:39:57,840 --> 04:40:03,040 elements one is key key is nothing but 7223 04:40:00,480 --> 04:40:05,520 root and left and right 7224 04:40:03,040 --> 04:40:07,920 for the particular tree 7225 04:40:05,520 --> 04:40:10,240 and next i'm considering one function 7226 04:40:07,920 --> 04:40:12,560 because i told you we'll be using in 7227 04:40:10,240 --> 04:40:15,440 order in order to delete something or 7228 04:40:12,560 --> 04:40:17,680 insert an element right so 7229 04:40:15,440 --> 04:40:20,000 inorder function will be used what is 7230 04:40:17,680 --> 04:40:22,000 the rules of in order it should be left 7231 04:40:20,000 --> 04:40:23,760 root and right side so first it has to 7232 04:40:22,000 --> 04:40:26,320 consider the left side then it has to go 7233 04:40:23,760 --> 04:40:29,440 to the root and then it has to go to the 7234 04:40:26,320 --> 04:40:31,760 right side of the tree and then again in 7235 04:40:29,440 --> 04:40:33,200 order to insert a node inside the tree 7236 04:40:31,760 --> 04:40:34,958 what are the different things you have 7237 04:40:33,200 --> 04:40:36,958 to consider you have to consider whether 7238 04:40:34,958 --> 04:40:39,840 it is null or is it equal to the root 7239 04:40:36,958 --> 04:40:41,440 node and you should also consider if the 7240 04:40:39,840 --> 04:40:43,600 node which you have taken or the value 7241 04:40:41,440 --> 04:40:45,920 which you have taken is greater than or 7242 04:40:43,600 --> 04:40:47,760 lesser than as well if it is greater 7243 04:40:45,920 --> 04:40:50,160 than it is going to the left side of the 7244 04:40:47,760 --> 04:40:51,680 tree if it is lesser than obviously it 7245 04:40:50,160 --> 04:40:53,680 is going to right side which is not 7246 04:40:51,680 --> 04:40:56,000 mentioned here only one particular 7247 04:40:53,680 --> 04:40:57,840 condition is taken that is which is 7248 04:40:56,000 --> 04:40:59,680 lesser than if it is lesser than and it 7249 04:40:57,840 --> 04:41:02,080 is going for left side if it is greater 7250 04:40:59,680 --> 04:41:04,958 than it is going for right side 7251 04:41:02,080 --> 04:41:08,080 so then you have again in order 7252 04:41:04,958 --> 04:41:10,080 successor define that means what happens 7253 04:41:08,080 --> 04:41:11,840 if you want to 7254 04:41:10,080 --> 04:41:13,600 replace something right you have to know 7255 04:41:11,840 --> 04:41:16,320 the successor of it you have to know the 7256 04:41:13,600 --> 04:41:18,400 child node of that particular node so in 7257 04:41:16,320 --> 04:41:21,280 order to find that we'll be using this 7258 04:41:18,400 --> 04:41:24,240 particular function and next in order to 7259 04:41:21,280 --> 04:41:27,440 know the left most node we'll be using 7260 04:41:24,240 --> 04:41:30,000 this particular function that is 7261 04:41:27,440 --> 04:41:32,400 current whatever the position is for the 7262 04:41:30,000 --> 04:41:34,638 node how it is traveling towards the 7263 04:41:32,400 --> 04:41:36,958 left side so current position from there 7264 04:41:34,638 --> 04:41:40,320 will be considering the left side of the 7265 04:41:36,958 --> 04:41:42,638 node if not will not if it is not having 7266 04:41:40,320 --> 04:41:44,320 any no towards the left side of that 7267 04:41:42,638 --> 04:41:47,120 particular 7268 04:41:44,320 --> 04:41:50,000 node right so will not consider that as 7269 04:41:47,120 --> 04:41:52,560 well then deleting a node is done so 7270 04:41:50,000 --> 04:41:54,718 what do you do deletion i just told you 7271 04:41:52,560 --> 04:41:56,958 you had three different types of cases 7272 04:41:54,718 --> 04:41:59,440 of deletion so here we are considering 7273 04:41:56,958 --> 04:42:02,560 uh with the help of inorder how do you 7274 04:41:59,440 --> 04:42:04,638 delete and node right so it will be 7275 04:42:02,560 --> 04:42:05,760 compared first it will be seen whether 7276 04:42:04,638 --> 04:42:08,000 it is 7277 04:42:05,760 --> 04:42:10,320 null or not null if it is not then it 7278 04:42:08,000 --> 04:42:12,798 will go inside the next condition 7279 04:42:10,320 --> 04:42:16,400 whether it is checking if the key is 7280 04:42:12,798 --> 04:42:17,120 that is the value root is lesser than 7281 04:42:16,400 --> 04:42:19,200 the 7282 04:42:17,120 --> 04:42:21,520 node which you have given so if it is 7283 04:42:19,200 --> 04:42:23,200 lesser than then deletion 2 takes place 7284 04:42:21,520 --> 04:42:25,680 to the left side if it is greater than 7285 04:42:23,200 --> 04:42:28,878 it will go towards the right side so 7286 04:42:25,680 --> 04:42:31,280 likewise it will follow the rules until 7287 04:42:28,878 --> 04:42:33,760 unless it will delete an element by 7288 04:42:31,280 --> 04:42:36,480 using in order so these are the 7289 04:42:33,760 --> 04:42:39,120 different inputs we have for it so this 7290 04:42:36,480 --> 04:42:42,080 is the pre generally so it starts from 9 7291 04:42:39,120 --> 04:42:44,080 4 2 and so on and ends with 5. so 7292 04:42:42,080 --> 04:42:47,200 considering the 7293 04:42:44,080 --> 04:42:49,520 image of what i taught you right how do 7294 04:42:47,200 --> 04:42:51,040 you write it considering the root node 7295 04:42:49,520 --> 04:42:52,560 anything towards 7296 04:42:51,040 --> 04:42:54,560 lesser than the root node towards the 7297 04:42:52,560 --> 04:42:57,120 left side anything towards greater than 7298 04:42:54,560 --> 04:42:59,840 the root note towards the right so this 7299 04:42:57,120 --> 04:43:02,560 considering this uh factor we will keep 7300 04:42:59,840 --> 04:43:04,560 give the input to the program so quickly 7301 04:43:02,560 --> 04:43:07,360 if we run this program let's check out 7302 04:43:04,560 --> 04:43:09,840 how does it display so in order to 7303 04:43:07,360 --> 04:43:14,080 traversal how it happens it will go from 7304 04:43:09,840 --> 04:43:15,680 2 to 4 again 5 7 8 and so on up to 15. 7305 04:43:14,080 --> 04:43:18,480 if you want to delete something what do 7306 04:43:15,680 --> 04:43:20,958 you want to delete after delete 11 so 7307 04:43:18,480 --> 04:43:23,440 where is 11 11 is 7308 04:43:20,958 --> 04:43:25,600 like 11 is having a successor of 15 so 7309 04:43:23,440 --> 04:43:28,560 what does it do 11 eliminates in the 7310 04:43:25,600 --> 04:43:30,798 place of 11 15 will be there so will 7311 04:43:28,560 --> 04:43:33,600 replace with the help of in order method 7312 04:43:30,798 --> 04:43:34,400 or what is the flow of uh in order that 7313 04:43:33,600 --> 04:43:35,280 is 7314 04:43:34,400 --> 04:43:37,600 left 7315 04:43:35,280 --> 04:43:39,840 root and right so considering this 7316 04:43:37,600 --> 04:43:41,440 factor it will give you the deletion 7317 04:43:39,840 --> 04:43:43,680 process and replacement for that 7318 04:43:41,440 --> 04:43:46,400 particular deleted node as well so 7319 04:43:43,680 --> 04:43:48,240 that's about binary search tree 7320 04:43:46,400 --> 04:43:50,718 now let's talk about advantage and 7321 04:43:48,240 --> 04:43:53,360 disadvantage of binary tree if you talk 7322 04:43:50,718 --> 04:43:55,360 about advantages we can represent data 7323 04:43:53,360 --> 04:43:57,600 with some relationship right so this is 7324 04:43:55,360 --> 04:43:59,360 the root node or this is the parent node 7325 04:43:57,600 --> 04:44:01,520 and this is the child node so that 7326 04:43:59,360 --> 04:44:03,760 relationship parent chat relationship is 7327 04:44:01,520 --> 04:44:08,400 always there and while you are inserting 7328 04:44:03,760 --> 04:44:11,120 or searching an element or a node in a 7329 04:44:08,400 --> 04:44:13,440 binary it is much more efficient as 7330 04:44:11,120 --> 04:44:15,280 compared to other data structures which 7331 04:44:13,440 --> 04:44:17,040 we have seen 7332 04:44:15,280 --> 04:44:20,080 now if you talk about disadvantages if 7333 04:44:17,040 --> 04:44:23,200 you want to sort this binary tree it is 7334 04:44:20,080 --> 04:44:25,120 much more difficult right because we can 7335 04:44:23,200 --> 04:44:26,320 obviously we can either go on the left 7336 04:44:25,120 --> 04:44:28,320 side 7337 04:44:26,320 --> 04:44:30,400 this can be a tree that means this is 7338 04:44:28,320 --> 04:44:32,240 the left skew tree or this can be the 7339 04:44:30,400 --> 04:44:34,240 scenario where we have all the elements 7340 04:44:32,240 --> 04:44:36,240 on the right side of the node so this 7341 04:44:34,240 --> 04:44:38,718 can be another scenario where and we 7342 04:44:36,240 --> 04:44:41,040 have this right skewed binary tree so 7343 04:44:38,718 --> 04:44:43,680 sorting is difficult and it is not much 7344 04:44:41,040 --> 04:44:46,240 more much flexible right if you talk 7345 04:44:43,680 --> 04:44:48,560 about this binary tree we always have 7346 04:44:46,240 --> 04:44:51,760 either the two children or one children 7347 04:44:48,560 --> 04:44:53,920 or no children there is no flexibility 7348 04:44:51,760 --> 04:44:56,560 in terms of how you want to store it can 7349 04:44:53,920 --> 04:44:58,480 we store another node on the 7350 04:44:56,560 --> 04:45:00,718 with this root node no that is not the 7351 04:44:58,480 --> 04:45:03,200 case right and we don't follow any 7352 04:45:00,718 --> 04:45:05,760 restrictions like okay we always find 7353 04:45:03,200 --> 04:45:08,480 this node that is if this is root node 7354 04:45:05,760 --> 04:45:10,400 we always find uh an element which is 7355 04:45:08,480 --> 04:45:12,718 less than this node on the left side or 7356 04:45:10,400 --> 04:45:15,200 on the right side that is of the greater 7357 04:45:12,718 --> 04:45:16,480 element so that is that flexibility is 7358 04:45:15,200 --> 04:45:19,440 not there 7359 04:45:16,480 --> 04:45:22,638 so the next concept is graphs so graphs 7360 04:45:19,440 --> 04:45:25,600 are very familiar for us since uh school 7361 04:45:22,638 --> 04:45:27,200 days and we used to do uh any bar graph 7362 04:45:25,600 --> 04:45:29,120 or histograms a pie chart or something 7363 04:45:27,200 --> 04:45:30,878 like that but here in data structure 7364 04:45:29,120 --> 04:45:33,680 also we have graphs which is 7365 04:45:30,878 --> 04:45:36,400 mathematically oriented right it is one 7366 04:45:33,680 --> 04:45:38,798 of the kind of data structure so which 7367 04:45:36,400 --> 04:45:41,600 is always derived from the mathematics 7368 04:45:38,798 --> 04:45:45,280 concepts so graph is always a collection 7369 04:45:41,600 --> 04:45:47,360 of vertizes and edges so vertizes will 7370 04:45:45,280 --> 04:45:49,680 be represented with the help of v 7371 04:45:47,360 --> 04:45:52,320 capital v or small v and edges will be 7372 04:45:49,680 --> 04:45:55,440 represented with the help of capital e 7373 04:45:52,320 --> 04:45:57,360 or small e and it will always define the 7374 04:45:55,440 --> 04:45:59,600 relationship we will be having certain 7375 04:45:57,360 --> 04:46:02,080 connections between the edges or the 7376 04:45:59,600 --> 04:46:02,878 vertices or the node to node connections 7377 04:46:02,080 --> 04:46:04,958 so 7378 04:46:02,878 --> 04:46:07,040 there are different types of graphs for 7379 04:46:04,958 --> 04:46:09,760 different types of projects or computer 7380 04:46:07,040 --> 04:46:12,000 science concepts so here i'm just giving 7381 04:46:09,760 --> 04:46:14,638 you a simple example of a graph in order 7382 04:46:12,000 --> 04:46:18,240 to teach you what is vertices and what 7383 04:46:14,638 --> 04:46:20,798 is edges right so i'll write down a 7384 04:46:18,240 --> 04:46:23,680 simple graph 7385 04:46:20,798 --> 04:46:23,680 which is having 7386 04:46:24,160 --> 04:46:27,280 certain connections 7387 04:46:33,520 --> 04:46:41,120 right so here what you can consider 7388 04:46:38,080 --> 04:46:41,120 vertizes are 7389 04:46:42,080 --> 04:46:45,360 a 7390 04:46:43,840 --> 04:46:46,160 b 7391 04:46:45,360 --> 04:46:47,200 d 7392 04:46:46,160 --> 04:46:49,360 and c 7393 04:46:47,200 --> 04:46:51,840 so whatever the nodes you have those are 7394 04:46:49,360 --> 04:46:53,600 the vertices how do you count the edges 7395 04:46:51,840 --> 04:46:56,240 so here i have just connected it 7396 04:46:53,600 --> 04:46:58,718 together so it is forming a loop kind of 7397 04:46:56,240 --> 04:47:02,480 element so if you consider some other 7398 04:46:58,718 --> 04:47:05,280 graph also you can have a clarity on 7399 04:47:02,480 --> 04:47:08,400 edges right i am cutting down this so if 7400 04:47:05,280 --> 04:47:11,200 you want to know what is an edge so edge 7401 04:47:08,400 --> 04:47:13,520 forms between a and b 7402 04:47:11,200 --> 04:47:15,280 again you have an edge from 7403 04:47:13,520 --> 04:47:17,040 a and d 7404 04:47:15,280 --> 04:47:20,000 right if you want to implement that in 7405 04:47:17,040 --> 04:47:21,680 this graph as well you can take it as a 7406 04:47:20,000 --> 04:47:25,200 to b as one edge 7407 04:47:21,680 --> 04:47:27,040 and b to c as one more edge and c to d 7408 04:47:25,200 --> 04:47:28,320 as another h 7409 04:47:27,040 --> 04:47:30,160 right so 7410 04:47:28,320 --> 04:47:31,600 edges are calculated between the 7411 04:47:30,160 --> 04:47:34,000 connection between the two different 7412 04:47:31,600 --> 04:47:36,160 vertices this is the basic introduction 7413 04:47:34,000 --> 04:47:39,040 towards the graph data structure and 7414 04:47:36,160 --> 04:47:40,560 we'll be seeing implementation in python 7415 04:47:39,040 --> 04:47:43,520 as well 7416 04:47:40,560 --> 04:47:45,440 so there is a graph class and you have 7417 04:47:43,520 --> 04:47:48,320 to have a constructor in order to know 7418 04:47:45,440 --> 04:47:50,560 the edges in order to store the vertices 7419 04:47:48,320 --> 04:47:53,440 and you have to store the graph weight 7420 04:47:50,560 --> 04:47:56,080 as well so we have to allocate certain 7421 04:47:53,440 --> 04:47:58,718 memory in the format of list in python 7422 04:47:56,080 --> 04:48:00,718 for that and then we are adding edges to 7423 04:47:58,718 --> 04:48:02,400 the dietetic graph so i told you there 7424 04:48:00,718 --> 04:48:04,878 are different types of graphs it is 7425 04:48:02,400 --> 04:48:07,040 directed weighted graphs and it is graph 7426 04:48:04,878 --> 04:48:09,440 in loop so everything will be considered 7427 04:48:07,040 --> 04:48:10,240 but here i am considering for directed 7428 04:48:09,440 --> 04:48:13,920 graph 7429 04:48:10,240 --> 04:48:16,638 and then you have the edges where you 7430 04:48:13,920 --> 04:48:19,520 have sources and destination that is if 7431 04:48:16,638 --> 04:48:21,840 it is a and b a is a source and b is a 7432 04:48:19,520 --> 04:48:23,840 destination so that is how edges are 7433 04:48:21,840 --> 04:48:26,718 calculated it is a combination of two 7434 04:48:23,840 --> 04:48:29,520 different vertices so then you have a 7435 04:48:26,718 --> 04:48:31,840 function in order to print the 7436 04:48:29,520 --> 04:48:34,400 list whatever the list is then graph we 7437 04:48:31,840 --> 04:48:36,400 cannot draw as it is as we draw to 7438 04:48:34,400 --> 04:48:38,240 understand the graph but we have to put 7439 04:48:36,400 --> 04:48:40,798 that in a sequential order or in the 7440 04:48:38,240 --> 04:48:42,638 form of list so that particular 7441 04:48:40,798 --> 04:48:46,000 representation is taken care with the 7442 04:48:42,638 --> 04:48:47,120 help of print graph function and then we 7443 04:48:46,000 --> 04:48:50,000 have 7444 04:48:47,120 --> 04:48:53,520 the input so input is already given here 7445 04:48:50,000 --> 04:48:56,638 so the edges is being directed so zero 7446 04:48:53,520 --> 04:48:58,560 to one one to two and two to zero two to 7447 04:48:56,638 --> 04:49:00,480 one three to two and four to five and 7448 04:48:58,560 --> 04:49:02,400 five to four so the edges are being 7449 04:49:00,480 --> 04:49:04,718 connected it has been directed from 7450 04:49:02,400 --> 04:49:05,520 which vertex to another vertex it has 7451 04:49:04,718 --> 04:49:08,000 been 7452 04:49:05,520 --> 04:49:09,680 told here with the help of edges and 7453 04:49:08,000 --> 04:49:11,840 then what are the different vertices we 7454 04:49:09,680 --> 04:49:13,680 have we have the labeling from zero to 7455 04:49:11,840 --> 04:49:16,638 five that means we have six different 7456 04:49:13,680 --> 04:49:18,320 vertices and then we have to construct 7457 04:49:16,638 --> 04:49:20,480 the graph how do you construct a graph 7458 04:49:18,320 --> 04:49:22,560 with the help of this particular 7459 04:49:20,480 --> 04:49:25,680 statement that is graph is combination 7460 04:49:22,560 --> 04:49:28,400 of edges and the number of vertices we 7461 04:49:25,680 --> 04:49:31,600 have and we are finally printing the 7462 04:49:28,400 --> 04:49:34,400 graph and it goes like this so first it 7463 04:49:31,600 --> 04:49:36,400 will go from 0 to 1 and then 1 to 2 2 to 7464 04:49:34,400 --> 04:49:39,120 again 0 again 3 to 2 7465 04:49:36,400 --> 04:49:42,320 4 to 5 5 to 4 you have one more 7466 04:49:39,120 --> 04:49:43,760 extension here that is 2 to 1 so this is 7467 04:49:42,320 --> 04:49:46,240 how the 7468 04:49:43,760 --> 04:49:48,480 graph looks like in python 7469 04:49:46,240 --> 04:49:50,240 after knowing what does breadth first 7470 04:49:48,480 --> 04:49:54,080 search right we'll implement that 7471 04:49:50,240 --> 04:49:57,200 particular bfs algorithm in python as 7472 04:49:54,080 --> 04:49:59,200 well so before that let's quickly see 7473 04:49:57,200 --> 04:50:02,320 what is this algorithm all about in 7474 04:49:59,200 --> 04:50:05,360 general breadth first search will always 7475 04:50:02,320 --> 04:50:08,400 traverse the nodes which is from level 1 7476 04:50:05,360 --> 04:50:11,600 to level 2 to level 3 it will not leave 7477 04:50:08,400 --> 04:50:13,280 any single level so for example if you 7478 04:50:11,600 --> 04:50:16,480 have a node 7479 04:50:13,280 --> 04:50:19,760 right a root node followed by sub nodes 7480 04:50:16,480 --> 04:50:22,240 so you will traverse level wise not node 7481 04:50:19,760 --> 04:50:26,400 wise so for example 7482 04:50:22,240 --> 04:50:27,680 say you have 10 here and you have 7483 04:50:26,400 --> 04:50:29,520 8 7484 04:50:27,680 --> 04:50:31,840 9 this is 7485 04:50:29,520 --> 04:50:35,520 level 1 sometimes it is also called as 7486 04:50:31,840 --> 04:50:37,360 level 0 as well right again you have 7487 04:50:35,520 --> 04:50:38,798 one more level 7488 04:50:37,360 --> 04:50:40,160 11 7489 04:50:38,798 --> 04:50:45,200 this is 12 7490 04:50:40,160 --> 04:50:48,718 again here this is 13 and this is 14 7491 04:50:45,200 --> 04:50:50,638 right you have this graph and it goes 7492 04:50:48,718 --> 04:50:52,480 level wise this is level two or it can 7493 04:50:50,638 --> 04:50:54,400 also be level one if you start with zero 7494 04:50:52,480 --> 04:50:57,440 it comes with one if if you start with 7495 04:50:54,400 --> 04:50:59,600 one it will go to two so 7496 04:50:57,440 --> 04:51:02,878 first it will start with ten it will 7497 04:50:59,600 --> 04:51:06,240 finish 8 it will finish 9 again it will 7498 04:51:02,878 --> 04:51:10,240 come back to 11 and then 12 again here 7499 04:51:06,240 --> 04:51:12,718 it will come back to 3 and then 4 so 7500 04:51:10,240 --> 04:51:14,560 level wise it will try to 7501 04:51:12,718 --> 04:51:18,160 finish the 7502 04:51:14,560 --> 04:51:20,000 traversing in bfs algorithm so 7503 04:51:18,160 --> 04:51:21,920 as it is written here that is 7504 04:51:20,000 --> 04:51:24,400 neighboring nodes in a single level are 7505 04:51:21,920 --> 04:51:27,360 traversed first and then the 7506 04:51:24,400 --> 04:51:29,520 next node or next level will be 7507 04:51:27,360 --> 04:51:31,520 put up into consideration else it will 7508 04:51:29,520 --> 04:51:33,280 not go to the next level until unless it 7509 04:51:31,520 --> 04:51:36,080 is travels for 7510 04:51:33,280 --> 04:51:38,958 every node in that particular level so 7511 04:51:36,080 --> 04:51:42,718 let's quickly see how does this bfs 7512 04:51:38,958 --> 04:51:45,760 algorithm will work in python 7513 04:51:42,718 --> 04:51:49,120 so here you have 7514 04:51:45,760 --> 04:51:51,520 bfs graph and root so root is the main 7515 04:51:49,120 --> 04:51:53,520 thing where it will start traversal at 7516 04:51:51,520 --> 04:51:57,200 least once it will visit each and every 7517 04:51:53,520 --> 04:51:59,440 node according to the level wise is the 7518 04:51:57,200 --> 04:52:01,360 thing you have to consider so visited 7519 04:51:59,440 --> 04:52:03,600 how many times it is visited and where 7520 04:52:01,360 --> 04:52:06,718 it is visited in the queue actually you 7521 04:52:03,600 --> 04:52:09,520 can't define a graph as we write it as 7522 04:52:06,718 --> 04:52:11,760 an example so we put up with the help of 7523 04:52:09,520 --> 04:52:13,840 numbers you could see here zero is the 7524 04:52:11,760 --> 04:52:16,638 level one 7525 04:52:13,840 --> 04:52:19,600 again you have next level two and three 7526 04:52:16,638 --> 04:52:21,440 right so it will traverse all the 7527 04:52:19,600 --> 04:52:23,600 nodes that is one two three there are 7528 04:52:21,440 --> 04:52:25,600 only three nodes here that is three 7529 04:52:23,600 --> 04:52:28,798 vertices it will travel to all the 7530 04:52:25,600 --> 04:52:31,200 vertex and it will finish so let me 7531 04:52:28,798 --> 04:52:33,680 quickly run this program and see how 7532 04:52:31,200 --> 04:52:35,600 does it yeah that is how it is starts 7533 04:52:33,680 --> 04:52:38,560 from zero it will go to one then it will 7534 04:52:35,600 --> 04:52:41,760 go to two and then it will reach out 3 7535 04:52:38,560 --> 04:52:44,320 so it will traverse accordingly one 7536 04:52:41,760 --> 04:52:46,080 after the other and visits each node one 7537 04:52:44,320 --> 04:52:48,480 at a time so 7538 04:52:46,080 --> 04:52:51,280 breadth first search will visit 7539 04:52:48,480 --> 04:52:54,878 according to the level of the graph and 7540 04:52:51,280 --> 04:52:55,840 it will always have the nodes visited 7541 04:52:54,878 --> 04:52:58,480 before 7542 04:52:55,840 --> 04:53:01,440 finishing the traversal right so that's 7543 04:52:58,480 --> 04:53:03,760 about breadth first search in python 7544 04:53:01,440 --> 04:53:06,080 after learning what is depth first such 7545 04:53:03,760 --> 04:53:09,360 we have to implement the same algorithm 7546 04:53:06,080 --> 04:53:11,360 in python as well so let's quickly see 7547 04:53:09,360 --> 04:53:14,718 what is depth first search 7548 04:53:11,360 --> 04:53:17,600 before implementing the same right so 7549 04:53:14,718 --> 04:53:20,400 this algorithm will always traverse 7550 04:53:17,600 --> 04:53:23,440 between nodes towards the depth of the 7551 04:53:20,400 --> 04:53:26,798 tree that means first node if it is 7552 04:53:23,440 --> 04:53:29,920 having two three and four levels first 7553 04:53:26,798 --> 04:53:32,400 it will complete the levels according to 7554 04:53:29,920 --> 04:53:34,560 the depth wise it will not go towards 7555 04:53:32,400 --> 04:53:37,280 horizontal manner it will just work in 7556 04:53:34,560 --> 04:53:38,560 the vertical manner for example i'll 7557 04:53:37,280 --> 04:53:40,480 just 7558 04:53:38,560 --> 04:53:41,520 write a 7559 04:53:40,480 --> 04:53:42,878 tree 7560 04:53:41,520 --> 04:53:45,440 or a graph 7561 04:53:42,878 --> 04:53:47,360 to show you how does this work it is a 7562 04:53:45,440 --> 04:53:49,200 very general example 7563 04:53:47,360 --> 04:53:53,200 so here it has 7564 04:53:49,200 --> 04:53:53,200 the value 10 and again 7565 04:53:53,680 --> 04:53:59,120 it has two different 7566 04:53:55,840 --> 04:54:00,400 subtrees some nodes that is eight 7567 04:53:59,120 --> 04:54:02,958 and seven 7568 04:54:00,400 --> 04:54:04,400 so again what it will happen anything it 7569 04:54:02,958 --> 04:54:07,520 is there 7570 04:54:04,400 --> 04:54:11,280 towards the depth for example here this 7571 04:54:07,520 --> 04:54:14,320 manner first we will go to 10 then 7 7572 04:54:11,280 --> 04:54:18,160 then 6 it will backtrack and come back 7573 04:54:14,320 --> 04:54:21,120 to 8. so it will first always consider 7574 04:54:18,160 --> 04:54:23,760 the nodes which is towards the vertical 7575 04:54:21,120 --> 04:54:26,240 side which is towards the depth of the 7576 04:54:23,760 --> 04:54:29,360 particular tree or graph 7577 04:54:26,240 --> 04:54:32,400 so hope that was clear and let's quickly 7578 04:54:29,360 --> 04:54:35,520 hop into ide to see how does dfs 7579 04:54:32,400 --> 04:54:37,520 algorithm will work in python so here is 7580 04:54:35,520 --> 04:54:40,798 the program for dfs 7581 04:54:37,520 --> 04:54:43,360 in python so as i told you it will visit 7582 04:54:40,798 --> 04:54:45,840 each and every node towards the depth of 7583 04:54:43,360 --> 04:54:48,240 the particular graph or tree so it will 7584 04:54:45,840 --> 04:54:50,240 not go towards neighboring nodes or it 7585 04:54:48,240 --> 04:54:52,400 will not go parallely it will not 7586 04:54:50,240 --> 04:54:55,040 traverse it is only the vertical travels 7587 04:54:52,400 --> 04:54:56,958 that will happen so again you have a 7588 04:54:55,040 --> 04:54:59,200 starting position you have a graph which 7589 04:54:56,958 --> 04:55:01,120 is declared you also have to have a 7590 04:54:59,200 --> 04:55:03,520 count of visiting nodes so you should 7591 04:55:01,120 --> 04:55:06,080 not repeatedly visit that particular 7592 04:55:03,520 --> 04:55:08,798 vertex or the node again and again right 7593 04:55:06,080 --> 04:55:12,080 so visited is none as of now because 7594 04:55:08,798 --> 04:55:12,878 we'll be starting to visit the graph and 7595 04:55:12,080 --> 04:55:16,000 then 7596 04:55:12,878 --> 04:55:18,798 visiting nodes has been set to 7597 04:55:16,000 --> 04:55:21,680 0 initially then we'll start and then it 7598 04:55:18,798 --> 04:55:24,638 will proceed one vertex to another 7599 04:55:21,680 --> 04:55:27,760 vertex in horizontal manner say for 7600 04:55:24,638 --> 04:55:30,320 example giving graphs in coding is 7601 04:55:27,760 --> 04:55:32,958 different than we write it is very easy 7602 04:55:30,320 --> 04:55:35,680 for us to just write it down accordingly 7603 04:55:32,958 --> 04:55:37,280 with levels mentioning and also how was 7604 04:55:35,680 --> 04:55:40,240 the flow of the tree or the graph but 7605 04:55:37,280 --> 04:55:41,360 here you have to mention the notes right 7606 04:55:40,240 --> 04:55:43,520 in zero 7607 04:55:41,360 --> 04:55:45,600 where it is having that particular 7608 04:55:43,520 --> 04:55:47,760 connection from zero you have connection 7609 04:55:45,600 --> 04:55:50,240 to one and two again from one you have 7610 04:55:47,760 --> 04:55:52,400 connection to zero three and four then 7611 04:55:50,240 --> 04:55:54,080 two has connection to zero three has 7612 04:55:52,400 --> 04:55:56,000 connection to one and four has 7613 04:55:54,080 --> 04:55:59,360 connection to two and three that means 7614 04:55:56,000 --> 04:56:01,440 it is telling you the flow of the graph 7615 04:55:59,360 --> 04:56:04,320 how it is connected rather than 7616 04:56:01,440 --> 04:56:06,000 rewriting it's very pictorial and 7617 04:56:04,320 --> 04:56:08,160 representation which is understandable 7618 04:56:06,000 --> 04:56:10,638 by everyone but here you have to give it 7619 04:56:08,160 --> 04:56:13,200 node wise all these numbers are the 7620 04:56:10,638 --> 04:56:15,360 nodes or the vertices so let me quickly 7621 04:56:13,200 --> 04:56:16,480 run this 7622 04:56:15,360 --> 04:56:18,560 and check 7623 04:56:16,480 --> 04:56:21,920 yes it is going 7624 04:56:18,560 --> 04:56:24,320 towards the depth of the graph that is 0 7625 04:56:21,920 --> 04:56:28,798 first and then it is it is going to 2 7626 04:56:24,320 --> 04:56:31,840 then 1 3 and 4. so the graph is 0 1 2 3 7627 04:56:28,798 --> 04:56:34,000 4. so it is having the vertex which is 7628 04:56:31,840 --> 04:56:37,440 starting from 0 ending from 4 but the 7629 04:56:34,000 --> 04:56:39,840 traversal will happen from 0 to 2 then 1 7630 04:56:37,440 --> 04:56:42,878 then 3 and then it will go to 4. so this 7631 04:56:39,840 --> 04:56:46,638 is about depth first search in python 7632 04:56:42,878 --> 04:56:48,718 hash tables is a next concept so what is 7633 04:56:46,638 --> 04:56:51,520 hash table before learning that let's 7634 04:56:48,718 --> 04:56:54,080 see what is hashing right so hashing 7635 04:56:51,520 --> 04:56:57,200 plays a very important role generally in 7636 04:56:54,080 --> 04:56:58,320 order to compress data in order to have 7637 04:56:57,200 --> 04:57:01,440 a 7638 04:56:58,320 --> 04:57:04,000 encrypted it's not exact encryption it 7639 04:57:01,440 --> 04:57:07,840 is just changing the values into its 7640 04:57:04,000 --> 04:57:10,240 relevant keys okay so cryptology we see 7641 04:57:07,840 --> 04:57:12,400 so that is also there and database 7642 04:57:10,240 --> 04:57:15,520 indexing can also be made with the help 7643 04:57:12,400 --> 04:57:17,360 of hash tables so let's quickly see what 7644 04:57:15,520 --> 04:57:19,120 is hashing function and what is hash 7645 04:57:17,360 --> 04:57:21,840 table and so on right 7646 04:57:19,120 --> 04:57:23,840 coming up to the first point here so 7647 04:57:21,840 --> 04:57:26,320 what they define as hashing is a black 7648 04:57:23,840 --> 04:57:28,798 box that takes a key as input and 7649 04:57:26,320 --> 04:57:31,520 provides hash value as output i'll give 7650 04:57:28,798 --> 04:57:34,638 you a simple real-time example you're an 7651 04:57:31,520 --> 04:57:36,878 employee in an organization right so 7652 04:57:34,638 --> 04:57:39,200 when it is an organization there are 7653 04:57:36,878 --> 04:57:41,360 more than 50 people as well there are 7654 04:57:39,200 --> 04:57:44,160 more than 500 people as well there are 7655 04:57:41,360 --> 04:57:45,920 more than thousands people as well 7656 04:57:44,160 --> 04:57:48,480 what is the guarantee that your name is 7657 04:57:45,920 --> 04:57:50,480 not repeated with initial 7658 04:57:48,480 --> 04:57:53,440 they might be other person who is having 7659 04:57:50,480 --> 04:57:56,878 name exactly like you for example there 7660 04:57:53,440 --> 04:57:59,280 is a person called sham r okay there are 7661 04:57:56,878 --> 04:58:01,280 two different people or three different 7662 04:57:59,280 --> 04:58:02,320 people in the same name same initial 7663 04:58:01,280 --> 04:58:05,680 everything 7664 04:58:02,320 --> 04:58:06,798 so how do you define them the only key 7665 04:58:05,680 --> 04:58:09,120 that will 7666 04:58:06,798 --> 04:58:12,000 give them the unique identity is the 7667 04:58:09,120 --> 04:58:14,320 employee id right 7668 04:58:12,000 --> 04:58:16,638 you agree with me employee id is 7669 04:58:14,320 --> 04:58:19,440 different for different individual on 7670 04:58:16,638 --> 04:58:21,520 the basis of that employee key itself 7671 04:58:19,440 --> 04:58:23,440 people will recognize 7672 04:58:21,520 --> 04:58:26,080 in case if they want to do any 7673 04:58:23,440 --> 04:58:29,360 communication or it is necessary for any 7674 04:58:26,080 --> 04:58:33,120 documentation so if they use employee id 7675 04:58:29,360 --> 04:58:34,638 so employee id acts as a real-time 7676 04:58:33,120 --> 04:58:37,360 example for 7677 04:58:34,638 --> 04:58:40,160 keying and this is a hashing technique 7678 04:58:37,360 --> 04:58:44,798 for example the first person sham 7679 04:58:40,160 --> 04:58:46,878 r is assigned with the employee id 2. so 7680 04:58:44,798 --> 04:58:50,240 anywhere anytime if they ask for 7681 04:58:46,878 --> 04:58:52,240 employee 2 that is always sham r right 7682 04:58:50,240 --> 04:58:55,440 people might be confused listening to 7683 04:58:52,240 --> 04:58:57,440 the name but this will be clear employee 7684 04:58:55,440 --> 04:58:58,958 id will be clear 7685 04:58:57,440 --> 04:59:00,638 so 7686 04:58:58,958 --> 04:59:03,200 it will be 7687 04:59:00,638 --> 04:59:05,680 one way function generally so there is 7688 04:59:03,200 --> 04:59:08,320 no reverse right we are creating an 7689 04:59:05,680 --> 04:59:10,000 employee id for one single employee then 7690 04:59:08,320 --> 04:59:13,520 employee is not doing anything back to 7691 04:59:10,000 --> 04:59:15,840 us so it is a one-way function generally 7692 04:59:13,520 --> 04:59:19,680 a value is converted to its relevant 7693 04:59:15,840 --> 04:59:21,920 number or integers or a key that's it so 7694 04:59:19,680 --> 04:59:24,560 then a good hashing function has 7695 04:59:21,920 --> 04:59:27,680 minimalistic collision so the word 7696 04:59:24,560 --> 04:59:29,200 collision what is collision having 7697 04:59:27,680 --> 04:59:32,240 two different 7698 04:59:29,200 --> 04:59:34,798 numbers with same hash key is called 7699 04:59:32,240 --> 04:59:36,840 collision right so i'll let you know 7700 04:59:34,798 --> 04:59:38,560 what is collision further 7701 04:59:36,840 --> 04:59:40,798 so 7702 04:59:38,560 --> 04:59:43,440 a hash table is a data structure that 7703 04:59:40,798 --> 04:59:45,840 uses pair of keys and values i've 7704 04:59:43,440 --> 04:59:48,718 already told you every 7705 04:59:45,840 --> 04:59:51,520 value for example sham is a value an 7706 04:59:48,718 --> 04:59:52,798 employee id is a key right it is stored 7707 04:59:51,520 --> 04:59:55,200 in a 7708 04:59:52,798 --> 04:59:58,560 hash table just like database table we 7709 04:59:55,200 --> 05:00:01,600 also have hash table it has the key and 7710 04:59:58,560 --> 05:00:04,320 value pair that's it say for example it 7711 05:00:01,600 --> 05:00:06,400 can be represented this way so the key 7712 05:00:04,320 --> 05:00:09,200 is employee id 7713 05:00:06,400 --> 05:00:12,480 and the value is the name of the 7714 05:00:09,200 --> 05:00:14,638 employee so this forms a hash table so 7715 05:00:12,480 --> 05:00:17,200 it will also have a 7716 05:00:14,638 --> 05:00:19,280 hash key which is hash indexing the 7717 05:00:17,200 --> 05:00:23,120 indexing will also be present i'll show 7718 05:00:19,280 --> 05:00:25,440 you with a simple example later so 7719 05:00:23,120 --> 05:00:28,638 it will be always one of it its kind 7720 05:00:25,440 --> 05:00:30,400 that means it is having a unique key i 7721 05:00:28,638 --> 05:00:31,600 told you one of its kind is nothing but 7722 05:00:30,400 --> 05:00:34,480 unique key 7723 05:00:31,600 --> 05:00:37,520 which is created by hash function now 7724 05:00:34,480 --> 05:00:40,080 the question is what is hash function 7725 05:00:37,520 --> 05:00:44,320 if you can see the formula here 7726 05:00:40,080 --> 05:00:47,280 h of k is equal to k1 modulo m so what 7727 05:00:44,320 --> 05:00:48,638 is it h k represents the 7728 05:00:47,280 --> 05:00:51,280 function of 7729 05:00:48,638 --> 05:00:53,200 hash that is hash function 7730 05:00:51,280 --> 05:00:55,440 and there is this mathematical formula 7731 05:00:53,200 --> 05:00:57,920 for us in order to solve to get a hash 7732 05:00:55,440 --> 05:01:02,638 function to get a hash key generated 7733 05:00:57,920 --> 05:01:04,638 that is k1 modulo m so here m is the 7734 05:01:02,638 --> 05:01:05,920 size of the list i'll let you know with 7735 05:01:04,638 --> 05:01:08,958 an example 7736 05:01:05,920 --> 05:01:12,240 k1 is the value we want to store what we 7737 05:01:08,958 --> 05:01:15,040 want to store that is k1 and value we 7738 05:01:12,240 --> 05:01:18,080 want to calculate the key for is this 7739 05:01:15,040 --> 05:01:19,920 particular k inside the hash function so 7740 05:01:18,080 --> 05:01:22,320 this is a brief introduction towards 7741 05:01:19,920 --> 05:01:24,878 hashing and hash tables and also hash 7742 05:01:22,320 --> 05:01:27,760 functions let's quickly jump on to the 7743 05:01:24,878 --> 05:01:30,480 real time example where i can show you 7744 05:01:27,760 --> 05:01:31,600 how to implement this particular 7745 05:01:30,480 --> 05:01:34,638 formula 7746 05:01:31,600 --> 05:01:36,798 now let's implement hashing technique 7747 05:01:34,638 --> 05:01:40,160 with a simple example 7748 05:01:36,798 --> 05:01:41,200 so here i'm taking a 7749 05:01:40,160 --> 05:01:42,160 list 7750 05:01:41,200 --> 05:01:45,120 okay 7751 05:01:42,160 --> 05:01:46,080 it is having it indexes 7752 05:01:45,120 --> 05:01:47,360 0 7753 05:01:46,080 --> 05:01:48,240 1 7754 05:01:47,360 --> 05:01:50,878 2 7755 05:01:48,240 --> 05:01:52,400 and you want to store 7756 05:01:50,878 --> 05:01:55,600 it is a size of 7757 05:01:52,400 --> 05:01:58,798 3 obviously you want to store certain 7758 05:01:55,600 --> 05:02:02,400 values inside that right say for example 7759 05:01:58,798 --> 05:02:03,280 you want to store 1 2 3 okay by using 7760 05:02:02,400 --> 05:02:05,680 the 7761 05:02:03,280 --> 05:02:07,120 formula h of k 7762 05:02:05,680 --> 05:02:10,878 is equal to 7763 05:02:07,120 --> 05:02:14,080 k1 modulo m this is the standard formula 7764 05:02:10,878 --> 05:02:15,840 which we are using let's see how do we 7765 05:02:14,080 --> 05:02:18,958 calculate the 7766 05:02:15,840 --> 05:02:21,200 hash table contents so let's quickly 7767 05:02:18,958 --> 05:02:21,200 have 7768 05:02:21,600 --> 05:02:26,000 the first one for the value 1 you want 7769 05:02:23,680 --> 05:02:28,480 to store 1 comma 2 comma 3 inside this 7770 05:02:26,000 --> 05:02:30,560 particular list which is indexed as 0 1 7771 05:02:28,480 --> 05:02:34,000 2 okay so 7772 05:02:30,560 --> 05:02:37,040 h of 1 the value which you want to store 7773 05:02:34,000 --> 05:02:40,000 then you have to take the value 7774 05:02:37,040 --> 05:02:43,680 and then modulo by what is the total 7775 05:02:40,000 --> 05:02:46,000 number we have the size 3 right if you 7776 05:02:43,680 --> 05:02:47,280 do this modulation then you will get the 7777 05:02:46,000 --> 05:02:50,798 answer 1 7778 05:02:47,280 --> 05:02:53,040 so it says the value 1 to be stored in 7779 05:02:50,798 --> 05:02:57,440 index 1 right 7780 05:02:53,040 --> 05:03:02,080 then repeating for the value 2 right 2 7781 05:02:57,440 --> 05:03:05,360 modulo 3 the answer is 2 it says 7782 05:03:02,080 --> 05:03:09,280 2 should be stored in the index 2. 7783 05:03:05,360 --> 05:03:13,760 coming up to the next one right so 3 7784 05:03:09,280 --> 05:03:17,040 modulo 3 is equal to 0 indexing 0 the 7785 05:03:13,760 --> 05:03:20,878 value 3 should be stored so we have got 7786 05:03:17,040 --> 05:03:24,400 value 3 the index hash table index 3 and 7787 05:03:20,878 --> 05:03:27,760 value one as hashtag index one value two 7788 05:03:24,400 --> 05:03:29,120 has hashtable index two so this is how 7789 05:03:27,760 --> 05:03:31,600 you implement 7790 05:03:29,120 --> 05:03:35,040 hashing function with the help of 7791 05:03:31,600 --> 05:03:37,760 particular mathematical formula right so 7792 05:03:35,040 --> 05:03:40,958 this is the table which is formed 7793 05:03:37,760 --> 05:03:43,120 let's implement this particular example 7794 05:03:40,958 --> 05:03:45,520 in python programming with the same 7795 05:03:43,120 --> 05:03:47,760 concept it's very simple let's quickly 7796 05:03:45,520 --> 05:03:50,798 switch on to the id now 7797 05:03:47,760 --> 05:03:53,600 here you can find this particular 7798 05:03:50,798 --> 05:03:55,600 program whatever i thought as an example 7799 05:03:53,600 --> 05:03:58,560 is put up into a program there is no 7800 05:03:55,600 --> 05:04:00,560 other difference okay so first we have 7801 05:03:58,560 --> 05:04:03,280 creating a function that is called 7802 05:04:00,560 --> 05:04:06,718 hashing underscore key it is having two 7803 05:04:03,280 --> 05:04:08,958 different parameters that is key comma m 7804 05:04:06,718 --> 05:04:12,560 okay the same formula what we used has 7805 05:04:08,958 --> 05:04:15,840 been implemented here no change so next 7806 05:04:12,560 --> 05:04:18,718 written key modulo m right so modulus 7807 05:04:15,840 --> 05:04:20,958 operation is performed on the 7808 05:04:18,718 --> 05:04:23,280 value which you want to store 7809 05:04:20,958 --> 05:04:25,040 then the size of the hash table the size 7810 05:04:23,280 --> 05:04:28,480 of the hash table there in the example 7811 05:04:25,040 --> 05:04:31,280 was 3 here i am taking it as 8 so if you 7812 05:04:28,480 --> 05:04:33,920 apply that formula you will get an 7813 05:04:31,280 --> 05:04:35,040 answer where it is stored for let's 7814 05:04:33,920 --> 05:04:37,680 quickly 7815 05:04:35,040 --> 05:04:40,638 check the output for the same now 7816 05:04:37,680 --> 05:04:45,760 here you can see the hash value 4 is 4 7817 05:04:40,638 --> 05:04:48,480 and 3 is 3 10 is 2 12 is 4 8 is 0. so 7818 05:04:45,760 --> 05:04:51,520 this is how we try to give the 7819 05:04:48,480 --> 05:04:54,160 values and the key pairs in hashing 7820 05:04:51,520 --> 05:04:56,080 technique hope this was clear 7821 05:04:54,160 --> 05:04:57,760 algorithms 7822 05:04:56,080 --> 05:05:00,400 let's try to understand what is an 7823 05:04:57,760 --> 05:05:02,718 algorithm it is a step-by-step approach 7824 05:05:00,400 --> 05:05:04,798 to solve a particular problem 7825 05:05:02,718 --> 05:05:06,798 if there is a problem what is the things 7826 05:05:04,798 --> 05:05:09,360 that you are going to take care of so 7827 05:05:06,798 --> 05:05:12,718 the solution that you give should be 7828 05:05:09,360 --> 05:05:16,000 crisp right it should be exactly what is 7829 05:05:12,718 --> 05:05:18,160 required right if a problem is about 7830 05:05:16,000 --> 05:05:20,480 uh swapping two numbers you should be 7831 05:05:18,160 --> 05:05:23,120 able to swap two numbers and with the 7832 05:05:20,480 --> 05:05:24,798 most efficient approach and 7833 05:05:23,120 --> 05:05:26,878 when you define an algorithm or you when 7834 05:05:24,798 --> 05:05:31,120 you are writing an algorithm it should 7835 05:05:26,878 --> 05:05:34,080 be understood by a non-technical person 7836 05:05:31,120 --> 05:05:36,560 right there should be no such uh 7837 05:05:34,080 --> 05:05:39,360 language dependency 7838 05:05:36,560 --> 05:05:41,200 in your algorithm right so 7839 05:05:39,360 --> 05:05:43,600 this is what an algorithm is as a 7840 05:05:41,200 --> 05:05:45,760 step-by-step approach to solve a 7841 05:05:43,600 --> 05:05:49,120 particular problem your problem can be 7842 05:05:45,760 --> 05:05:51,600 complex or it can be an easy problem it 7843 05:05:49,120 --> 05:05:53,920 is free from programming language there 7844 05:05:51,600 --> 05:05:56,080 are multiple possible algorithms to 7845 05:05:53,920 --> 05:05:59,680 solve a given problem let's suppose i 7846 05:05:56,080 --> 05:06:02,400 told you to swap two numbers right now 7847 05:05:59,680 --> 05:06:05,440 one algorithm is wherein you can go 7848 05:06:02,400 --> 05:06:07,200 ahead and create one temporary variable 7849 05:06:05,440 --> 05:06:09,040 and then swap them 7850 05:06:07,200 --> 05:06:11,360 the another approach is that you're 7851 05:06:09,040 --> 05:06:13,760 going to use addition and subtraction 7852 05:06:11,360 --> 05:06:15,600 operators to solve the same 7853 05:06:13,760 --> 05:06:17,360 you're going to use these operators and 7854 05:06:15,600 --> 05:06:19,840 you can solve this problem of swapping 7855 05:06:17,360 --> 05:06:22,400 two numbers right which one do you think 7856 05:06:19,840 --> 05:06:25,120 is the efficient approach obviously here 7857 05:06:22,400 --> 05:06:28,000 you're using an extra variable and here 7858 05:06:25,120 --> 05:06:30,480 you're not using any extra variable so 7859 05:06:28,000 --> 05:06:32,718 this seems to be a better approach and 7860 05:06:30,480 --> 05:06:35,040 obviously it should be understood by a 7861 05:06:32,718 --> 05:06:36,080 non-technical person you should not be 7862 05:06:35,040 --> 05:06:39,040 using 7863 05:06:36,080 --> 05:06:42,638 language dependency code like you should 7864 05:06:39,040 --> 05:06:45,440 not use any language agnostic code right 7865 05:06:42,638 --> 05:06:48,638 so it should be language independent so 7866 05:06:45,440 --> 05:06:51,120 let's see what is an algorithm analysis 7867 05:06:48,638 --> 05:06:54,320 this is again a step-by-step approach to 7868 05:06:51,120 --> 05:06:58,320 solve a particular problem it helps us 7869 05:06:54,320 --> 05:07:00,878 identify which algorithm is optimal okay 7870 05:06:58,320 --> 05:07:03,520 this is the analysis part of algorithm 7871 05:07:00,878 --> 05:07:06,160 right here in let's suppose i told you 7872 05:07:03,520 --> 05:07:08,240 to swap it helps us to understand the 7873 05:07:06,160 --> 05:07:09,920 trade-off between time and space 7874 05:07:08,240 --> 05:07:11,600 complexity right 7875 05:07:09,920 --> 05:07:13,120 so there should be always you should be 7876 05:07:11,600 --> 05:07:15,040 able to understand the difference 7877 05:07:13,120 --> 05:07:17,200 between time complexity and space 7878 05:07:15,040 --> 05:07:20,798 complexity and depending on that you 7879 05:07:17,200 --> 05:07:22,878 should be able to do a trade-off as well 7880 05:07:20,798 --> 05:07:24,160 now when i mean trade-off you should be 7881 05:07:22,878 --> 05:07:26,480 able to 7882 05:07:24,160 --> 05:07:29,040 depict that are you going to keep your 7883 05:07:26,480 --> 05:07:31,760 time complexity efficient or are you 7884 05:07:29,040 --> 05:07:34,400 going to use your time efficiently or 7885 05:07:31,760 --> 05:07:36,718 are you going to save your space so 7886 05:07:34,400 --> 05:07:38,400 let's suppose you are given an example 7887 05:07:36,718 --> 05:07:40,240 right 7888 05:07:38,400 --> 05:07:42,000 let me give you an example 7889 05:07:40,240 --> 05:07:44,080 so you are giving an example you are 7890 05:07:42,000 --> 05:07:45,200 given an array and in that area you want 7891 05:07:44,080 --> 05:07:47,680 to search 7892 05:07:45,200 --> 05:07:50,718 an element now the first approach is 7893 05:07:47,680 --> 05:07:52,560 that you will go and search one by one 7894 05:07:50,718 --> 05:07:54,400 right you will go here you will iterate 7895 05:07:52,560 --> 05:07:56,320 through here and here and here you try 7896 05:07:54,400 --> 05:07:58,400 to search an element right let's suppose 7897 05:07:56,320 --> 05:08:01,360 you are searching for four 7898 05:07:58,400 --> 05:08:03,760 in the worst case you are traversing 7899 05:08:01,360 --> 05:08:06,718 a big o of n time right and in the best 7900 05:08:03,760 --> 05:08:08,240 case you are traversing because one time 7901 05:08:06,718 --> 05:08:10,480 this is the best case and this is the 7902 05:08:08,240 --> 05:08:12,320 worst case right so these are the two 7903 05:08:10,480 --> 05:08:13,200 cases 7904 05:08:12,320 --> 05:08:16,240 but 7905 05:08:13,200 --> 05:08:19,120 this code in itself whenever you are 7906 05:08:16,240 --> 05:08:22,240 traversing in a linear fashion is 7907 05:08:19,120 --> 05:08:22,240 inefficient right 7908 05:08:22,400 --> 05:08:26,718 and if you try if you're trying to 7909 05:08:24,240 --> 05:08:28,480 search for an element using different 7910 05:08:26,718 --> 05:08:30,798 approach let's suppose you are using a 7911 05:08:28,480 --> 05:08:33,040 binary search and then you are trying to 7912 05:08:30,798 --> 05:08:35,360 search an element obviously your time 7913 05:08:33,040 --> 05:08:37,280 complexity will reduce 7914 05:08:35,360 --> 05:08:40,480 the best case will again remain bigger 7915 05:08:37,280 --> 05:08:42,320 one but it will reduce to big o of 7916 05:08:40,480 --> 05:08:45,440 logging 7917 05:08:42,320 --> 05:08:47,680 now there are certain parameters or 7918 05:08:45,440 --> 05:08:50,958 depending on these parameters you can 7919 05:08:47,680 --> 05:08:53,040 define the best worst and average case 7920 05:08:50,958 --> 05:08:55,600 of a given problem so now there are 7921 05:08:53,040 --> 05:08:58,400 different types of algorithmic analysis 7922 05:08:55,600 --> 05:09:00,958 that those are best case average case 7923 05:08:58,400 --> 05:09:03,520 and worst case okay so the best case is 7924 05:09:00,958 --> 05:09:06,080 obviously when you are searching for an 7925 05:09:03,520 --> 05:09:08,718 element in an array or let's suppose 100 7926 05:09:06,080 --> 05:09:11,040 in size right or thousand size the best 7927 05:09:08,718 --> 05:09:12,958 case is that the first element that you 7928 05:09:11,040 --> 05:09:15,600 look if you if you're talking about what 7929 05:09:12,958 --> 05:09:17,920 linear search okay best 7930 05:09:15,600 --> 05:09:19,520 case scenario is that the element that 7931 05:09:17,920 --> 05:09:21,440 you're trying to search is the first 7932 05:09:19,520 --> 05:09:23,600 element right done this is your best 7933 05:09:21,440 --> 05:09:26,000 case and it requires constant amount of 7934 05:09:23,600 --> 05:09:28,080 time now if you talk about average case 7935 05:09:26,000 --> 05:09:31,200 it will be somewhere around the middle 7936 05:09:28,080 --> 05:09:33,840 right about 1000 divided by 2 or you can 7937 05:09:31,200 --> 05:09:37,360 say big o of n divided by 2 since we are 7938 05:09:33,840 --> 05:09:40,000 neglecting we do not take care of these 7939 05:09:37,360 --> 05:09:42,000 constant terms or we can negotiate or we 7940 05:09:40,000 --> 05:09:44,798 can neglect these 7941 05:09:42,000 --> 05:09:46,958 constant terms because as soon as our 7942 05:09:44,798 --> 05:09:48,798 input is increased right let's suppose 7943 05:09:46,958 --> 05:09:51,200 right now we are talking about 7944 05:09:48,798 --> 05:09:53,040 million uh thousand elements we are 7945 05:09:51,200 --> 05:09:55,200 talking about 10 billion elements at 7946 05:09:53,040 --> 05:09:58,400 that time dividing it by two still is 7947 05:09:55,200 --> 05:10:01,440 same okay not same but it is 7948 05:09:58,400 --> 05:10:03,360 it can be ignored because as soon as i 7949 05:10:01,440 --> 05:10:06,560 as we are increasing our end let's 7950 05:10:03,360 --> 05:10:09,760 suppose to 10 crore or more than 10 7951 05:10:06,560 --> 05:10:11,840 billion okay 10 100 billion is the input 7952 05:10:09,760 --> 05:10:14,480 size right at that time dividing by 2 7953 05:10:11,840 --> 05:10:17,280 still it is greater than what we can do 7954 05:10:14,480 --> 05:10:20,080 if we have something around log n okay 7955 05:10:17,280 --> 05:10:22,480 so obviously the average case is still 7956 05:10:20,080 --> 05:10:25,120 big o of n now when you talk about worst 7957 05:10:22,480 --> 05:10:27,120 case the element is found at the end or 7958 05:10:25,120 --> 05:10:28,958 it is not even found in the array you 7959 05:10:27,120 --> 05:10:30,798 are traversing 7960 05:10:28,958 --> 05:10:32,400 each and every element in the array and 7961 05:10:30,798 --> 05:10:34,878 still you were not able to find the 7962 05:10:32,400 --> 05:10:38,240 element so this is the worst case okay 7963 05:10:34,878 --> 05:10:40,320 now the worst case is always taken into 7964 05:10:38,240 --> 05:10:43,360 consideration because this is the 7965 05:10:40,320 --> 05:10:45,920 maximum resource that you require okay 7966 05:10:43,360 --> 05:10:48,840 when you talk about time complexity or 7967 05:10:45,920 --> 05:10:52,798 space complexity right 7968 05:10:48,840 --> 05:10:56,560 so time and space right so you always 7969 05:10:52,798 --> 05:10:57,360 talk about worst case scenario 7970 05:10:56,560 --> 05:10:59,680 right 7971 05:10:57,360 --> 05:11:02,718 and you always deduce your time and 7972 05:10:59,680 --> 05:11:06,000 space complexities based on worst case 7973 05:11:02,718 --> 05:11:08,400 it is computed because the analysis as 7974 05:11:06,000 --> 05:11:11,360 it dictates the maximum resource 7975 05:11:08,400 --> 05:11:13,840 requirements so worst case 7976 05:11:11,360 --> 05:11:16,320 helps us to find what are the maximum 7977 05:11:13,840 --> 05:11:19,120 risk resources that this is going to 7978 05:11:16,320 --> 05:11:21,600 take okay so we keep track of those 7979 05:11:19,120 --> 05:11:24,400 things and we allocate resources in such 7980 05:11:21,600 --> 05:11:27,200 manner so we always talk about worst 7981 05:11:24,400 --> 05:11:30,240 case now let me give you an example of 7982 05:11:27,200 --> 05:11:33,200 linear and binary search so in linear 7983 05:11:30,240 --> 05:11:35,280 search right we always talk about it and 7984 05:11:33,200 --> 05:11:37,440 in binary search 7985 05:11:35,280 --> 05:11:40,240 the space complexity of both these 7986 05:11:37,440 --> 05:11:42,000 algorithms is constant in worst case 7987 05:11:40,240 --> 05:11:44,958 right and if you talk about the best 7988 05:11:42,000 --> 05:11:47,360 cases of linear and binary research it 7989 05:11:44,958 --> 05:11:50,160 is again constant 7990 05:11:47,360 --> 05:11:53,200 right and it is again same okay 7991 05:11:50,160 --> 05:11:55,680 now when you talk about 7992 05:11:53,200 --> 05:11:58,080 we go off on now when you talk about the 7993 05:11:55,680 --> 05:12:00,958 worst case scenario right 7994 05:11:58,080 --> 05:12:03,440 and this is this is what helps us to 7995 05:12:00,958 --> 05:12:05,600 uh differentiate between binary and 7996 05:12:03,440 --> 05:12:07,440 linear so it's the worst case scenario 7997 05:12:05,600 --> 05:12:09,920 the worst case scenario for linear 7998 05:12:07,440 --> 05:12:12,878 searches because n wherein for binary 7999 05:12:09,920 --> 05:12:15,120 research is it is because of login now 8000 05:12:12,878 --> 05:12:16,958 now suppose there are eight inputs 8001 05:12:15,120 --> 05:12:20,000 you're going to traverse eight times in 8002 05:12:16,958 --> 05:12:23,440 it in this linear search but if you have 8003 05:12:20,000 --> 05:12:26,400 this log eight to the base two value 8004 05:12:23,440 --> 05:12:28,878 it it deduces or you can reduce it to 8005 05:12:26,400 --> 05:12:31,520 three into log two to the base two which 8006 05:12:28,878 --> 05:12:33,520 is three times so in this you are only 8007 05:12:31,520 --> 05:12:36,080 iterating three times in this you are 8008 05:12:33,520 --> 05:12:37,920 iterating eight times now as you can see 8009 05:12:36,080 --> 05:12:40,000 that input is very 8010 05:12:37,920 --> 05:12:43,360 low or very less that means there are 8011 05:12:40,000 --> 05:12:44,560 only eight elements as soon as our 8012 05:12:43,360 --> 05:12:47,040 what 8013 05:12:44,560 --> 05:12:48,718 our input our n or the number of 8014 05:12:47,040 --> 05:12:49,760 elements in the array 8015 05:12:48,718 --> 05:12:52,798 grows 8016 05:12:49,760 --> 05:12:55,440 this size of this i these iterations 8017 05:12:52,798 --> 05:12:58,160 become substantial and that is for the 8018 05:12:55,440 --> 05:12:59,840 same reason we always talk about worst 8019 05:12:58,160 --> 05:13:01,120 case scenario 8020 05:12:59,840 --> 05:13:04,000 okay 8021 05:13:01,120 --> 05:13:06,160 now let's talk about time complexity 8022 05:13:04,000 --> 05:13:08,718 it determines the total number of unit 8023 05:13:06,160 --> 05:13:11,200 operations to be undertaken to solve a 8024 05:13:08,718 --> 05:13:14,080 particular problem right let's suppose 8025 05:13:11,200 --> 05:13:16,000 uh let me take an example of 8026 05:13:14,080 --> 05:13:18,480 number of iterations like 8027 05:13:16,000 --> 05:13:21,200 so if you are able to search an element 8028 05:13:18,480 --> 05:13:23,920 in a single unit of operation then it is 8029 05:13:21,200 --> 05:13:25,600 termed to be a constant operation and if 8030 05:13:23,920 --> 05:13:27,440 you're doing the same for number of 8031 05:13:25,600 --> 05:13:29,840 inputs that let's suppose there are n 8032 05:13:27,440 --> 05:13:31,520 elements and you're traversing to find a 8033 05:13:29,840 --> 05:13:34,480 particular element you are traversing n 8034 05:13:31,520 --> 05:13:36,638 times so it's termed to be a big o of n 8035 05:13:34,480 --> 05:13:38,958 so to solve a particular problem you 8036 05:13:36,638 --> 05:13:41,120 determine the number of unit operations 8037 05:13:38,958 --> 05:13:43,600 now unit operation is an operation that 8038 05:13:41,120 --> 05:13:45,600 is independent and it can be broken down 8039 05:13:43,600 --> 05:13:47,680 into similar operations it is 8040 05:13:45,600 --> 05:13:49,600 independent of architecture it is 8041 05:13:47,680 --> 05:13:52,080 computed on the basis of algorithm 8042 05:13:49,600 --> 05:13:54,958 itself you have an algorithm in place 8043 05:13:52,080 --> 05:13:57,520 and based on that you compute 8044 05:13:54,958 --> 05:13:59,760 the time complexity of each and every 8045 05:13:57,520 --> 05:14:01,360 step right if there is a loop what will 8046 05:13:59,760 --> 05:14:03,520 happen if there's a condition what will 8047 05:14:01,360 --> 05:14:04,320 happen if there's a while look what will 8048 05:14:03,520 --> 05:14:07,360 happen 8049 05:14:04,320 --> 05:14:10,160 it's high priority critical in optimal 8050 05:14:07,360 --> 05:14:12,798 algorithm selection now depending on 8051 05:14:10,160 --> 05:14:15,040 this time complexity you can determine 8052 05:14:12,798 --> 05:14:17,760 whether you want to go with a particular 8053 05:14:15,040 --> 05:14:19,200 algorithm or not because let's suppose 8054 05:14:17,760 --> 05:14:21,200 you're trying to search in algorithm 8055 05:14:19,200 --> 05:14:23,200 right you can have two options right you 8056 05:14:21,200 --> 05:14:25,440 can use a bind research or you can use a 8057 05:14:23,200 --> 05:14:28,638 linear search now depending upon the 8058 05:14:25,440 --> 05:14:30,878 time complexity binary search works much 8059 05:14:28,638 --> 05:14:31,840 more faster as compared to in your 8060 05:14:30,878 --> 05:14:33,920 search 8061 05:14:31,840 --> 05:14:36,560 it works in order and and it works in 8062 05:14:33,920 --> 05:14:38,878 big offend so time complexity plays a 8063 05:14:36,560 --> 05:14:41,600 major role and there is always a 8064 05:14:38,878 --> 05:14:43,440 trade-off between the time and space 8065 05:14:41,600 --> 05:14:45,440 and in this session we will be covering 8066 05:14:43,440 --> 05:14:46,798 both of these things okay 8067 05:14:45,440 --> 05:14:49,040 so 8068 05:14:46,798 --> 05:14:51,120 now let's take an example and let's try 8069 05:14:49,040 --> 05:14:53,520 to determine the time complexity of that 8070 05:14:51,120 --> 05:14:55,520 particular example now let's suppose you 8071 05:14:53,520 --> 05:14:58,400 are given a for loop and in that for 8072 05:14:55,520 --> 05:14:59,760 loop you are iterating from zero to n 8073 05:14:58,400 --> 05:15:02,080 right 8074 05:14:59,760 --> 05:15:03,600 where n is i is less than n and then 8075 05:15:02,080 --> 05:15:05,040 you're doing the plus plus 8076 05:15:03,600 --> 05:15:07,280 now 8077 05:15:05,040 --> 05:15:10,000 you're doing some work in it okay now 8078 05:15:07,280 --> 05:15:12,638 since you're traversing from 0 to n now 8079 05:15:10,000 --> 05:15:15,040 this is a scenario wherein this will be 8080 05:15:12,638 --> 05:15:16,560 operated or this will work 8081 05:15:15,040 --> 05:15:19,440 the number of unit operations that are 8082 05:15:16,560 --> 05:15:21,600 present in this for loop is big of n 8083 05:15:19,440 --> 05:15:23,760 now similarly there is another for loop 8084 05:15:21,600 --> 05:15:26,000 in the same program which is operating 8085 05:15:23,760 --> 05:15:28,878 from 0 to n okay 8086 05:15:26,000 --> 05:15:31,600 and then the same thing there's a 8087 05:15:28,878 --> 05:15:34,958 nested for loop in it and 8088 05:15:31,600 --> 05:15:34,958 then what you are doing 8089 05:15:35,280 --> 05:15:40,000 you are traversing from 0 to 8090 05:15:38,240 --> 05:15:42,080 what 8091 05:15:40,000 --> 05:15:45,040 let's suppose this is traveled n is 8 8092 05:15:42,080 --> 05:15:46,798 right n is equal to add eight and in 8093 05:15:45,040 --> 05:15:48,718 this for loop you are traversing eight 8094 05:15:46,798 --> 05:15:51,520 times and in this while loop you are 8095 05:15:48,718 --> 05:15:53,440 traversing three times right 8096 05:15:51,520 --> 05:15:55,680 let's suppose you are multiplying every 8097 05:15:53,440 --> 05:15:56,958 every time by two okay 8098 05:15:55,680 --> 05:15:59,040 so now 8099 05:15:56,958 --> 05:16:02,240 in this for loop the nested for loop the 8100 05:15:59,040 --> 05:16:04,400 time complexity of the same is n log n 8101 05:16:02,240 --> 05:16:06,320 now why it is n log n because if you 8102 05:16:04,400 --> 05:16:08,560 observe carefully number of operations 8103 05:16:06,320 --> 05:16:10,240 that i told you is eight so outer loop 8104 05:16:08,560 --> 05:16:13,680 is working eight times and the inner 8105 05:16:10,240 --> 05:16:17,120 loop is working log 2 to the base 8 that 8106 05:16:13,680 --> 05:16:19,280 is 3 times so 8107 05:16:17,120 --> 05:16:21,040 now what is the scenario what will be 8108 05:16:19,280 --> 05:16:22,240 the total time complexity it will be big 8109 05:16:21,040 --> 05:16:25,600 of n 8110 05:16:22,240 --> 05:16:27,520 plus big o of n log n okay this will be 8111 05:16:25,600 --> 05:16:28,798 the time complexity 8112 05:16:27,520 --> 05:16:30,560 now since 8113 05:16:28,798 --> 05:16:32,798 now what is happening over here this is 8114 05:16:30,560 --> 05:16:35,280 the scenario wherein 8115 05:16:32,798 --> 05:16:37,600 what is happening it is happening m into 8116 05:16:35,280 --> 05:16:40,400 n that means you are multiplying big o 8117 05:16:37,600 --> 05:16:41,440 of n that is the nest for the nested one 8118 05:16:40,400 --> 05:16:42,878 okay 8119 05:16:41,440 --> 05:16:45,440 into big o of 8120 05:16:42,878 --> 05:16:47,440 log n so you are multiplying it whenever 8121 05:16:45,440 --> 05:16:49,600 you have a situation wherein you have a 8122 05:16:47,440 --> 05:16:52,160 for loop inside a for loop therein you 8123 05:16:49,600 --> 05:16:54,798 will multiply the time complexities 8124 05:16:52,160 --> 05:16:57,040 now you have a for loop outside and then 8125 05:16:54,798 --> 05:17:00,160 you have this nested for loop for this 8126 05:16:57,040 --> 05:17:01,200 nested for loop you have n into login 8127 05:17:00,160 --> 05:17:03,680 right 8128 05:17:01,200 --> 05:17:05,440 and for this you have 8129 05:17:03,680 --> 05:17:07,600 what we go of n 8130 05:17:05,440 --> 05:17:08,480 now this is a scenario wherein what 8131 05:17:07,600 --> 05:17:09,920 happens 8132 05:17:08,480 --> 05:17:13,680 m plus n 8133 05:17:09,920 --> 05:17:16,080 and in this you will always take 8134 05:17:13,680 --> 05:17:18,958 into the consideration the maximum 8135 05:17:16,080 --> 05:17:21,760 number of resources that are there so 8136 05:17:18,958 --> 05:17:24,400 that one for loop will take so in this 8137 05:17:21,760 --> 05:17:25,360 you're taking only n resources or this 8138 05:17:24,400 --> 05:17:27,680 is the 8139 05:17:25,360 --> 05:17:30,160 time complexity of this is n and in this 8140 05:17:27,680 --> 05:17:32,958 you are taking n log and so obviously 8141 05:17:30,160 --> 05:17:34,560 this is greater right so you always say 8142 05:17:32,958 --> 05:17:37,440 that the time complexity of that 8143 05:17:34,560 --> 05:17:39,440 particular problem will be n log n 8144 05:17:37,440 --> 05:17:42,320 because you are neglecting these smaller 8145 05:17:39,440 --> 05:17:44,560 terms as soon as our input is increased 8146 05:17:42,320 --> 05:17:45,680 these terms will be neglected and they 8147 05:17:44,560 --> 05:17:47,760 will not be 8148 05:17:45,680 --> 05:17:50,718 that much significant while you are 8149 05:17:47,760 --> 05:17:52,958 allocating resources 8150 05:17:50,718 --> 05:17:55,040 now what about the space complexity it 8151 05:17:52,958 --> 05:17:57,600 determines the total space to be 8152 05:17:55,040 --> 05:17:59,840 allocated in order to solve a particular 8153 05:17:57,600 --> 05:18:02,320 problem now whenever you are using any 8154 05:17:59,840 --> 05:18:04,320 extra space in the algorithm 8155 05:18:02,320 --> 05:18:07,200 while you are implementing it that is 8156 05:18:04,320 --> 05:18:09,120 termed to be as space complexity it can 8157 05:18:07,200 --> 05:18:10,638 be in the form of arrays that extra 8158 05:18:09,120 --> 05:18:12,958 array that you have used to solve a 8159 05:18:10,638 --> 05:18:15,360 problem or an extra linked list that you 8160 05:18:12,958 --> 05:18:16,560 have used to solve a problem or a stack 8161 05:18:15,360 --> 05:18:19,520 or a cue 8162 05:18:16,560 --> 05:18:22,000 right it involves memory of the computer 8163 05:18:19,520 --> 05:18:23,840 right you are in your involving memory 8164 05:18:22,000 --> 05:18:25,760 right your d you're taking something 8165 05:18:23,840 --> 05:18:27,760 extra right solve a particular problem 8166 05:18:25,760 --> 05:18:30,480 let's talk about merge sort then you 8167 05:18:27,760 --> 05:18:32,958 take extra memory and in worst case you 8168 05:18:30,480 --> 05:18:34,798 can take people an extra memory that 8169 05:18:32,958 --> 05:18:36,560 means the number of elements that are 8170 05:18:34,798 --> 05:18:38,080 present in the array you're taking the 8171 05:18:36,560 --> 05:18:40,400 same array 8172 05:18:38,080 --> 05:18:43,600 as an auxiliary memory that extra memory 8173 05:18:40,400 --> 05:18:45,680 right it's low priority critical in 8174 05:18:43,600 --> 05:18:48,160 optimal algorithm selection when you 8175 05:18:45,680 --> 05:18:51,520 talk about space complexity we try to 8176 05:18:48,160 --> 05:18:54,878 avoid any extra space we try to solve it 8177 05:18:51,520 --> 05:18:57,360 with minimal space right so the we don't 8178 05:18:54,878 --> 05:18:59,120 use any of these auxiliary memories okay 8179 05:18:57,360 --> 05:19:02,000 now let me take an example of a merge 8180 05:18:59,120 --> 05:19:04,480 sort wherein in that merge sort every 8181 05:19:02,000 --> 05:19:07,040 time you create a left array for the 8182 05:19:04,480 --> 05:19:08,878 left side and the right side every time 8183 05:19:07,040 --> 05:19:11,200 you create these sub arrays and then you 8184 05:19:08,878 --> 05:19:13,120 calculate and then then you divide them 8185 05:19:11,200 --> 05:19:14,560 into further further and further unless 8186 05:19:13,120 --> 05:19:16,798 there is no 8187 05:19:14,560 --> 05:19:18,798 possible division and then you start 8188 05:19:16,798 --> 05:19:21,360 merging them so again this is a 8189 05:19:18,798 --> 05:19:25,040 condition or this is a scenario wherein 8190 05:19:21,360 --> 05:19:27,120 you are using extra memory of big o of n 8191 05:19:25,040 --> 05:19:30,080 now if you talk about simpler examples 8192 05:19:27,120 --> 05:19:32,560 let's suppose you are trying to 8193 05:19:30,080 --> 05:19:34,798 change the positions of even and odd 8194 05:19:32,560 --> 05:19:36,878 numbers right 8195 05:19:34,798 --> 05:19:38,878 so let's suppose you have these indexes 8196 05:19:36,878 --> 05:19:41,200 right in the array and you have some 8197 05:19:38,878 --> 05:19:42,480 values that suppose 10 20 8198 05:19:41,200 --> 05:19:44,638 15 8199 05:19:42,480 --> 05:19:46,878 20 15 8200 05:19:44,638 --> 05:19:49,840 and 10. let's suppose these are the 8201 05:19:46,878 --> 05:19:52,718 values right now you want to use or you 8202 05:19:49,840 --> 05:19:54,638 want to find out the frequency of these 8203 05:19:52,718 --> 05:19:56,878 numbers so you will be using 8204 05:19:54,638 --> 05:19:59,120 another array and in that area what you 8205 05:19:56,878 --> 05:20:00,560 will be doing let's suppose you have you 8206 05:19:59,120 --> 05:20:02,320 are picking up this 10 and you are 8207 05:20:00,560 --> 05:20:04,878 storing it in this now you'll be 8208 05:20:02,320 --> 05:20:07,520 checking okay if this is already present 8209 05:20:04,878 --> 05:20:10,480 pick up the counter and keep track of 8210 05:20:07,520 --> 05:20:12,400 how many times this 10 occurred now this 8211 05:20:10,480 --> 05:20:13,840 will work only for when you are trying 8212 05:20:12,400 --> 05:20:16,240 to search 8213 05:20:13,840 --> 05:20:18,638 10 throughout the area i'm taking a name 8214 05:20:16,240 --> 05:20:20,638 example right so i'm taking this example 8215 05:20:18,638 --> 05:20:22,320 and i'm trying to search 10 and i'm 8216 05:20:20,638 --> 05:20:24,480 trying to calculate how many times it 8217 05:20:22,320 --> 05:20:26,638 has occurred so you place a 10 in this 8218 05:20:24,480 --> 05:20:27,600 array and then what you're doing you 8219 05:20:26,638 --> 05:20:29,520 will just 8220 05:20:27,600 --> 05:20:31,920 iterate through the array and then keep 8221 05:20:29,520 --> 05:20:33,760 a track of this uh how many times this 8222 05:20:31,920 --> 05:20:36,080 has occurred and keep incrementing your 8223 05:20:33,760 --> 05:20:39,440 counter right so now you're using an 8224 05:20:36,080 --> 05:20:41,680 extra space right now even if this 8225 05:20:39,440 --> 05:20:43,680 example is not understood let me take 8226 05:20:41,680 --> 05:20:46,480 another example let's suppose you're 8227 05:20:43,680 --> 05:20:47,920 using a recursion right 8228 05:20:46,480 --> 05:20:50,320 now you might be thinking okay 8229 05:20:47,920 --> 05:20:52,878 internally you are not using any extra 8230 05:20:50,320 --> 05:20:54,638 space but what happens in recursion 8231 05:20:52,878 --> 05:20:56,958 every time the recursion is there that 8232 05:20:54,638 --> 05:20:59,760 means function calling itself an 8233 05:20:56,958 --> 05:21:02,480 activation record is created and a stack 8234 05:20:59,760 --> 05:21:04,400 is used internally wherein your main 8235 05:21:02,480 --> 05:21:06,080 function will be there and the number of 8236 05:21:04,400 --> 05:21:07,840 function calls right the function 8237 05:21:06,080 --> 05:21:10,160 calling itself function calling itself 8238 05:21:07,840 --> 05:21:11,920 function calling itself will be will be 8239 05:21:10,160 --> 05:21:13,440 we will be pushing it on the stack and 8240 05:21:11,920 --> 05:21:15,680 once you have returned then we will be 8241 05:21:13,440 --> 05:21:17,760 popping it out right so internally you 8242 05:21:15,680 --> 05:21:20,160 are using an extra memory and it depends 8243 05:21:17,760 --> 05:21:23,040 on from program to program are you are 8244 05:21:20,160 --> 05:21:26,080 you using big o of n or using log in 8245 05:21:23,040 --> 05:21:28,878 space but internally recursion uses 8246 05:21:26,080 --> 05:21:31,440 extra memory so an auxiliary memory or 8247 05:21:28,878 --> 05:21:34,000 you can say that extra memory is used 8248 05:21:31,440 --> 05:21:35,840 while you are using recursion so that is 8249 05:21:34,000 --> 05:21:38,480 why we try to avoid 8250 05:21:35,840 --> 05:21:40,718 recursive problems and we 8251 05:21:38,480 --> 05:21:43,360 we try to solve our problems using 8252 05:21:40,718 --> 05:21:45,840 iteration so which is obviously that we 8253 05:21:43,360 --> 05:21:48,160 are not using any auxiliary memory 8254 05:21:45,840 --> 05:21:51,440 although it is internally but still 8255 05:21:48,160 --> 05:21:54,240 recursion uses extra memory and that 8256 05:21:51,440 --> 05:21:56,798 thing is taken into concentration now 8257 05:21:54,240 --> 05:21:59,680 you might be thinking okay now should we 8258 05:21:56,798 --> 05:22:01,280 use or should we save space or should we 8259 05:21:59,680 --> 05:22:03,440 use less time 8260 05:22:01,280 --> 05:22:05,920 should we go for time efficient com pro 8261 05:22:03,440 --> 05:22:08,480 algorithm or space efficient algorithm 8262 05:22:05,920 --> 05:22:10,240 now there is always a trade-off between 8263 05:22:08,480 --> 05:22:12,878 space and time 8264 05:22:10,240 --> 05:22:15,200 so in some problems they might ask you 8265 05:22:12,878 --> 05:22:17,600 to save or to write an efficient 8266 05:22:15,200 --> 05:22:19,840 algorithm keeping the time into 8267 05:22:17,600 --> 05:22:22,320 consideration that you have to you you 8268 05:22:19,840 --> 05:22:24,160 don't have to use extra time but in some 8269 05:22:22,320 --> 05:22:26,320 they might say okay you can use extra 8270 05:22:24,160 --> 05:22:29,760 space but time should be 8271 05:22:26,320 --> 05:22:32,480 efficient or in some cases they say use 8272 05:22:29,760 --> 05:22:34,160 as much time as you want but space 8273 05:22:32,480 --> 05:22:36,320 should be less 8274 05:22:34,160 --> 05:22:39,040 so that is also there so there is always 8275 05:22:36,320 --> 05:22:41,680 a trade-off between both of these things 8276 05:22:39,040 --> 05:22:43,520 now let's see how we can find the time 8277 05:22:41,680 --> 05:22:46,878 and space complexity 8278 05:22:43,520 --> 05:22:48,638 of any given code or algorithm 8279 05:22:46,878 --> 05:22:50,958 so basically here i am going to give you 8280 05:22:48,638 --> 05:22:53,360 some tips and tricks when it comes to 8281 05:22:50,958 --> 05:22:55,520 finding the time complexity of any given 8282 05:22:53,360 --> 05:22:58,400 code or for that matter even space 8283 05:22:55,520 --> 05:23:00,080 complexity for any given code so what 8284 05:22:58,400 --> 05:23:01,760 happens in any programming language 8285 05:23:00,080 --> 05:23:03,600 there are certain constructs or certain 8286 05:23:01,760 --> 05:23:05,760 programming uh you know concepts which 8287 05:23:03,600 --> 05:23:06,798 we use like for example conditional 8288 05:23:05,760 --> 05:23:09,200 loops 8289 05:23:06,798 --> 05:23:12,558 conditional statements then no looping 8290 05:23:09,200 --> 05:23:16,000 constructs so nested loops so how does 8291 05:23:12,558 --> 05:23:18,400 you know time complexity is found or uh 8292 05:23:16,000 --> 05:23:20,320 in this cases and also the space complex 8293 05:23:18,400 --> 05:23:23,120 like what is the total space being taken 8294 05:23:20,320 --> 05:23:24,798 while executing that particular code no 8295 05:23:23,120 --> 05:23:27,200 space when i talk about know how many 8296 05:23:24,798 --> 05:23:29,920 variable at a time resides in memory 8297 05:23:27,200 --> 05:23:32,160 that decides the space complexity okay 8298 05:23:29,920 --> 05:23:34,480 and time complexity is again the total 8299 05:23:32,160 --> 05:23:36,240 time taken maybe in terms of n we 8300 05:23:34,480 --> 05:23:38,558 calculate if suppose n is a number of 8301 05:23:36,240 --> 05:23:40,718 input so if n or two is a number of 8302 05:23:38,558 --> 05:23:42,240 inputs so for two input what's the total 8303 05:23:40,718 --> 05:23:44,320 time taken 8304 05:23:42,240 --> 05:23:46,160 for each code to run or to execute so 8305 05:23:44,320 --> 05:23:48,718 we'll assume there are n inputs and for 8306 05:23:46,160 --> 05:23:50,878 n given input how many times the you 8307 05:23:48,718 --> 05:23:53,840 know the particular code runs that 8308 05:23:50,878 --> 05:23:55,600 decides your time complexity okay so i'm 8309 05:23:53,840 --> 05:23:57,440 going to talk about few small tips and 8310 05:23:55,600 --> 05:23:59,120 tricks about finding space and time 8311 05:23:57,440 --> 05:24:01,520 complexity to you 8312 05:23:59,120 --> 05:24:04,638 starting from 8313 05:24:01,520 --> 05:24:07,200 uh the first aspect that is your 8314 05:24:04,638 --> 05:24:09,360 looping constructs okay so if i talk 8315 05:24:07,200 --> 05:24:11,680 about loop the very frequently used loop 8316 05:24:09,360 --> 05:24:13,280 is your for loop so let's see if you 8317 05:24:11,680 --> 05:24:16,638 know int i 8318 05:24:13,280 --> 05:24:18,160 is equal to zero and it goes till i uh 8319 05:24:16,638 --> 05:24:19,200 you know less than 8320 05:24:18,160 --> 05:24:21,840 n 8321 05:24:19,200 --> 05:24:24,400 and i plus plus and then there is some 8322 05:24:21,840 --> 05:24:25,840 statement here maybe uh x is equal to y 8323 05:24:24,400 --> 05:24:27,520 plus z 8324 05:24:25,840 --> 05:24:29,520 let's assume that all these you know 8325 05:24:27,520 --> 05:24:32,240 variables were declared before in any 8326 05:24:29,520 --> 05:24:34,000 programming language for that matter 8327 05:24:32,240 --> 05:24:35,840 okay so what will if i tell you that 8328 05:24:34,000 --> 05:24:38,160 okay find out the time complexity what 8329 05:24:35,840 --> 05:24:39,920 time this complete code is going to take 8330 05:24:38,160 --> 05:24:41,040 if n is the input 8331 05:24:39,920 --> 05:24:42,878 okay 8332 05:24:41,040 --> 05:24:45,200 what you will do is 8333 05:24:42,878 --> 05:24:46,798 okay if i assume there is no the number 8334 05:24:45,200 --> 05:24:49,520 of input is only one 8335 05:24:46,798 --> 05:24:50,878 so the for loop will run only one time 8336 05:24:49,520 --> 05:24:53,200 and this complete statement will run 8337 05:24:50,878 --> 05:24:54,878 only one time so time taken is only one 8338 05:24:53,200 --> 05:24:56,798 unit of time 8339 05:24:54,878 --> 05:25:00,558 right and what will be the space taken 8340 05:24:56,798 --> 05:25:02,400 the time only this variable uh you know 8341 05:25:00,558 --> 05:25:05,120 these two variables will be stored at a 8342 05:25:02,400 --> 05:25:06,480 time in the in the 8343 05:25:05,120 --> 05:25:08,798 memory 8344 05:25:06,480 --> 05:25:10,718 right so no matter you know what is the 8345 05:25:08,798 --> 05:25:12,320 input every time you need only two 8346 05:25:10,718 --> 05:25:13,600 variables at a time to be stored there 8347 05:25:12,320 --> 05:25:15,760 in 8348 05:25:13,600 --> 05:25:17,200 memory and when the sum is done that is 8349 05:25:15,760 --> 05:25:19,680 discarded and then only there is only 8350 05:25:17,200 --> 05:25:22,718 one variable then after that right so 8351 05:25:19,680 --> 05:25:24,958 space complexity is constant here 8352 05:25:22,718 --> 05:25:26,798 right only one unit of space is taken 8353 05:25:24,958 --> 05:25:28,798 because every time no matter how much is 8354 05:25:26,798 --> 05:25:30,878 the input every time that same two 8355 05:25:28,798 --> 05:25:33,120 variables occupies the space so space 8356 05:25:30,878 --> 05:25:35,600 here complexity will be of one 8357 05:25:33,120 --> 05:25:37,200 but time complexity will be for one 8358 05:25:35,600 --> 05:25:39,520 element this for loop will run only one 8359 05:25:37,200 --> 05:25:42,000 time so time complexity will be also one 8360 05:25:39,520 --> 05:25:43,680 unit of time it is going to take for two 8361 05:25:42,000 --> 05:25:45,040 uh no elements this for loop will run 8362 05:25:43,680 --> 05:25:46,878 two times 8363 05:25:45,040 --> 05:25:48,878 similarly for n elements this for loop 8364 05:25:46,878 --> 05:25:50,400 is going to run n times so this 8365 05:25:48,878 --> 05:25:53,200 statement will be executed how many 8366 05:25:50,400 --> 05:25:56,160 times n times so we can say that it 8367 05:25:53,200 --> 05:25:59,600 takes o of n unit of time 8368 05:25:56,160 --> 05:26:02,160 okay big o of n unit of time 8369 05:25:59,600 --> 05:26:04,958 it takes to run the code 8370 05:26:02,160 --> 05:26:07,280 okay now coming to the another thing is 8371 05:26:04,958 --> 05:26:08,638 your nested loops 8372 05:26:07,280 --> 05:26:10,798 okay 8373 05:26:08,638 --> 05:26:12,718 so guys whenever you find the small so 8374 05:26:10,798 --> 05:26:14,080 smaller smaller codes you can fragment 8375 05:26:12,718 --> 05:26:16,320 together and find out the overall time 8376 05:26:14,080 --> 05:26:18,000 complexity for any larger code so 8377 05:26:16,320 --> 05:26:20,958 talking about nested loop so nested loop 8378 05:26:18,000 --> 05:26:22,400 how it runs 4 is you know end suppose i 8379 05:26:20,958 --> 05:26:24,718 is equal to 0 8380 05:26:22,400 --> 05:26:26,638 i less than n 8381 05:26:24,718 --> 05:26:30,160 i plus plus and then inside this there 8382 05:26:26,638 --> 05:26:34,958 is one more for loop okay 8383 05:26:30,160 --> 05:26:36,558 maybe int j of 0 to j less than n 8384 05:26:34,958 --> 05:26:38,558 j plus plus 8385 05:26:36,558 --> 05:26:40,638 and then this there is a statement may 8386 05:26:38,558 --> 05:26:42,638 be same statement i am writing index is 8387 05:26:40,638 --> 05:26:44,480 equal to y plus z 8388 05:26:42,638 --> 05:26:46,878 and then this closes the bracket 8389 05:26:44,480 --> 05:26:48,798 now how many times this code will run 8390 05:26:46,878 --> 05:26:50,958 again space complexity is going to be of 8391 05:26:48,798 --> 05:26:53,040 one guys because you can see the actual 8392 05:26:50,958 --> 05:26:55,200 operation being done is only this and 8393 05:26:53,040 --> 05:26:57,040 every time it requires two unit of space 8394 05:26:55,200 --> 05:26:58,558 so it's constant throughout the 8395 05:26:57,040 --> 05:27:00,958 execution it doesn't depends on the 8396 05:26:58,558 --> 05:27:03,360 number of inputs so it is o of 1 the 8397 05:27:00,958 --> 05:27:05,440 constant time the constant space 8398 05:27:03,360 --> 05:27:07,120 talking about time complexity so you see 8399 05:27:05,440 --> 05:27:09,440 here 8400 05:27:07,120 --> 05:27:11,920 for each value of i 8401 05:27:09,440 --> 05:27:14,480 j runs from 0 to n 8402 05:27:11,920 --> 05:27:16,638 so if value of i is 1 8403 05:27:14,480 --> 05:27:19,120 or you can say value of i is 0 then this 8404 05:27:16,638 --> 05:27:21,680 runs from 0 to n that is n times 8405 05:27:19,120 --> 05:27:24,480 a value of i is 1 then also this runs 8406 05:27:21,680 --> 05:27:26,400 from 0 to n n times a value of i is 2 8407 05:27:24,480 --> 05:27:28,958 then also this runs from 0 to n for n 8408 05:27:26,400 --> 05:27:30,320 times similarly so on 8409 05:27:28,958 --> 05:27:31,920 okay 8410 05:27:30,320 --> 05:27:35,200 it means 8411 05:27:31,920 --> 05:27:37,520 if this goes from 0 to n 8412 05:27:35,200 --> 05:27:39,760 this will also go from 8413 05:27:37,520 --> 05:27:42,798 for each value of i it is going to go 8414 05:27:39,760 --> 05:27:42,798 from 0 to n 8415 05:27:43,040 --> 05:27:46,160 1 to n because it's less than not less 8416 05:27:44,958 --> 05:27:49,440 than equal to 8417 05:27:46,160 --> 05:27:50,718 or you can say 0 to n minus 1 8418 05:27:49,440 --> 05:27:54,798 right 8419 05:27:50,718 --> 05:27:56,878 it means what for i value as 0 8420 05:27:54,798 --> 05:27:59,760 this code runs n times 8421 05:27:56,878 --> 05:28:02,400 then i value of 1 this code runs n times 8422 05:27:59,760 --> 05:28:04,240 i value of n this code will run n into n 8423 05:28:02,400 --> 05:28:06,400 times 8424 05:28:04,240 --> 05:28:07,840 yes or no like 8425 05:28:06,400 --> 05:28:10,798 are you getting my point so this is 8426 05:28:07,840 --> 05:28:12,558 mathematical concept here so n into n 8427 05:28:10,798 --> 05:28:16,080 that is the time complexity for this 8428 05:28:12,558 --> 05:28:16,080 will be o of n square 8429 05:28:16,638 --> 05:28:19,520 right 8430 05:28:17,520 --> 05:28:21,760 now coming to the third type of a 8431 05:28:19,520 --> 05:28:24,638 concept if else loop conditional 8432 05:28:21,760 --> 05:28:27,120 statement okay so if says if 8433 05:28:24,638 --> 05:28:29,680 and there is some statement over here 8434 05:28:27,120 --> 05:28:31,200 okay then it says else and then some 8435 05:28:29,680 --> 05:28:33,760 statement over here suppose a time 8436 05:28:31,200 --> 05:28:36,798 complexity of this is o of n and time 8437 05:28:33,760 --> 05:28:38,638 complexity of this is o of n square 8438 05:28:36,798 --> 05:28:41,840 which will be the final time complexity 8439 05:28:38,638 --> 05:28:44,400 out of this complete fl slope 8440 05:28:41,840 --> 05:28:45,600 final time complexity will be whichever 8441 05:28:44,400 --> 05:28:48,160 part 8442 05:28:45,600 --> 05:28:50,480 of either if or else whichever part has 8443 05:28:48,160 --> 05:28:52,798 less time complexity which is taking 8444 05:28:50,480 --> 05:28:54,400 less time to execute if this is taking n 8445 05:28:52,798 --> 05:28:57,280 times to execute this thing n square 8446 05:28:54,400 --> 05:28:59,440 times to execute definitely n is smaller 8447 05:28:57,280 --> 05:29:01,360 n is lesser value than n square 8448 05:28:59,440 --> 05:29:03,840 so the overall time complexity for out 8449 05:29:01,360 --> 05:29:05,840 of this f else loop will be o of 8450 05:29:03,840 --> 05:29:08,400 n 8451 05:29:05,840 --> 05:29:08,400 is it clear 8452 05:29:08,480 --> 05:29:10,798 right 8453 05:29:09,280 --> 05:29:12,718 okay so 8454 05:29:10,798 --> 05:29:14,480 this is your uh these are the three 8455 05:29:12,718 --> 05:29:17,200 constructs now let's see if there are 8456 05:29:14,480 --> 05:29:19,040 sequential statements like definitely in 8457 05:29:17,200 --> 05:29:20,958 any given program there is not only just 8458 05:29:19,040 --> 05:29:22,958 for loop or just you know or nested for 8459 05:29:20,958 --> 05:29:25,120 loop or flow there are other statements 8460 05:29:22,958 --> 05:29:26,958 also along with these loops so in that 8461 05:29:25,120 --> 05:29:29,520 case how the time complexity or space 8462 05:29:26,958 --> 05:29:31,440 complexity is taken care again guys here 8463 05:29:29,520 --> 05:29:33,280 i am i'm not told about space complexity 8464 05:29:31,440 --> 05:29:35,040 in this fl slope so space complexity 8465 05:29:33,280 --> 05:29:37,280 again depends on what you know statement 8466 05:29:35,040 --> 05:29:38,638 is being over like at a given point of 8467 05:29:37,280 --> 05:29:40,638 time how much is the space being 8468 05:29:38,638 --> 05:29:41,760 occupied in memory to execute that 8469 05:29:40,638 --> 05:29:43,680 statement 8470 05:29:41,760 --> 05:29:45,520 that's does that space allotment depends 8471 05:29:43,680 --> 05:29:47,520 on the number of input 8472 05:29:45,520 --> 05:29:49,040 then what how it depends on number of 8473 05:29:47,520 --> 05:29:51,520 input will be your space complexity 8474 05:29:49,040 --> 05:29:53,840 otherwise it always remains constant 8475 05:29:51,520 --> 05:29:55,520 fine let's see the fourth aspects where 8476 05:29:53,840 --> 05:29:56,878 there are other statements also along 8477 05:29:55,520 --> 05:29:58,240 with these looping constructs and 8478 05:29:56,878 --> 05:30:00,000 conditional statements in a given 8479 05:29:58,240 --> 05:30:01,760 program or a code 8480 05:30:00,000 --> 05:30:03,120 so now the fourth condition which i am 8481 05:30:01,760 --> 05:30:04,958 going to see here is 8482 05:30:03,120 --> 05:30:08,080 that suppose there is a statement you 8483 05:30:04,958 --> 05:30:11,680 know x is equal to y plus z 8484 05:30:08,080 --> 05:30:13,440 okay then there is a for loop over here 8485 05:30:11,680 --> 05:30:16,160 then there is a 8486 05:30:13,440 --> 05:30:17,920 maybe no do while loop 8487 05:30:16,160 --> 05:30:18,958 so it does something and then while loop 8488 05:30:17,920 --> 05:30:20,240 is there 8489 05:30:18,958 --> 05:30:22,320 okay 8490 05:30:20,240 --> 05:30:23,440 then there is again maybe some statement 8491 05:30:22,320 --> 05:30:26,798 uh 8492 05:30:23,440 --> 05:30:29,440 sorry some statement here maybe is 8493 05:30:26,798 --> 05:30:31,040 a uh is equal to a 8494 05:30:29,440 --> 05:30:32,638 plus plus 8495 05:30:31,040 --> 05:30:33,360 fine 8496 05:30:32,638 --> 05:30:34,878 now 8497 05:30:33,360 --> 05:30:36,080 let's see from so what you'll do is 8498 05:30:34,878 --> 05:30:37,840 we'll start from the beginning of the 8499 05:30:36,080 --> 05:30:40,160 code so first code here takes how much 8500 05:30:37,840 --> 05:30:41,600 time is it dependent on the input no 8501 05:30:40,160 --> 05:30:44,080 every time it's going to be y plus z 8502 05:30:41,600 --> 05:30:46,878 only no matter input is 1 2 3 or 8503 05:30:44,080 --> 05:30:49,040 whatever so it is constant time 8504 05:30:46,878 --> 05:30:51,280 suppose this for loop whichever whatever 8505 05:30:49,040 --> 05:30:53,440 is the for loop over here this takes com 8506 05:30:51,280 --> 05:30:55,280 time complexity of o of n 8507 05:30:53,440 --> 05:30:56,958 okay then there is this do while loop 8508 05:30:55,280 --> 05:30:59,760 which also takes the time complexity of 8509 05:30:56,958 --> 05:31:01,600 o of n okay ah then there is this a is 8510 05:30:59,760 --> 05:31:03,120 going a plus plus statement which takes 8511 05:31:01,600 --> 05:31:05,280 constant time 8512 05:31:03,120 --> 05:31:06,958 okay then in this case what will be the 8513 05:31:05,280 --> 05:31:08,958 final time complexity 8514 05:31:06,958 --> 05:31:12,160 so the final time complexity is going to 8515 05:31:08,958 --> 05:31:14,480 be the summation of all that is 1 plus n 8516 05:31:12,160 --> 05:31:16,718 plus n plus 1 8517 05:31:14,480 --> 05:31:16,718 okay 8518 05:31:16,798 --> 05:31:20,958 and this will be your 8519 05:31:18,718 --> 05:31:22,320 so constants are always removed because 8520 05:31:20,958 --> 05:31:23,840 it doesn't play much role because 8521 05:31:22,320 --> 05:31:25,760 constant is going to be always taking 8522 05:31:23,840 --> 05:31:27,680 constant time so that's not 8523 05:31:25,760 --> 05:31:31,040 that doesn't affect our performance 8524 05:31:27,680 --> 05:31:32,878 right of the code so we'll remove those 8525 05:31:31,040 --> 05:31:35,760 n and n so 8526 05:31:32,878 --> 05:31:38,000 n plus n will be 2 of n right again this 8527 05:31:35,760 --> 05:31:40,878 is a constant which will be eliminated 8528 05:31:38,000 --> 05:31:42,718 and this will be just n 8529 05:31:40,878 --> 05:31:45,040 right so the total time complexity will 8530 05:31:42,718 --> 05:31:47,120 be o of n suppose out of this one of the 8531 05:31:45,040 --> 05:31:49,040 time complexity would have been of n 8532 05:31:47,120 --> 05:31:50,718 square 8533 05:31:49,040 --> 05:31:52,638 correct so 8534 05:31:50,718 --> 05:31:56,160 the time complexity here suppose this is 8535 05:31:52,638 --> 05:31:59,280 so in that case it will be 0 1 plus n 8536 05:31:56,160 --> 05:32:00,878 plus n square plus 1 correct so o 8537 05:31:59,280 --> 05:32:03,680 constants will be again removed it will 8538 05:32:00,878 --> 05:32:04,558 be n square plus n 8539 05:32:03,680 --> 05:32:06,160 right 8540 05:32:04,558 --> 05:32:08,718 again you see here 8541 05:32:06,160 --> 05:32:09,840 no compared to n square n is a smaller 8542 05:32:08,718 --> 05:32:12,160 value 8543 05:32:09,840 --> 05:32:13,760 right so our main focus should be on the 8544 05:32:12,160 --> 05:32:16,718 bigger values because this becomes 8545 05:32:13,760 --> 05:32:18,638 negligible in front of this one 8546 05:32:16,718 --> 05:32:21,600 so our total time complexity will be o 8547 05:32:18,638 --> 05:32:21,600 of n square only 8548 05:32:21,680 --> 05:32:25,680 getting it so that's how the time and 8549 05:32:23,840 --> 05:32:27,440 space complexity is calculated for any 8550 05:32:25,680 --> 05:32:29,280 given code 8551 05:32:27,440 --> 05:32:31,440 basically time complexity some tips and 8552 05:32:29,280 --> 05:32:33,360 tricks i gave you here which you can use 8553 05:32:31,440 --> 05:32:34,718 to calculate uh now simply by just 8554 05:32:33,360 --> 05:32:36,718 looking at the code you can make out 8555 05:32:34,718 --> 05:32:38,718 what is the no time complexity for that 8556 05:32:36,718 --> 05:32:40,958 given code 8557 05:32:38,718 --> 05:32:43,200 there are other methods also like master 8558 05:32:40,958 --> 05:32:44,638 theorem which is properly used for 8559 05:32:43,200 --> 05:32:45,600 finding the time and space complexity 8560 05:32:44,638 --> 05:32:47,760 but 8561 05:32:45,600 --> 05:32:49,760 that's a time taking procedure it's for 8562 05:32:47,760 --> 05:32:51,120 a full you know process or steps that 8563 05:32:49,760 --> 05:32:52,878 you have to follow but these tips and 8564 05:32:51,120 --> 05:32:55,680 tricks will help you quickly find out or 8565 05:32:52,878 --> 05:32:57,600 verify the time and space complexity of 8566 05:32:55,680 --> 05:32:59,200 your given code 8567 05:32:57,600 --> 05:33:01,360 now let's talk about searching 8568 05:32:59,200 --> 05:33:02,878 algorithms and the first searching 8569 05:33:01,360 --> 05:33:06,160 algorithm that we are going to talk 8570 05:33:02,878 --> 05:33:08,638 about is linear search algorithm 8571 05:33:06,160 --> 05:33:11,120 so what is linear search 8572 05:33:08,638 --> 05:33:13,040 it helps us to search an element in a 8573 05:33:11,120 --> 05:33:15,840 linear data structure now let's talk 8574 05:33:13,040 --> 05:33:18,240 about one example wherein we will be 8575 05:33:15,840 --> 05:33:20,240 searching some element inside the area 8576 05:33:18,240 --> 05:33:22,320 so let's suppose this is an array and 8577 05:33:20,240 --> 05:33:27,280 the elements are 8578 05:33:22,320 --> 05:33:30,160 10 20 30 40 and 50. now if we're trying 8579 05:33:27,280 --> 05:33:34,160 to search an element that is 50 inside 8580 05:33:30,160 --> 05:33:37,040 this array how linear search works is 8581 05:33:34,160 --> 05:33:39,440 that it checks each and every element 8582 05:33:37,040 --> 05:33:41,920 that is to be searched right that is 8583 05:33:39,440 --> 05:33:44,958 there in the element array right let's 8584 05:33:41,920 --> 05:33:47,280 talk about this example here 50 now 50 8585 05:33:44,958 --> 05:33:50,160 will be compared right we'll check 8586 05:33:47,280 --> 05:33:52,718 is this 10 equal to 50 no 8587 05:33:50,160 --> 05:33:55,520 is this 20 equal to 15 no 8588 05:33:52,718 --> 05:33:59,840 is this 50 equal to 30 is this equal to 8589 05:33:55,520 --> 05:34:01,360 40 is this equal to 50 yes so here we 8590 05:33:59,840 --> 05:34:03,840 were able to 8591 05:34:01,360 --> 05:34:06,320 do a linear search right we were 8592 05:34:03,840 --> 05:34:09,280 searching for this element inside this 8593 05:34:06,320 --> 05:34:12,240 area one by one we compared first with 8594 05:34:09,280 --> 05:34:15,680 10 then 20 then 30 and then 40 and 8595 05:34:12,240 --> 05:34:19,040 finally then with 50 at the end we were 8596 05:34:15,680 --> 05:34:22,400 able to find this element in the array 8597 05:34:19,040 --> 05:34:23,760 in a linear fashion now this is what is 8598 05:34:22,400 --> 05:34:25,600 termed as 8599 05:34:23,760 --> 05:34:27,680 linear search 8600 05:34:25,600 --> 05:34:30,360 now let's talk about linear search 8601 05:34:27,680 --> 05:34:33,280 algorithm since it is a very 8602 05:34:30,360 --> 05:34:35,600 straightforward or you can see a brute 8603 05:34:33,280 --> 05:34:37,520 force algorithm 8604 05:34:35,600 --> 05:34:40,400 right it's a brute force algorithm of 8605 05:34:37,520 --> 05:34:43,200 finding the element in the array so this 8606 05:34:40,400 --> 05:34:45,920 is how it works right we have one for 8607 05:34:43,200 --> 05:34:48,000 loop wherein we will be 8608 05:34:45,920 --> 05:34:50,798 what i trading through all the elements 8609 05:34:48,000 --> 05:34:52,958 that is from 0 to n and inside that what 8610 05:34:50,798 --> 05:34:55,120 are we doing we are looking for the item 8611 05:34:52,958 --> 05:34:57,520 that is that element that we want to 8612 05:34:55,120 --> 05:34:59,840 search right let's suppose this is 50 8613 05:34:57,520 --> 05:35:02,558 right this 50 will be we will check if 8614 05:34:59,840 --> 05:35:06,638 this 50 is equal to the element that is 8615 05:35:02,558 --> 05:35:09,200 ar of i right and then if that is the 8616 05:35:06,638 --> 05:35:12,000 case if we find out the element 8617 05:35:09,200 --> 05:35:15,040 in the entire area we will return its 8618 05:35:12,000 --> 05:35:17,520 index right that is index right that i 8619 05:35:15,040 --> 05:35:20,558 will we will return this i 8620 05:35:17,520 --> 05:35:23,360 now there might be the case as well if 8621 05:35:20,558 --> 05:35:25,760 we are at the end of the array and we 8622 05:35:23,360 --> 05:35:28,000 have exhausted the last element as well 8623 05:35:25,760 --> 05:35:31,040 and we were not able to find this 50 8624 05:35:28,000 --> 05:35:32,000 right let's suppose this is 10 20 30 and 8625 05:35:31,040 --> 05:35:34,558 40. 8626 05:35:32,000 --> 05:35:36,638 now this 50 is not present in the entire 8627 05:35:34,558 --> 05:35:40,400 area at that time what we are going to 8628 05:35:36,638 --> 05:35:42,638 return is minus 1 so we will say that 8629 05:35:40,400 --> 05:35:45,040 okay we will not able to find this 8630 05:35:42,638 --> 05:35:48,160 element whenever we are returning this 8631 05:35:45,040 --> 05:35:50,400 minus 1 in this area and this minus 1 8632 05:35:48,160 --> 05:35:51,600 indicates that we will not able to find 8633 05:35:50,400 --> 05:35:54,080 that element 8634 05:35:51,600 --> 05:35:55,120 now we shall see how to implement linear 8635 05:35:54,080 --> 05:35:58,638 search 8636 05:35:55,120 --> 05:36:01,280 in python so linear search will work 8637 05:35:58,638 --> 05:36:03,280 with the help of an array here so what 8638 05:36:01,280 --> 05:36:05,360 we are doing is we are searching one 8639 05:36:03,280 --> 05:36:08,320 single element in throughout an array in 8640 05:36:05,360 --> 05:36:10,958 sequential manner so this is how linear 8641 05:36:08,320 --> 05:36:14,080 search will work so here if you could 8642 05:36:10,958 --> 05:36:17,120 see we have array we have number which 8643 05:36:14,080 --> 05:36:19,840 one you have to search for and you have 8644 05:36:17,120 --> 05:36:23,600 the starting position variable so in 8645 05:36:19,840 --> 05:36:25,520 iterations array will move on and on by 8646 05:36:23,600 --> 05:36:27,120 searching from one place to another 8647 05:36:25,520 --> 05:36:30,480 place the first place to second second 8648 05:36:27,120 --> 05:36:32,240 to third and so on in total we have five 8649 05:36:30,480 --> 05:36:34,000 different elements in an array that 8650 05:36:32,240 --> 05:36:36,798 means four different places because 8651 05:36:34,000 --> 05:36:39,120 array starts from zero zero one two 8652 05:36:36,798 --> 05:36:40,718 three four so index is of four and the 8653 05:36:39,120 --> 05:36:42,160 elements are of five 8654 05:36:40,718 --> 05:36:44,958 so 8655 05:36:42,160 --> 05:36:47,200 we have to first take the key search 8656 05:36:44,958 --> 05:36:49,040 element and we have to compare that 8657 05:36:47,200 --> 05:36:51,920 particular element 8658 05:36:49,040 --> 05:36:55,200 to all the elements inside the array 8659 05:36:51,920 --> 05:36:57,440 right so if it is not matching the array 8660 05:36:55,200 --> 05:36:59,280 is not matching with the number you are 8661 05:36:57,440 --> 05:37:01,840 searching it will throw up an error 8662 05:36:59,280 --> 05:37:04,958 called element not found if it is found 8663 05:37:01,840 --> 05:37:07,600 it will show you index value where it is 8664 05:37:04,958 --> 05:37:10,638 which place of an array it is there so 8665 05:37:07,600 --> 05:37:14,400 let me quickly run the program for you 8666 05:37:10,638 --> 05:37:18,080 so i'm trying to search the element 8667 05:37:14,400 --> 05:37:22,798 1 right so the element 1 is in index 8668 05:37:18,080 --> 05:37:26,400 position 3 0 1 2 and 3 so x is the 8669 05:37:22,798 --> 05:37:28,958 variable which is used in order to find 8670 05:37:26,400 --> 05:37:32,000 which number it it will just act as a 8671 05:37:28,958 --> 05:37:35,360 key x will act as a key you can change 8672 05:37:32,000 --> 05:37:37,760 this and check if you want to search for 8673 05:37:35,360 --> 05:37:40,638 it for example it is not at all there in 8674 05:37:37,760 --> 05:37:42,958 the array so it will say element not 8675 05:37:40,638 --> 05:37:45,280 found if you want to search 2 for 8676 05:37:42,958 --> 05:37:46,958 example the answer should be 0 right 8677 05:37:45,280 --> 05:37:50,160 let's check 8678 05:37:46,958 --> 05:37:52,000 right so index value is 0 it is situated 8679 05:37:50,160 --> 05:37:54,400 in the first place of an array if you 8680 05:37:52,000 --> 05:37:56,000 want to search for 9 it is the last 8681 05:37:54,400 --> 05:38:00,400 place 8682 05:37:56,000 --> 05:38:02,638 so it's a 4 right so this is how the 8683 05:38:00,400 --> 05:38:05,280 value which you want to search 8684 05:38:02,638 --> 05:38:07,920 is always compared with all the elements 8685 05:38:05,280 --> 05:38:09,840 sequentially one after the other so for 8686 05:38:07,920 --> 05:38:11,600 example nine is compared with two it's 8687 05:38:09,840 --> 05:38:14,080 not matching then it will go to the next 8688 05:38:11,600 --> 05:38:16,160 one nine is compared with four it is not 8689 05:38:14,080 --> 05:38:18,638 matching and zero again compared with 8690 05:38:16,160 --> 05:38:21,200 one not matching it will go to the ninth 8691 05:38:18,638 --> 05:38:23,840 ninth place where it is situated right 8692 05:38:21,200 --> 05:38:26,240 it will compare the elements 8693 05:38:23,840 --> 05:38:28,798 sequentially so this is about linear 8694 05:38:26,240 --> 05:38:32,320 search in python now let's talk about 8695 05:38:28,798 --> 05:38:34,400 the time complexity of linear search 8696 05:38:32,320 --> 05:38:37,440 now if you observe carefully let's try 8697 05:38:34,400 --> 05:38:39,520 to understand this best case right 8698 05:38:37,440 --> 05:38:42,240 so now if you are looking for the 8699 05:38:39,520 --> 05:38:44,958 element and let's suppose these are the 8700 05:38:42,240 --> 05:38:46,558 elements in the array now let's suppose 8701 05:38:44,958 --> 05:38:48,638 in best case what can happen you're 8702 05:38:46,558 --> 05:38:50,798 looking for 10 and 10 is the first 8703 05:38:48,638 --> 05:38:52,558 element in the array now how many 8704 05:38:50,798 --> 05:38:54,638 iterations did you require did it 8705 05:38:52,558 --> 05:38:56,718 require to find you the 10 8706 05:38:54,638 --> 05:38:58,958 none right the constant time right only 8707 05:38:56,718 --> 05:39:01,280 one single unit operation was done and 8708 05:38:58,958 --> 05:39:04,000 you were able to find this 10. so this 8709 05:39:01,280 --> 05:39:06,160 is the best case time complexity where 8710 05:39:04,000 --> 05:39:08,400 the element that you're trying to find 8711 05:39:06,160 --> 05:39:10,000 is the first element that you search 8712 05:39:08,400 --> 05:39:11,760 right in this case you 8713 05:39:10,000 --> 05:39:14,638 you're looking for 10 and 10 is the 8714 05:39:11,760 --> 05:39:18,798 first element so this is your best case 8715 05:39:14,638 --> 05:39:21,040 now what about the average case and the 8716 05:39:18,798 --> 05:39:23,200 worst case now let's suppose average 8717 05:39:21,040 --> 05:39:26,240 cases that you were looking for an 8718 05:39:23,200 --> 05:39:28,400 element which is at the middle point 8719 05:39:26,240 --> 05:39:29,600 right let me just 8720 05:39:28,400 --> 05:39:31,760 zero one 8721 05:39:29,600 --> 05:39:33,840 two three four now 8722 05:39:31,760 --> 05:39:36,638 or you can put another another and 8723 05:39:33,840 --> 05:39:38,878 looking for sixty now in this case or 8724 05:39:36,638 --> 05:39:42,320 you can just skip it okay 8725 05:39:38,878 --> 05:39:42,320 no need to add one more 8726 05:39:42,638 --> 05:39:47,200 okay so now you're looking for an 8727 05:39:44,480 --> 05:39:50,000 element which is somewhere around 8728 05:39:47,200 --> 05:39:51,120 in the middle okay in this case you're 8729 05:39:50,000 --> 05:39:53,760 looking for 8730 05:39:51,120 --> 05:39:55,840 30 okay so now if you observe you are 8731 05:39:53,760 --> 05:39:58,878 only i trading half of the elements that 8732 05:39:55,840 --> 05:40:01,040 is 5 by 2 which is nothing but n by 2 8733 05:39:58,878 --> 05:40:02,558 since constants doesn't play in the do 8734 05:40:01,040 --> 05:40:04,798 not play any role when you're talking 8735 05:40:02,558 --> 05:40:08,400 about time complexity that is why 8736 05:40:04,798 --> 05:40:10,878 average k still boils down to big o of n 8737 05:40:08,400 --> 05:40:13,280 now whatever what happens in worst case 8738 05:40:10,878 --> 05:40:15,440 you're looking for an element that is 50 8739 05:40:13,280 --> 05:40:17,840 and which is present at the end of the 8740 05:40:15,440 --> 05:40:19,520 array or in the worst case you're 8741 05:40:17,840 --> 05:40:21,600 looking for something that is not 8742 05:40:19,520 --> 05:40:23,600 present in the array that is 60 let's 8743 05:40:21,600 --> 05:40:26,558 suppose and in that case you will still 8744 05:40:23,600 --> 05:40:29,680 iterate through the entire area and that 8745 05:40:26,558 --> 05:40:32,160 is why the worst case time complexity in 8746 05:40:29,680 --> 05:40:33,760 that case will be below and because you 8747 05:40:32,160 --> 05:40:36,240 are iterating 8748 05:40:33,760 --> 05:40:38,160 through the entire array and that 8749 05:40:36,240 --> 05:40:40,558 element was not found you are looking 8750 05:40:38,160 --> 05:40:42,000 for 60 and that is not present there so 8751 05:40:40,558 --> 05:40:44,080 you're right reading through the entire 8752 05:40:42,000 --> 05:40:46,638 area that is n 8753 05:40:44,080 --> 05:40:49,440 operations are done so that is why it 8754 05:40:46,638 --> 05:40:51,600 boils down to big o of n 8755 05:40:49,440 --> 05:40:56,080 now let's talk about the space 8756 05:40:51,600 --> 05:40:58,320 complexity of linear search 8757 05:40:56,080 --> 05:41:01,680 when we are trying to find the element 8758 05:40:58,320 --> 05:41:05,200 in the area that is 10 20 30 and 40 and 8759 05:41:01,680 --> 05:41:07,120 50. we were not using any extra memory 8760 05:41:05,200 --> 05:41:09,440 right we're not using any auxiliary 8761 05:41:07,120 --> 05:41:12,000 memory 8762 05:41:09,440 --> 05:41:13,840 or extra memory in order to find that 8763 05:41:12,000 --> 05:41:17,120 element we were just 8764 05:41:13,840 --> 05:41:19,840 looping around these elements one by one 8765 05:41:17,120 --> 05:41:22,000 and we were doing it on this particular 8766 05:41:19,840 --> 05:41:23,280 on the same array that we were 8767 05:41:22,000 --> 05:41:25,200 given right 8768 05:41:23,280 --> 05:41:28,000 since we are not using any auxiliary 8769 05:41:25,200 --> 05:41:29,520 memory that can be in the form of what a 8770 05:41:28,000 --> 05:41:32,798 stack 8771 05:41:29,520 --> 05:41:35,520 a linked list or an array or a string or 8772 05:41:32,798 --> 05:41:38,400 a queue we are not using these auxiliary 8773 05:41:35,520 --> 05:41:40,558 memories because they don't they are not 8774 05:41:38,400 --> 05:41:42,558 required right we are searching for an 8775 05:41:40,558 --> 05:41:44,400 element in this particular area that 8776 05:41:42,558 --> 05:41:45,920 we've given to us that was given to us 8777 05:41:44,400 --> 05:41:48,480 right so 8778 05:41:45,920 --> 05:41:51,520 the space complexity of 8779 05:41:48,480 --> 05:41:53,360 linear search is constant right we will 8780 05:41:51,520 --> 05:41:56,480 find we were able to find it in a 8781 05:41:53,360 --> 05:41:58,958 constant amount of space okay we are not 8782 05:41:56,480 --> 05:42:01,120 using any extra space 8783 05:41:58,958 --> 05:42:04,320 now let's try to understand binary 8784 05:42:01,120 --> 05:42:04,320 search algorithm 8785 05:42:04,878 --> 05:42:09,840 what is binary search 8786 05:42:07,760 --> 05:42:12,718 so binary search is one of the searching 8787 05:42:09,840 --> 05:42:15,120 techniques right like we saw in linear 8788 05:42:12,718 --> 05:42:18,718 search wherein the time complexity of 8789 05:42:15,120 --> 05:42:21,280 linear search was before n right we were 8790 05:42:18,718 --> 05:42:24,160 iterating through all the elements and 8791 05:42:21,280 --> 05:42:27,600 now this is a much more efficient 8792 05:42:24,160 --> 05:42:30,080 algorithm as compared to linear search 8793 05:42:27,600 --> 05:42:32,480 now again why do we need searching is 8794 05:42:30,080 --> 05:42:35,040 the thing that let's suppose if you have 8795 05:42:32,480 --> 05:42:37,840 a given set of elements and you want to 8796 05:42:35,040 --> 05:42:39,840 search if that element is present in 8797 05:42:37,840 --> 05:42:41,200 your array or not 8798 05:42:39,840 --> 05:42:44,160 that time 8799 05:42:41,200 --> 05:42:46,878 right we can use either linear search or 8800 05:42:44,160 --> 05:42:50,160 binary search now binary search is much 8801 05:42:46,878 --> 05:42:53,920 more efficient and it is used on a 8802 05:42:50,160 --> 05:42:57,600 sorted array or it can be used on an 8803 05:42:53,920 --> 05:43:00,400 array wherein some order is maintained 8804 05:42:57,600 --> 05:43:03,520 because based on that order we will 8805 05:43:00,400 --> 05:43:05,280 divide our array right it is a searching 8806 05:43:03,520 --> 05:43:06,558 algorithm which 8807 05:43:05,280 --> 05:43:09,920 is 8808 05:43:06,558 --> 05:43:12,798 or which follows the divide and conquer 8809 05:43:09,920 --> 05:43:14,558 strategy right let's suppose this is our 8810 05:43:12,798 --> 05:43:17,920 array and now 8811 05:43:14,558 --> 05:43:19,440 since it will be divided in such a way 8812 05:43:17,920 --> 05:43:22,240 that we can 8813 05:43:19,440 --> 05:43:24,718 neglect one part of it right we will be 8814 05:43:22,240 --> 05:43:26,878 dividing and then conquering that means 8815 05:43:24,718 --> 05:43:29,520 we will be then searching for our 8816 05:43:26,878 --> 05:43:32,160 element now let's suppose we are looking 8817 05:43:29,520 --> 05:43:34,638 for something that is now let's take an 8818 05:43:32,160 --> 05:43:35,840 example where in this array 8819 05:43:34,638 --> 05:43:38,400 is 8820 05:43:35,840 --> 05:43:41,520 written or the elements contained in 8821 05:43:38,400 --> 05:43:44,798 this array are in such a way that we if 8822 05:43:41,520 --> 05:43:46,638 we skip this part or the 8823 05:43:44,798 --> 05:43:49,120 left hand side will be skipped or the 8824 05:43:46,638 --> 05:43:51,440 right hand sides can be skipped in such 8825 05:43:49,120 --> 05:43:55,120 a way that they don't 8826 05:43:51,440 --> 05:43:57,920 affect our output so every time in 8827 05:43:55,120 --> 05:44:00,638 linear search our search space is 8828 05:43:57,920 --> 05:44:02,320 reduced unless and until we find that 8829 05:44:00,638 --> 05:44:05,440 element or 8830 05:44:02,320 --> 05:44:09,040 the array is exhausted okay so our 8831 05:44:05,440 --> 05:44:12,480 search space is reduced to half in every 8832 05:44:09,040 --> 05:44:15,840 iteration so this is what a binary 8833 05:44:12,480 --> 05:44:20,000 search is we look for an element in such 8834 05:44:15,840 --> 05:44:22,958 a way that every time we are neglecting 8835 05:44:20,000 --> 05:44:25,440 half portion of the array and let's take 8836 05:44:22,958 --> 05:44:27,440 an example when we have this entire area 8837 05:44:25,440 --> 05:44:28,958 so first half that means if four 8838 05:44:27,440 --> 05:44:30,878 elements eight if there are eight 8839 05:44:28,958 --> 05:44:33,440 elements four on this side four on this 8840 05:44:30,878 --> 05:44:35,200 side these four will be neglected then 8841 05:44:33,440 --> 05:44:37,360 we have two on this side two on this 8842 05:44:35,200 --> 05:44:39,600 side these two will be neglected then 8843 05:44:37,360 --> 05:44:42,080 one on this side one on this side then 8844 05:44:39,600 --> 05:44:44,400 this one will be neglected and tilt 8845 05:44:42,080 --> 05:44:47,360 unless and until we find that element or 8846 05:44:44,400 --> 05:44:51,840 the entire array is exhausted right so 8847 05:44:47,360 --> 05:44:53,920 this is how your binary search works 8848 05:44:51,840 --> 05:44:56,240 now let's try to understand binary 8849 05:44:53,920 --> 05:44:58,558 search algorithm so first we are going 8850 05:44:56,240 --> 05:45:00,000 to understand the iterative approach and 8851 05:44:58,558 --> 05:45:02,798 then we are going to understand the 8852 05:45:00,000 --> 05:45:05,760 recursive approach so i try to as the 8853 05:45:02,798 --> 05:45:08,400 name suggests we are going to use 8854 05:45:05,760 --> 05:45:10,558 for loops right we will start with a for 8855 05:45:08,400 --> 05:45:13,440 loop and it will 8856 05:45:10,558 --> 05:45:16,160 iterate and we will iterate unless until 8857 05:45:13,440 --> 05:45:18,718 the beginning is less than 8858 05:45:16,160 --> 05:45:21,200 the end right so because we will be 8859 05:45:18,718 --> 05:45:23,600 updating our both beginning in some 8860 05:45:21,200 --> 05:45:24,400 cases and in some cases we'll update our 8861 05:45:23,600 --> 05:45:27,920 end 8862 05:45:24,400 --> 05:45:30,798 now what happens now since we know that 8863 05:45:27,920 --> 05:45:32,638 in this iterative approach or in this 8864 05:45:30,798 --> 05:45:34,400 binary search it doesn't depend whether 8865 05:45:32,638 --> 05:45:36,558 we are using iterative approach or 8866 05:45:34,400 --> 05:45:38,958 recursive approach the logic will remain 8867 05:45:36,558 --> 05:45:41,360 same right so we will be having this 8868 05:45:38,958 --> 05:45:43,360 array and it will be in some order so 8869 05:45:41,360 --> 05:45:45,920 that we can neglect some part of it it 8870 05:45:43,360 --> 05:45:48,400 doesn't have to be sorted always we can 8871 05:45:45,920 --> 05:45:51,680 still apply binary search even if the 8872 05:45:48,400 --> 05:45:53,360 array is not sorted but still some order 8873 05:45:51,680 --> 05:45:55,680 is there so that we can neglect some 8874 05:45:53,360 --> 05:45:58,080 part of it because again keeping this 8875 05:45:55,680 --> 05:46:02,000 thing in mind that it follows the 8876 05:45:58,080 --> 05:46:04,798 paradigm of divide and conquer so now we 8877 05:46:02,000 --> 05:46:06,000 have this beginning and end at place and 8878 05:46:04,798 --> 05:46:08,718 we 8879 05:46:06,000 --> 05:46:10,638 we will always iterate when beginning is 8880 05:46:08,718 --> 05:46:13,200 less than end right 8881 05:46:10,638 --> 05:46:15,760 so now what happens after that now we 8882 05:46:13,200 --> 05:46:16,840 have this array and now what we will be 8883 05:46:15,760 --> 05:46:18,878 what we will 8884 05:46:16,840 --> 05:46:21,200 do let me take 8885 05:46:18,878 --> 05:46:22,958 new pointers so this is your beginning 8886 05:46:21,200 --> 05:46:25,360 and this is your end so this is your 8887 05:46:22,958 --> 05:46:28,718 beginning and this is your end now you 8888 05:46:25,360 --> 05:46:31,600 will be taking a new middle index right 8889 05:46:28,718 --> 05:46:34,638 and let's call it m and now let's name 8890 05:46:31,600 --> 05:46:36,878 these things 0 1 2 3 4 so you will do 8891 05:46:34,638 --> 05:46:39,280 what beginning plus 8892 05:46:36,878 --> 05:46:42,160 n divided by 2 so that some part of it 8893 05:46:39,280 --> 05:46:44,958 can be neglected right so it will be 2 8894 05:46:42,160 --> 05:46:47,200 so your mid is at this position now you 8895 05:46:44,958 --> 05:46:48,958 will see okay the element that i'm 8896 05:46:47,200 --> 05:46:51,120 looking for is 8897 05:46:48,958 --> 05:46:53,840 uh let's suppose is 50 8898 05:46:51,120 --> 05:46:57,280 and the element that i am currently at 8899 05:46:53,840 --> 05:46:59,280 is 30 so obviously it will be never from 8900 05:46:57,280 --> 05:47:01,840 this side there is no chance that we 8901 05:46:59,280 --> 05:47:04,240 will be able to find 50 from this side 8902 05:47:01,840 --> 05:47:06,558 that is the left hand side right 8903 05:47:04,240 --> 05:47:08,878 first we will check okay is this 30 8904 05:47:06,558 --> 05:47:12,080 equal to 50 no so this will never be 8905 05:47:08,878 --> 05:47:15,200 executed right then we'll check is my 8906 05:47:12,080 --> 05:47:18,000 item that is there is this 30 8907 05:47:15,200 --> 05:47:20,320 less than or greater than 50 if it is 8908 05:47:18,000 --> 05:47:23,360 greater than 50 right 8909 05:47:20,320 --> 05:47:26,638 if it is if the element that is 8910 05:47:23,360 --> 05:47:28,400 if item that is 50 is greater than 30 8911 05:47:26,638 --> 05:47:30,638 right which is in this case 8912 05:47:28,400 --> 05:47:33,040 our 50 that is the element that we are 8913 05:47:30,638 --> 05:47:35,520 looking for the item item is this point 8914 05:47:33,040 --> 05:47:38,320 is greater than 30 so there is no point 8915 05:47:35,520 --> 05:47:41,520 that it will be on this side so we will 8916 05:47:38,320 --> 05:47:44,638 skip or we will neglect this half 8917 05:47:41,520 --> 05:47:47,840 portion of the array so that is why our 8918 05:47:44,638 --> 05:47:50,718 new beginning is updated it will be new 8919 05:47:47,840 --> 05:47:53,200 it will be middle index plus one that is 8920 05:47:50,718 --> 05:47:55,920 middle index was two plus one that is 8921 05:47:53,200 --> 05:47:59,840 this will be our new beginning so we 8922 05:47:55,920 --> 05:48:02,080 have smartly skipped the half portion of 8923 05:47:59,840 --> 05:48:04,320 the array so now let's 8924 05:48:02,080 --> 05:48:06,558 drop all of these things and now let's 8925 05:48:04,320 --> 05:48:08,400 see what happens in the 8926 05:48:06,558 --> 05:48:10,878 next iteration 8927 05:48:08,400 --> 05:48:13,120 now we'll keep this thing in mind that 8928 05:48:10,878 --> 05:48:15,440 we are not looping or we are not 8929 05:48:13,120 --> 05:48:16,878 exceeding this limit that is beginning 8930 05:48:15,440 --> 05:48:18,160 should be always 8931 05:48:16,878 --> 05:48:20,160 less than 8932 05:48:18,160 --> 05:48:22,798 or it should be always less than or 8933 05:48:20,160 --> 05:48:25,200 equal to n okay so this this condition 8934 05:48:22,798 --> 05:48:26,160 should be maintained and similarly we'll 8935 05:48:25,200 --> 05:48:28,878 again 8936 05:48:26,160 --> 05:48:31,200 divide our array and then look for the 8937 05:48:28,878 --> 05:48:33,600 same things right first we'll look for 8938 05:48:31,200 --> 05:48:37,600 the element then we'll skip some part of 8939 05:48:33,600 --> 05:48:40,240 it so this is the iterative approach for 8940 05:48:37,600 --> 05:48:42,240 binary search right now let's look at 8941 05:48:40,240 --> 05:48:44,798 the recursive approach or recursive 8942 05:48:42,240 --> 05:48:46,480 algorithm for the same so again 8943 05:48:44,798 --> 05:48:48,798 beginning the ground rule will remain 8944 05:48:46,480 --> 05:48:51,360 same will always 8945 05:48:48,798 --> 05:48:54,000 i trade or we will always recursively 8946 05:48:51,360 --> 05:48:56,718 call binary search unless and until this 8947 05:48:54,000 --> 05:48:58,798 beginning is less than end done now what 8948 05:48:56,718 --> 05:49:00,798 happens we will again find middle index 8949 05:48:58,798 --> 05:49:03,120 that is beginning plus and 8950 05:49:00,798 --> 05:49:05,040 divided by 2 then we will look for the 8951 05:49:03,120 --> 05:49:07,520 element these three steps will remain 8952 05:49:05,040 --> 05:49:10,400 same even you if you're using recursive 8953 05:49:07,520 --> 05:49:13,440 approaches now what happens in recursive 8954 05:49:10,400 --> 05:49:15,280 recursion right we again call 8955 05:49:13,440 --> 05:49:17,920 the function again and again that is 8956 05:49:15,280 --> 05:49:20,480 what is recursion so in this 8957 05:49:17,920 --> 05:49:23,200 in this entire tutorial we will be 8958 05:49:20,480 --> 05:49:25,600 covering recursion as well but in the 8959 05:49:23,200 --> 05:49:27,840 later past part of the course here you 8960 05:49:25,600 --> 05:49:29,760 can get a good intuition or let me give 8961 05:49:27,840 --> 05:49:32,000 you a brief intuition about how 8962 05:49:29,760 --> 05:49:34,558 recursion works so let's suppose this is 8963 05:49:32,000 --> 05:49:35,920 your activation record every time when a 8964 05:49:34,558 --> 05:49:38,958 recursion 8965 05:49:35,920 --> 05:49:41,600 is there an activation record is called 8966 05:49:38,958 --> 05:49:43,600 so let's suppose you have these three 8967 05:49:41,600 --> 05:49:45,680 statements 8968 05:49:43,600 --> 05:49:49,520 and let's suppose in your algorithm you 8969 05:49:45,680 --> 05:49:51,840 have statement p1 p2 and p3 8970 05:49:49,520 --> 05:49:53,600 right and at p2 you are calling the 8971 05:49:51,840 --> 05:49:55,920 function again right you're calling this 8972 05:49:53,600 --> 05:49:57,680 function again so now what happens an 8973 05:49:55,920 --> 05:49:59,520 activation record is called he will 8974 05:49:57,680 --> 05:50:02,160 check okay is this statement executed 8975 05:49:59,520 --> 05:50:05,440 yes so one will be executed is second 8976 05:50:02,160 --> 05:50:07,840 executed yes so second is executed but 8977 05:50:05,440 --> 05:50:10,240 at second you are calling this function 8978 05:50:07,840 --> 05:50:13,120 again so at that time again a new 8979 05:50:10,240 --> 05:50:16,558 activation record is created now this 8980 05:50:13,120 --> 05:50:19,120 third step is left behind right now this 8981 05:50:16,558 --> 05:50:21,520 will be covered when we come back or 8982 05:50:19,120 --> 05:50:23,360 return from this function call that we 8983 05:50:21,520 --> 05:50:25,840 called here so let's suppose this 8984 05:50:23,360 --> 05:50:27,840 function was here now in here you are 8985 05:50:25,840 --> 05:50:29,200 returning right this function called 8986 05:50:27,840 --> 05:50:31,440 let's suppose this is not less than 8987 05:50:29,200 --> 05:50:34,320 beginning uh beginning is not less than 8988 05:50:31,440 --> 05:50:36,558 end so this will be some somewhat this 8989 05:50:34,320 --> 05:50:38,320 case is relatable right this is similar 8990 05:50:36,558 --> 05:50:40,878 to what we are looking for right let's 8991 05:50:38,320 --> 05:50:43,280 suppose there is some similar situation 8992 05:50:40,878 --> 05:50:45,120 wherein beginning is not less than and 8993 05:50:43,280 --> 05:50:47,120 at that time you will be returning from 8994 05:50:45,120 --> 05:50:49,280 this right now once you have returned 8995 05:50:47,120 --> 05:50:51,280 you will be then calling this function 8996 05:50:49,280 --> 05:50:53,040 again but this time around for this and 8997 05:50:51,280 --> 05:50:55,520 let's suppose this time around you will 8998 05:50:53,040 --> 05:50:57,520 you are calling this one is executed 8999 05:50:55,520 --> 05:50:59,360 step one is executed now again this 9000 05:50:57,520 --> 05:51:01,840 activation is record is called the 9001 05:50:59,360 --> 05:51:03,760 situation report is one and then again 9002 05:51:01,840 --> 05:51:06,400 this is called 9003 05:51:03,760 --> 05:51:07,920 right this is executed again this two is 9004 05:51:06,400 --> 05:51:09,920 called again a new activation record 9005 05:51:07,920 --> 05:51:12,480 will be created and these three three 9006 05:51:09,920 --> 05:51:14,958 steps the step third will be still left 9007 05:51:12,480 --> 05:51:17,200 for execution so now here you return 9008 05:51:14,958 --> 05:51:18,638 right then it will go back to this step 9009 05:51:17,200 --> 05:51:20,798 right 9010 05:51:18,638 --> 05:51:22,000 and then once you are done with this now 9011 05:51:20,798 --> 05:51:24,718 there are two 9012 05:51:22,000 --> 05:51:27,120 positions or two possible scenarios 9013 05:51:24,718 --> 05:51:29,360 where you can return either you are 9014 05:51:27,120 --> 05:51:31,440 returning from this function just like 9015 05:51:29,360 --> 05:51:34,240 we have executed this condition and we 9016 05:51:31,440 --> 05:51:36,160 return right another is that once you 9017 05:51:34,240 --> 05:51:38,558 are done with this entire activation 9018 05:51:36,160 --> 05:51:41,120 record at that time you will also return 9019 05:51:38,558 --> 05:51:42,878 okay so these are two scenarios now you 9020 05:51:41,120 --> 05:51:44,878 have executed this there is no step to 9021 05:51:42,878 --> 05:51:46,558 be executed it will return now this left 9022 05:51:44,878 --> 05:51:48,558 this was left behind this will be 9023 05:51:46,558 --> 05:51:50,638 executed now nothing is to be executed 9024 05:51:48,558 --> 05:51:52,080 it will go to the caller which was this 9025 05:51:50,638 --> 05:51:54,958 and finally it will go to the main 9026 05:51:52,080 --> 05:51:57,040 method where it we call this at the 9027 05:51:54,958 --> 05:51:59,440 first place this function okay so this 9028 05:51:57,040 --> 05:52:02,400 is how an activation record is created a 9029 05:51:59,440 --> 05:52:04,000 stack is maintained okay even if uh you 9030 05:52:02,400 --> 05:52:06,320 might be thinking we are not using any 9031 05:52:04,000 --> 05:52:08,718 extra space but whenever recursion is 9032 05:52:06,320 --> 05:52:11,520 there an extra space that is in the form 9033 05:52:08,718 --> 05:52:13,200 of stack that stack is maintained so you 9034 05:52:11,520 --> 05:52:14,718 need to keep this thing in mind while 9035 05:52:13,200 --> 05:52:16,958 you are playing around with space 9036 05:52:14,718 --> 05:52:19,360 complexity at the time when you're using 9037 05:52:16,958 --> 05:52:20,958 recursion okay so now with that being 9038 05:52:19,360 --> 05:52:23,120 said let's clear our screen and let's 9039 05:52:20,958 --> 05:52:26,000 see how recursion is called here so 9040 05:52:23,120 --> 05:52:27,360 again now recursively what we will doing 9041 05:52:26,000 --> 05:52:29,360 if 9042 05:52:27,360 --> 05:52:32,480 now we have this mid right and let's 9043 05:52:29,360 --> 05:52:34,400 take an example of an array 9044 05:52:32,480 --> 05:52:39,920 0 1 2 9045 05:52:34,400 --> 05:52:41,120 3 4 10 20 30 40 and 50. so middle index 9046 05:52:39,920 --> 05:52:43,520 will be 9047 05:52:41,120 --> 05:52:45,280 0 plus 4 divided by 2 that is 2 so this 9048 05:52:43,520 --> 05:52:47,760 is our middle index right 9049 05:52:45,280 --> 05:52:50,320 so this is 0 this is our beginning and 9050 05:52:47,760 --> 05:52:52,798 this is our end right so now we are hit 9051 05:52:50,320 --> 05:52:54,400 here right so we again check the 50 that 9052 05:52:52,798 --> 05:52:57,520 we are looking this is our item that we 9053 05:52:54,400 --> 05:53:00,480 are looking for okay so is 50 9054 05:52:57,520 --> 05:53:03,040 and this is our middle index is 30 9055 05:53:00,480 --> 05:53:05,360 greater than 50 no it is not in this 9056 05:53:03,040 --> 05:53:08,320 case so this will never be executed this 9057 05:53:05,360 --> 05:53:10,320 is not executed as well right 9058 05:53:08,320 --> 05:53:12,798 now what about this condition the else 9059 05:53:10,320 --> 05:53:15,600 part now what we will be doing will be 9060 05:53:12,798 --> 05:53:17,760 skipping since this 30 is less than 9061 05:53:15,600 --> 05:53:20,080 we'll be skipping this part and we will 9062 05:53:17,760 --> 05:53:22,718 focus on middle index plus one which is 9063 05:53:20,080 --> 05:53:25,360 nothing but this so this will be our new 9064 05:53:22,718 --> 05:53:28,080 beginning and our process will start 9065 05:53:25,360 --> 05:53:30,320 moving right so now again then the same 9066 05:53:28,080 --> 05:53:31,680 thing will happen unless and until this 9067 05:53:30,320 --> 05:53:34,160 condition is 9068 05:53:31,680 --> 05:53:37,440 false okay so this is 9069 05:53:34,160 --> 05:53:39,360 how your binary search works when you're 9070 05:53:37,440 --> 05:53:41,600 using recursion so now let's try to 9071 05:53:39,360 --> 05:53:44,160 understand binary search and 9072 05:53:41,600 --> 05:53:46,480 let's see its demonstration okay 9073 05:53:44,160 --> 05:53:51,440 so we are looking for 20 and this is our 9074 05:53:46,480 --> 05:53:53,840 array right 10 11 16 20 and 23. now this 9075 05:53:51,440 --> 05:53:56,558 array is sorted right 9076 05:53:53,840 --> 05:53:59,120 so we can apply binary research okay 9077 05:53:56,558 --> 05:54:01,360 since we can neglect some part of the 9078 05:53:59,120 --> 05:54:03,760 array based on some conditions okay so 9079 05:54:01,360 --> 05:54:05,920 now our beginning in the first iteration 9080 05:54:03,760 --> 05:54:07,520 what is happening our beginning is 0 our 9081 05:54:05,920 --> 05:54:09,600 end is 4 and 9082 05:54:07,520 --> 05:54:11,360 our middle is this element now what 9083 05:54:09,600 --> 05:54:15,280 we're looking for 9084 05:54:11,360 --> 05:54:16,000 is 16 equal to 20 no it is not 9085 05:54:15,280 --> 05:54:19,360 but 9086 05:54:16,000 --> 05:54:20,160 16 is less than 20 so we will skip this 9087 05:54:19,360 --> 05:54:22,558 part 9088 05:54:20,160 --> 05:54:25,040 in the next iteration what happens we'll 9089 05:54:22,558 --> 05:54:26,558 be focusing on these three elements 9090 05:54:25,040 --> 05:54:28,798 right that is 2 9091 05:54:26,558 --> 05:54:31,120 6 from we will be focusing on this part 9092 05:54:28,798 --> 05:54:32,798 rather if we say we'll focus on this 9093 05:54:31,120 --> 05:54:34,958 part 9094 05:54:32,798 --> 05:54:36,878 right 9095 05:54:34,958 --> 05:54:38,798 focus on this part okay so now in the 9096 05:54:36,878 --> 05:54:40,878 second iteration what will be happening 9097 05:54:38,798 --> 05:54:42,878 our beginning is updated now our new 9098 05:54:40,878 --> 05:54:45,120 beginning is this point our end will 9099 05:54:42,878 --> 05:54:48,558 remain at its own position now we'll 9100 05:54:45,120 --> 05:54:51,680 find the middle index so it will be 4 9101 05:54:48,558 --> 05:54:54,718 plus 3 that is 3 that is 7 right and 9102 05:54:51,680 --> 05:54:56,558 divided by 2 it is 3.5 right so since 9103 05:54:54,718 --> 05:54:59,600 this will be truncated right the 9104 05:54:56,558 --> 05:55:01,360 truncation will happen and the integer 9105 05:54:59,600 --> 05:55:03,600 that is there the middle index will be 9106 05:55:01,360 --> 05:55:06,480 three so this is our middle index right 9107 05:55:03,600 --> 05:55:08,878 so you can see middle index is three now 9108 05:55:06,480 --> 05:55:12,400 is this element that we're looking for 9109 05:55:08,878 --> 05:55:15,200 yes so we'll return the index so we 9110 05:55:12,400 --> 05:55:18,000 found our element at index 3 and hence 9111 05:55:15,200 --> 05:55:21,280 we return 3 because if you observe 9112 05:55:18,000 --> 05:55:23,680 carefully it is returning if the element 9113 05:55:21,280 --> 05:55:27,200 is found it is returning the index so 9114 05:55:23,680 --> 05:55:27,200 this will be returned okay 9115 05:55:27,680 --> 05:55:33,680 so this is how binary search works 9116 05:55:31,440 --> 05:55:36,480 after knowing what does binary search 9117 05:55:33,680 --> 05:55:39,520 will implement the same in python 9118 05:55:36,480 --> 05:55:42,000 quickly switching up to the ide so 9119 05:55:39,520 --> 05:55:44,000 the binary search has 9120 05:55:42,000 --> 05:55:46,480 four different elements and important 9121 05:55:44,000 --> 05:55:48,240 the first one is array the second one is 9122 05:55:46,480 --> 05:55:50,160 which is the element to search for which 9123 05:55:48,240 --> 05:55:52,480 is stored in x and 9124 05:55:50,160 --> 05:55:54,958 low and high why because 9125 05:55:52,480 --> 05:55:57,280 every array in order to have the binary 9126 05:55:54,958 --> 05:55:58,798 search will be divided into two parts 9127 05:55:57,280 --> 05:56:01,360 right it will 9128 05:55:58,798 --> 05:56:03,520 go accordingly if the key that means 9129 05:56:01,360 --> 05:56:05,600 whatever the element you are searching 9130 05:56:03,520 --> 05:56:08,958 is matching the middle element it will 9131 05:56:05,600 --> 05:56:11,760 exit the binary search immediately if 9132 05:56:08,958 --> 05:56:14,798 not it will try to proceed with the 9133 05:56:11,760 --> 05:56:17,520 search of that particular element in 9134 05:56:14,798 --> 05:56:19,600 halves of the array like it will divide 9135 05:56:17,520 --> 05:56:21,600 arrange sub arrays the right and left 9136 05:56:19,600 --> 05:56:23,760 part it will try to 9137 05:56:21,600 --> 05:56:26,320 see and search for that element 9138 05:56:23,760 --> 05:56:29,760 accordingly as per the 9139 05:56:26,320 --> 05:56:31,520 key element is right so mid is equal to 9140 05:56:29,760 --> 05:56:34,958 low plus 9141 05:56:31,520 --> 05:56:37,600 high minus low by 2 so this is the basic 9142 05:56:34,958 --> 05:56:40,320 formula which will be using in order to 9143 05:56:37,600 --> 05:56:42,480 split the binary array 9144 05:56:40,320 --> 05:56:44,878 in order to have the search 9145 05:56:42,480 --> 05:56:47,360 right so 9146 05:56:44,878 --> 05:56:49,120 if array of middle that means middle 9147 05:56:47,360 --> 05:56:51,440 element is 9148 05:56:49,120 --> 05:56:53,120 equal to equal to that means it is equal 9149 05:56:51,440 --> 05:56:55,760 to the key element which you are 9150 05:56:53,120 --> 05:56:58,240 searching then it will immediately 9151 05:56:55,760 --> 05:57:01,280 give you the middle element as the 9152 05:56:58,240 --> 05:57:04,400 searched element so if else what happens 9153 05:57:01,280 --> 05:57:07,200 if the middle element is lesser than x 9154 05:57:04,400 --> 05:57:09,600 what it will do it will go to the right 9155 05:57:07,200 --> 05:57:12,000 side of an array if it is greater than x 9156 05:57:09,600 --> 05:57:15,680 it will go to the left side of an array 9157 05:57:12,000 --> 05:57:17,280 right so it will try to search in half 9158 05:57:15,680 --> 05:57:19,200 like sub arrays 9159 05:57:17,280 --> 05:57:21,200 here if you could see the array that is 9160 05:57:19,200 --> 05:57:22,558 3 4 5 6 9161 05:57:21,200 --> 05:57:24,638 7 and 9162 05:57:22,558 --> 05:57:26,798 8 and 9 you have all these elements 9163 05:57:24,638 --> 05:57:30,240 inside the array what you have to search 9164 05:57:26,798 --> 05:57:32,718 is 4 so 4 is the second element 9165 05:57:30,240 --> 05:57:35,280 immediately you can see but accordingly 9166 05:57:32,718 --> 05:57:38,080 you have to search as per the binary 9167 05:57:35,280 --> 05:57:39,200 search rules what it will do it will 9168 05:57:38,080 --> 05:57:41,120 first 9169 05:57:39,200 --> 05:57:43,680 cut this particular 9170 05:57:41,120 --> 05:57:46,638 array into two halves by using this 9171 05:57:43,680 --> 05:57:48,878 formula and then it will compare the key 9172 05:57:46,638 --> 05:57:50,080 element which you are trying to search 9173 05:57:48,878 --> 05:57:52,240 with the 9174 05:57:50,080 --> 05:57:54,958 elements which is already present in an 9175 05:57:52,240 --> 05:57:58,280 array in order to find so let me quickly 9176 05:57:54,958 --> 05:57:58,280 run this 9177 05:58:00,080 --> 05:58:05,520 okay it is telling the element which you 9178 05:58:02,798 --> 05:58:08,878 are searching is present in index 9179 05:58:05,520 --> 05:58:12,000 number one that means it is having the 9180 05:58:08,878 --> 05:58:16,160 count of array index not on the element 9181 05:58:12,000 --> 05:58:19,520 so 0 1 2 3 and so on so 4 is present in 9182 05:58:16,160 --> 05:58:22,160 index value 1 so this is how binary 9183 05:58:19,520 --> 05:58:24,878 search will work in python now let's 9184 05:58:22,160 --> 05:58:27,520 talk about the time complexity of binary 9185 05:58:24,878 --> 05:58:29,440 search now in the best case now what is 9186 05:58:27,520 --> 05:58:32,558 the best case now let's take an array 9187 05:58:29,440 --> 05:58:34,958 and in that array one two three four and 9188 05:58:32,558 --> 05:58:36,320 five these are the elements now the best 9189 05:58:34,958 --> 05:58:38,798 case is that 9190 05:58:36,320 --> 05:58:40,718 not that if the element like we saw in 9191 05:58:38,798 --> 05:58:42,240 linear search that this element when we 9192 05:58:40,718 --> 05:58:45,280 are looking to search for this same 9193 05:58:42,240 --> 05:58:47,200 element at that time that was the 9194 05:58:45,280 --> 05:58:50,400 best case scenario for linear search 9195 05:58:47,200 --> 05:58:53,680 right but in this binary search the best 9196 05:58:50,400 --> 05:58:56,638 case scenario is when your middle index 9197 05:58:53,680 --> 05:58:59,760 is at the at this location and you are 9198 05:58:56,638 --> 05:59:02,480 looking for you are searching three 9199 05:58:59,760 --> 05:59:04,558 in the entire area so at that time this 9200 05:59:02,480 --> 05:59:07,200 will take a constant amount of time and 9201 05:59:04,558 --> 05:59:09,840 this is the best case time complexity in 9202 05:59:07,200 --> 05:59:11,920 that case okay now in average case what 9203 05:59:09,840 --> 05:59:13,600 happens right if you talk about this 9204 05:59:11,920 --> 05:59:14,718 algorithm let me just clean out my 9205 05:59:13,600 --> 05:59:17,200 screen 9206 05:59:14,718 --> 05:59:19,440 so it follows a paradigm of divide and 9207 05:59:17,200 --> 05:59:21,600 conquer so let's suppose you have eight 9208 05:59:19,440 --> 05:59:24,400 elements first in the array it will be 9209 05:59:21,600 --> 05:59:26,160 divided into four because these four or 9210 05:59:24,400 --> 05:59:28,558 either it can be on the left side or on 9211 05:59:26,160 --> 05:59:30,798 the right side will be neglected and 9212 05:59:28,558 --> 05:59:32,558 then we deal about these then we focus 9213 05:59:30,798 --> 05:59:34,080 on these things okay these four elements 9214 05:59:32,558 --> 05:59:36,878 again it will be divided into two and 9215 05:59:34,080 --> 05:59:38,798 two then we will then click two elements 9216 05:59:36,878 --> 05:59:41,280 then one and one then again there will 9217 05:59:38,798 --> 05:59:43,440 be one of the element can be neglected 9218 05:59:41,280 --> 05:59:45,760 so there are one and one so we focus on 9219 05:59:43,440 --> 05:59:47,840 this element so the entire operation 9220 05:59:45,760 --> 05:59:50,718 will be done or entire searching will be 9221 05:59:47,840 --> 05:59:54,160 done in three steps right now if you i 9222 05:59:50,718 --> 05:59:56,480 take an example and if i do a log 9223 05:59:54,160 --> 05:59:57,680 8 to the base 2 what should be the value 9224 05:59:56,480 --> 05:59:59,440 of this 9225 05:59:57,680 --> 06:00:01,840 obviously when i do this this can be 9226 05:59:59,440 --> 06:00:04,160 written as 2 raised power 3 right and 9227 06:00:01,840 --> 06:00:07,200 this can be written as 3 into log 2 to 9228 06:00:04,160 --> 06:00:08,798 the base 2 now this is 1 and now you get 9229 06:00:07,200 --> 06:00:10,878 the answer as 3 9230 06:00:08,798 --> 06:00:12,798 so this 3 and this 3 9231 06:00:10,878 --> 06:00:15,360 are equal that means 9232 06:00:12,798 --> 06:00:17,680 if i talk about the worst case time 9233 06:00:15,360 --> 06:00:19,040 complexity of binary search it will be 9234 06:00:17,680 --> 06:00:20,718 somewhere around 9235 06:00:19,040 --> 06:00:23,200 log and 9236 06:00:20,718 --> 06:00:25,280 as it although as we saw in three steps 9237 06:00:23,200 --> 06:00:28,480 we were able to find the element and the 9238 06:00:25,280 --> 06:00:30,320 log n that means log 8 is the answer of 9239 06:00:28,480 --> 06:00:31,760 that is also 3 so you get the point 9240 06:00:30,320 --> 06:00:34,480 right so the worst case type of 9241 06:00:31,760 --> 06:00:37,200 complexity of binary search is 9242 06:00:34,480 --> 06:00:39,600 log n and same goes for the average case 9243 06:00:37,200 --> 06:00:42,480 wherein it will be somewhere around log 9244 06:00:39,600 --> 06:00:44,878 n divided by 2 neglecting log n divided 9245 06:00:42,480 --> 06:00:47,520 by 2 neglecting the constant terms it 9246 06:00:44,878 --> 06:00:50,000 will be again or it boils down though 9247 06:00:47,520 --> 06:00:52,638 they go off logging okay 9248 06:00:50,000 --> 06:00:54,878 now let's talk about space complexity of 9249 06:00:52,638 --> 06:00:58,878 binary search now when you talk about 9250 06:00:54,878 --> 06:01:01,840 space complexity right we only think of 9251 06:00:58,878 --> 06:01:04,080 auxiliary memories or you can say that 9252 06:01:01,840 --> 06:01:06,958 or you can see that what any extra 9253 06:01:04,080 --> 06:01:09,680 memory that you guys have used since we 9254 06:01:06,958 --> 06:01:12,000 did not use any extra memory that can be 9255 06:01:09,680 --> 06:01:14,240 in the form of array or it can be in the 9256 06:01:12,000 --> 06:01:16,718 form of stack or it can be in the form 9257 06:01:14,240 --> 06:01:19,920 of queue or linked list or even strings 9258 06:01:16,718 --> 06:01:23,920 right since we never use these extra 9259 06:01:19,920 --> 06:01:26,400 memories in our implementation so the 9260 06:01:23,920 --> 06:01:30,558 space complexity of 9261 06:01:26,400 --> 06:01:33,840 binary search is big o of one that is 9262 06:01:30,558 --> 06:01:36,160 it takes a constant amount of space 9263 06:01:33,840 --> 06:01:38,878 what is insertion sort 9264 06:01:36,160 --> 06:01:40,638 so the question is that what is sorting 9265 06:01:38,878 --> 06:01:42,958 so you might be thinking why do we need 9266 06:01:40,638 --> 06:01:46,080 these sorting algorithms 9267 06:01:42,958 --> 06:01:48,400 so if i told you that you have 9268 06:01:46,080 --> 06:01:50,240 a bunch of students right 9269 06:01:48,400 --> 06:01:52,000 you have a bunch of students and they 9270 06:01:50,240 --> 06:01:54,240 each have their role number 9271 06:01:52,000 --> 06:01:56,240 they are not present in 9272 06:01:54,240 --> 06:01:58,080 what in a numerical order or you can say 9273 06:01:56,240 --> 06:02:00,160 they are not present in some order i 9274 06:01:58,080 --> 06:02:03,280 want that order to be maintained let's 9275 06:02:00,160 --> 06:02:05,760 suppose you have one to ten students in 9276 06:02:03,280 --> 06:02:07,680 those bunch of students and each having 9277 06:02:05,760 --> 06:02:09,040 roll numbers from this range 9278 06:02:07,680 --> 06:02:11,600 from one to ten 9279 06:02:09,040 --> 06:02:12,400 now some of them are absent and some of 9280 06:02:11,600 --> 06:02:14,718 them 9281 06:02:12,400 --> 06:02:16,878 are some of the role numbers have left 9282 06:02:14,718 --> 06:02:18,160 the school but the roll numbers are not 9283 06:02:16,878 --> 06:02:20,000 changed yet 9284 06:02:18,160 --> 06:02:22,080 now what i told you i told you please 9285 06:02:20,000 --> 06:02:24,878 sort them or arrange them in such a 9286 06:02:22,080 --> 06:02:27,200 manner so that i can easily understand 9287 06:02:24,878 --> 06:02:29,280 which role number is after which either 9288 06:02:27,200 --> 06:02:31,840 in ascending or in descending order 9289 06:02:29,280 --> 06:02:34,160 suppose 1 is there 2 is there then 6 is 9290 06:02:31,840 --> 06:02:36,000 there then 8 is there then 10 is there 9291 06:02:34,160 --> 06:02:38,000 so rest of the rule numbers i can easily 9292 06:02:36,000 --> 06:02:40,558 depict ok these are the ones which 9293 06:02:38,000 --> 06:02:42,718 either are not there or 9294 06:02:40,558 --> 06:02:45,360 are absent 9295 06:02:42,718 --> 06:02:47,120 so in order to do so we have these 9296 06:02:45,360 --> 06:02:49,920 sorting algorithms 9297 06:02:47,120 --> 06:02:52,878 in picture and one of those sorting 9298 06:02:49,920 --> 06:02:56,080 algorithms is insertion sort now it is 9299 06:02:52,878 --> 06:02:58,240 the simplest easiest and a brute force 9300 06:02:56,080 --> 06:03:00,240 sorting algorithm now what do what do 9301 06:02:58,240 --> 06:03:02,400 you mean by brute force brute force 9302 06:03:00,240 --> 06:03:04,000 means straight forward 9303 06:03:02,400 --> 06:03:06,400 right in a naive way it means 9304 06:03:04,000 --> 06:03:08,878 straightforward that means you're not 9305 06:03:06,400 --> 06:03:09,840 keeping into uh you're not considering 9306 06:03:08,878 --> 06:03:11,200 any 9307 06:03:09,840 --> 06:03:13,440 efficiency 9308 06:03:11,200 --> 06:03:15,600 or you don't cons you don't care about 9309 06:03:13,440 --> 06:03:18,958 time complexity or space complexity you 9310 06:03:15,600 --> 06:03:22,718 just straight away sort it with the most 9311 06:03:18,958 --> 06:03:25,360 simpler and naive approach okay in this 9312 06:03:22,718 --> 06:03:27,120 brute force algorithm what happens that 9313 06:03:25,360 --> 06:03:28,558 let's suppose let me give an example 9314 06:03:27,120 --> 06:03:30,400 right obviously we can sort with the 9315 06:03:28,558 --> 06:03:32,080 help of this insertion sort algorithm 9316 06:03:30,400 --> 06:03:34,878 you can obviously sort either in 9317 06:03:32,080 --> 06:03:37,360 ascending or in descending order right 9318 06:03:34,878 --> 06:03:39,920 uh let's take one example we all know 9319 06:03:37,360 --> 06:03:42,400 about the card game right wherein you 9320 06:03:39,920 --> 06:03:44,480 have a bunch of cards right suppose you 9321 06:03:42,400 --> 06:03:46,320 have a single card that is in your hand 9322 06:03:44,480 --> 06:03:48,798 right and you have bunch of cards 9323 06:03:46,320 --> 06:03:51,600 available on the table now you start 9324 06:03:48,798 --> 06:03:53,680 picking those cards one by one obviously 9325 06:03:51,600 --> 06:03:55,600 the one that is in your hand is sorted 9326 06:03:53,680 --> 06:03:58,000 because if i told you to sort a number 9327 06:03:55,600 --> 06:04:00,000 one obviously there is only one element 9328 06:03:58,000 --> 06:04:02,240 in the array or anything right in the 9329 06:04:00,000 --> 06:04:04,000 linked list i told you to sort it but if 9330 06:04:02,240 --> 06:04:05,600 you're having only one element that is 9331 06:04:04,000 --> 06:04:07,840 itself sorted right you don't need to 9332 06:04:05,600 --> 06:04:08,718 sort that similarly what happens now 9333 06:04:07,840 --> 06:04:10,958 this 9334 06:04:08,718 --> 06:04:12,878 card is in your hand right it's just 9335 06:04:10,958 --> 06:04:14,638 like playing cards right now you have 9336 06:04:12,878 --> 06:04:16,958 this one card in your hand and it is 9337 06:04:14,638 --> 06:04:18,558 obviously sorted now what you will do in 9338 06:04:16,958 --> 06:04:20,878 the next turn 9339 06:04:18,558 --> 06:04:22,958 you start picking up one by one from 9340 06:04:20,878 --> 06:04:25,520 these set of cards that are available on 9341 06:04:22,958 --> 06:04:26,480 the table now let's suppose this is zero 9342 06:04:25,520 --> 06:04:28,400 okay 9343 06:04:26,480 --> 06:04:29,920 i'm considering these numerical values 9344 06:04:28,400 --> 06:04:32,160 so that because so that you can 9345 06:04:29,920 --> 06:04:34,638 understand and you can just connect the 9346 06:04:32,160 --> 06:04:37,520 dots right so what happens you have the 9347 06:04:34,638 --> 06:04:39,200 zero and now you start comparing it now 9348 06:04:37,520 --> 06:04:40,638 we are considering the scenario where 9349 06:04:39,200 --> 06:04:41,680 you are trying to sort an ascending 9350 06:04:40,638 --> 06:04:42,638 order okay 9351 06:04:41,680 --> 06:04:44,878 so 9352 06:04:42,638 --> 06:04:45,840 now let's try to decrease these things 9353 06:04:44,878 --> 06:04:48,000 so that 9354 06:04:45,840 --> 06:04:48,798 it's easier for you to understand things 9355 06:04:48,000 --> 06:04:52,000 okay 9356 06:04:48,798 --> 06:04:53,680 so now you have these two elements right 9357 06:04:52,000 --> 06:04:54,878 and now we are considering the case 9358 06:04:53,680 --> 06:04:57,840 wherein you are trying to sort in 9359 06:04:54,878 --> 06:05:00,400 ascending so you check okay if 0 is less 9360 06:04:57,840 --> 06:05:02,160 than one yes it is now you swap them 9361 06:05:00,400 --> 06:05:04,638 okay 9362 06:05:02,160 --> 06:05:06,558 now you have zero and one now these are 9363 06:05:04,638 --> 06:05:09,360 the two cards that are present and both 9364 06:05:06,558 --> 06:05:11,600 of these are sorted so now insertion 9365 06:05:09,360 --> 06:05:13,680 sort works in such a manner that you 9366 06:05:11,600 --> 06:05:15,280 will always have two 9367 06:05:13,680 --> 06:05:16,878 parts right 9368 06:05:15,280 --> 06:05:19,280 one is the sorted part obviously which 9369 06:05:16,878 --> 06:05:20,160 is in your hand and one is the unsorted 9370 06:05:19,280 --> 06:05:21,200 part 9371 06:05:20,160 --> 06:05:24,400 which is 9372 06:05:21,200 --> 06:05:26,878 on the on this deck right so similarly 9373 06:05:24,400 --> 06:05:28,798 you can you will start picking uh 9374 06:05:26,878 --> 06:05:31,200 elements or you can start picking these 9375 06:05:28,798 --> 06:05:33,280 cards one by one and keep sorting them 9376 06:05:31,200 --> 06:05:36,480 okay now this is 9377 06:05:33,280 --> 06:05:38,718 one simple scenario when you can apply 9378 06:05:36,480 --> 06:05:41,760 what insertion sort right this is the 9379 06:05:38,718 --> 06:05:43,120 most simpler way one can explain or one 9380 06:05:41,760 --> 06:05:46,480 can understand 9381 06:05:43,120 --> 06:05:48,320 you this insertion sort algorithm now it 9382 06:05:46,480 --> 06:05:50,160 is simple right now you start picking 9383 06:05:48,320 --> 06:05:52,320 these elements and you keep sorting them 9384 06:05:50,160 --> 06:05:55,040 and the at the end when all of these 9385 06:05:52,320 --> 06:05:57,920 elements are exhausted you will get your 9386 06:05:55,040 --> 06:06:00,400 sorted array now let's try to understand 9387 06:05:57,920 --> 06:06:02,480 insertion sort algorithm so in this 9388 06:06:00,400 --> 06:06:04,638 algorithm what happens obviously now we 9389 06:06:02,480 --> 06:06:06,638 know that we will have two parts right 9390 06:06:04,638 --> 06:06:09,440 one is the sorted part and another is 9391 06:06:06,638 --> 06:06:11,200 the unsorted part right so 9392 06:06:09,440 --> 06:06:13,520 obviously the one element that is 9393 06:06:11,200 --> 06:06:15,120 present in your hand or the element or 9394 06:06:13,520 --> 06:06:16,798 the card that is in your hand and 9395 06:06:15,120 --> 06:06:18,798 there's only one element there right the 9396 06:06:16,798 --> 06:06:20,480 one element in your hand obviously that 9397 06:06:18,798 --> 06:06:22,718 is sorted right so 9398 06:06:20,480 --> 06:06:24,638 we will not consider that first element 9399 06:06:22,718 --> 06:06:26,558 and we will start our iteration from the 9400 06:06:24,638 --> 06:06:28,400 second element right now we understand 9401 06:06:26,558 --> 06:06:31,040 why we are doing this that we are 9402 06:06:28,400 --> 06:06:33,200 starting from 2 to n minus 1 or 2 to n 9403 06:06:31,040 --> 06:06:34,878 depending upon the array that we are 9404 06:06:33,200 --> 06:06:36,558 starting from either we can start from 0 9405 06:06:34,878 --> 06:06:37,440 index or we can start from one index 9406 06:06:36,558 --> 06:06:38,638 right 9407 06:06:37,440 --> 06:06:41,200 so 9408 06:06:38,638 --> 06:06:43,280 we always start from element number two 9409 06:06:41,200 --> 06:06:44,080 right and then what we will do we'll 9410 06:06:43,280 --> 06:06:46,240 just 9411 06:06:44,080 --> 06:06:48,480 store this value inside temporary 9412 06:06:46,240 --> 06:06:52,160 variable and then we'll check if that 9413 06:06:48,480 --> 06:06:55,040 element is less than the element that we 9414 06:06:52,160 --> 06:06:57,120 have in this sorted part 9415 06:06:55,040 --> 06:06:59,760 if that is the case then we will shift 9416 06:06:57,120 --> 06:07:01,520 their positions right and we will get 9417 06:06:59,760 --> 06:07:03,600 both now we will have two elements in 9418 06:07:01,520 --> 06:07:05,280 the picture that is 0 and that and both 9419 06:07:03,600 --> 06:07:07,120 of these will be sorted in ascending 9420 06:07:05,280 --> 06:07:09,520 order and then 9421 06:07:07,120 --> 06:07:11,360 what we will do we'll consider the rest 9422 06:07:09,520 --> 06:07:13,920 of the cases that is starting from three 9423 06:07:11,360 --> 06:07:16,798 to so on to n okay now you might be 9424 06:07:13,920 --> 06:07:19,040 thinking okay how does this thing happen 9425 06:07:16,798 --> 06:07:21,280 let me take an example and let me show 9426 06:07:19,040 --> 06:07:23,920 you how let's consider this array that 9427 06:07:21,280 --> 06:07:26,000 we have over here that is index 0 these 9428 06:07:23,920 --> 06:07:30,480 are all the indexes that we have and 9429 06:07:26,000 --> 06:07:33,520 this is our array that is 23 10 16 11 9430 06:07:30,480 --> 06:07:34,638 and 20. so in the first step we are 9431 06:07:33,520 --> 06:07:37,360 making 9432 06:07:34,638 --> 06:07:39,520 now we are making partitions now this is 9433 06:07:37,360 --> 06:07:42,240 sorted part that is the first element 9434 06:07:39,520 --> 06:07:44,160 and this is our unsorted part now what 9435 06:07:42,240 --> 06:07:46,958 we will do in the first iteration this 9436 06:07:44,160 --> 06:07:49,760 is our iteration number one because this 9437 06:07:46,958 --> 06:07:50,958 is the case wherein we will start moving 9438 06:07:49,760 --> 06:07:53,600 from 9439 06:07:50,958 --> 06:07:55,760 second index that is first index if we 9440 06:07:53,600 --> 06:07:56,718 consider from zero right 9441 06:07:55,760 --> 06:07:58,400 so 9442 06:07:56,718 --> 06:08:00,798 we consider it from second element and 9443 06:07:58,400 --> 06:08:02,718 so on to n right so now in the first 9444 06:08:00,798 --> 06:08:05,120 iteration what we will do we'll compare 9445 06:08:02,718 --> 06:08:06,558 these two values okay let me just erase 9446 06:08:05,120 --> 06:08:09,120 everything 9447 06:08:06,558 --> 06:08:10,878 so that it's easier for you guys so now 9448 06:08:09,120 --> 06:08:13,600 we will compare these two now obviously 9449 06:08:10,878 --> 06:08:16,080 10 is less than 23 what we will do will 9450 06:08:13,600 --> 06:08:18,240 shift their positions now this is your 9451 06:08:16,080 --> 06:08:19,440 slotted part and this is your unsorted 9452 06:08:18,240 --> 06:08:21,280 part 9453 06:08:19,440 --> 06:08:23,440 we will do the same thing 9454 06:08:21,280 --> 06:08:25,840 right so in second iteration what we 9455 06:08:23,440 --> 06:08:28,718 will do here comes 60 now what we will 9456 06:08:25,840 --> 06:08:31,920 do we compare it first with 23 okay we 9457 06:08:28,718 --> 06:08:33,680 know now okay 16 is less than 23 so now 9458 06:08:31,920 --> 06:08:36,798 what we will do we'll swap their 9459 06:08:33,680 --> 06:08:38,958 positions so this is 16 and this is 23 9460 06:08:36,798 --> 06:08:41,120 now what will happen now 16 will be 9461 06:08:38,958 --> 06:08:44,160 compared with 10 obviously it is not 9462 06:08:41,120 --> 06:08:46,080 less than 10 so it will remain as it's 9463 06:08:44,160 --> 06:08:48,320 at its own position that is its new 9464 06:08:46,080 --> 06:08:50,798 position at index one right 9465 06:08:48,320 --> 06:08:53,440 so this is the second iteration and 9466 06:08:50,798 --> 06:08:55,040 after second iteration this will be your 9467 06:08:53,440 --> 06:08:57,600 sorted part as you can see that i have 9468 06:08:55,040 --> 06:09:00,798 bolded this text right holded the 9469 06:08:57,600 --> 06:09:00,798 borders of these two 9470 06:09:00,878 --> 06:09:05,040 these two elements and bolded the same 9471 06:09:03,200 --> 06:09:07,360 for these three elements because this is 9472 06:09:05,040 --> 06:09:09,520 the sorted part that we have over here 9473 06:09:07,360 --> 06:09:12,160 and this is the unsorted part now what 9474 06:09:09,520 --> 06:09:14,638 will happen in the third iteration that 9475 06:09:12,160 --> 06:09:18,638 it will check for this number that was 9476 06:09:14,638 --> 06:09:21,600 there it is 11 so for 11 what we will do 9477 06:09:18,638 --> 06:09:24,878 we'll compare it with what 9478 06:09:21,600 --> 06:09:26,958 repeat this thing now we'll take 11 into 9479 06:09:24,878 --> 06:09:29,920 consideration and now we'll check it 9480 06:09:26,958 --> 06:09:32,638 we'll swap them then 11 is here 23 is 9481 06:09:29,920 --> 06:09:35,120 here we'll check them we'll swap them 9482 06:09:32,638 --> 06:09:36,400 16 is here 11 is here we check them so 9483 06:09:35,120 --> 06:09:39,200 since 10 is 9484 06:09:36,400 --> 06:09:40,878 less than 11 so nothing will happen so 9485 06:09:39,200 --> 06:09:44,480 in the third iteration what will happen 9486 06:09:40,878 --> 06:09:47,040 we will have 10 11 16 and 23 these are 9487 06:09:44,480 --> 06:09:50,160 all sorted and we are only left with one 9488 06:09:47,040 --> 06:09:52,080 element which is unsorted right 9489 06:09:50,160 --> 06:09:52,958 now in the final iteration what will 9490 06:09:52,080 --> 06:09:55,920 happen 9491 06:09:52,958 --> 06:09:58,480 that 23 now the square d will be at its 9492 06:09:55,920 --> 06:10:00,558 original position that is it and rest of 9493 06:09:58,480 --> 06:10:02,878 the elements will be sorted now since we 9494 06:10:00,558 --> 06:10:05,360 have exhausted all the elements 9495 06:10:02,878 --> 06:10:07,360 all the elements have been exhausted and 9496 06:10:05,360 --> 06:10:10,240 we at the final step that is in 9497 06:10:07,360 --> 06:10:12,878 hydration 4 we will have this area that 9498 06:10:10,240 --> 06:10:14,798 is sorted after learning what is 9499 06:10:12,878 --> 06:10:18,240 incision sort let's quickly implement 9500 06:10:14,798 --> 06:10:20,480 the same in python language so i'm using 9501 06:10:18,240 --> 06:10:22,638 google collab whether it is easy for 9502 06:10:20,480 --> 06:10:24,558 everybody to access google collab so 9503 06:10:22,638 --> 06:10:27,280 need not install anything it's right 9504 06:10:24,558 --> 06:10:30,160 available in the online so let's quickly 9505 06:10:27,280 --> 06:10:33,040 switch to that google collab ide for 9506 06:10:30,160 --> 06:10:34,558 python so here you can find incision 9507 06:10:33,040 --> 06:10:36,160 sort the name 9508 06:10:34,558 --> 06:10:39,760 for the file in 9509 06:10:36,160 --> 06:10:41,920 python extension so with that we already 9510 06:10:39,760 --> 06:10:45,440 have this particular program which is 9511 06:10:41,920 --> 06:10:48,240 easy for me to explain to you so here 9512 06:10:45,440 --> 06:10:51,360 so we are considering a function called 9513 06:10:48,240 --> 06:10:53,520 incision sort right so the function is 9514 06:10:51,360 --> 06:10:56,558 called whenever the data has been passed 9515 06:10:53,520 --> 06:11:00,558 in order to sort the elements inside the 9516 06:10:56,558 --> 06:11:02,558 data in ascending order right so in 9517 06:11:00,558 --> 06:11:05,120 order to do that we have to write a 9518 06:11:02,558 --> 06:11:06,558 proper function accordingly as incision 9519 06:11:05,120 --> 06:11:09,040 sort will work 9520 06:11:06,558 --> 06:11:11,360 so how does that work you have already 9521 06:11:09,040 --> 06:11:14,000 learned about it so in order to 9522 06:11:11,360 --> 06:11:15,440 implement you have to use a for loop so 9523 06:11:14,000 --> 06:11:16,558 for loop has 9524 06:11:15,440 --> 06:11:19,040 a range 9525 06:11:16,558 --> 06:11:21,920 so it will be always checking for the 9526 06:11:19,040 --> 06:11:25,440 elements inside the array one by one for 9527 06:11:21,920 --> 06:11:27,920 comparison with the key element right so 9528 06:11:25,440 --> 06:11:29,200 whenever it is finding the key element 9529 06:11:27,920 --> 06:11:31,120 it will 9530 06:11:29,200 --> 06:11:32,878 which is greater than the key element or 9531 06:11:31,120 --> 06:11:36,160 which is lesser than it will swap 9532 06:11:32,878 --> 06:11:39,280 accordingly right so we are using while 9533 06:11:36,160 --> 06:11:41,120 loop in order to do that same work 9534 06:11:39,280 --> 06:11:43,280 so we are stopping from the current 9535 06:11:41,120 --> 06:11:45,120 position where it has been found which 9536 06:11:43,280 --> 06:11:47,440 is greater or which is smaller 9537 06:11:45,120 --> 06:11:49,040 accordingly we'll stop it 9538 06:11:47,440 --> 06:11:51,920 right 9539 06:11:49,040 --> 06:11:54,400 so then we have the data which has been 9540 06:11:51,920 --> 06:11:56,320 given here so the data is present that 9541 06:11:54,400 --> 06:11:58,558 is 52178 9542 06:11:56,320 --> 06:12:01,040 so what happens in this particular data 9543 06:11:58,558 --> 06:12:03,920 is when it passes through this function 9544 06:12:01,040 --> 06:12:06,000 every element will be sorted with the 9545 06:12:03,920 --> 06:12:08,878 help of incision sort function which we 9546 06:12:06,000 --> 06:12:12,000 have written here so first it will 9547 06:12:08,878 --> 06:12:15,040 compare the elements and it will try to 9548 06:12:12,000 --> 06:12:17,760 sort in ascending order say for example 9549 06:12:15,040 --> 06:12:20,240 if you want to do descending order then 9550 06:12:17,760 --> 06:12:22,240 you have to change just one single 9551 06:12:20,240 --> 06:12:25,120 element that is this key should be 9552 06:12:22,240 --> 06:12:28,638 greater than array element that's about 9553 06:12:25,120 --> 06:12:31,760 it nothing else no change so after that 9554 06:12:28,638 --> 06:12:34,558 incision sort uh is the function is 9555 06:12:31,760 --> 06:12:36,638 having the data which is present here so 9556 06:12:34,558 --> 06:12:38,718 all these functions will be completed 9557 06:12:36,638 --> 06:12:41,040 then we'll be printing the final output 9558 06:12:38,718 --> 06:12:43,280 how do you print once the function has 9559 06:12:41,040 --> 06:12:46,080 completed sorting immediately it will be 9560 06:12:43,280 --> 06:12:48,718 stored in the variable data itself so 9561 06:12:46,080 --> 06:12:51,440 that particular data has been printed 9562 06:12:48,718 --> 06:12:53,840 after sort elements will be viewed right 9563 06:12:51,440 --> 06:12:55,520 so this is just a print statement sorted 9564 06:12:53,840 --> 06:12:57,040 array in ascending order so if you're 9565 06:12:55,520 --> 06:12:59,200 doing for descending you can make it a 9566 06:12:57,040 --> 06:13:02,480 sorted array in descending order so 9567 06:12:59,200 --> 06:13:04,240 let's quickly check how this output look 9568 06:13:02,480 --> 06:13:06,160 like 9569 06:13:04,240 --> 06:13:08,638 so here you have sorted array in 9570 06:13:06,160 --> 06:13:11,440 ascending order so that is one two five 9571 06:13:08,638 --> 06:13:15,280 seven eight right from smaller to the 9572 06:13:11,440 --> 06:13:17,600 higher number so let's quickly make a 9573 06:13:15,280 --> 06:13:18,558 small change here so that 9574 06:13:17,600 --> 06:13:21,040 it will 9575 06:13:18,558 --> 06:13:23,840 give us the descending order let's try 9576 06:13:21,040 --> 06:13:23,840 to work on it 9577 06:13:25,520 --> 06:13:29,680 right if you could see here 9578 06:13:27,280 --> 06:13:31,600 key is greater than array element then 9579 06:13:29,680 --> 06:13:36,240 you will be getting the descending order 9580 06:13:31,600 --> 06:13:37,360 that is 8 7 5 2 1. so you can change it 9581 06:13:36,240 --> 06:13:39,680 likewise 9582 06:13:37,360 --> 06:13:43,440 okay i didn't change the printing 9583 06:13:39,680 --> 06:13:46,400 statement so i'm just changing 9584 06:13:43,440 --> 06:13:47,920 descending order right 9585 06:13:46,400 --> 06:13:50,400 run the same 9586 06:13:47,920 --> 06:13:52,878 that's been declared so 9587 06:13:50,400 --> 06:13:55,040 this is how incision sort will work in 9588 06:13:52,878 --> 06:13:57,840 python and the code if you could see it 9589 06:13:55,040 --> 06:13:59,520 is very small and quickly it is 9590 06:13:57,840 --> 06:14:01,440 eliminating all the variable 9591 06:13:59,520 --> 06:14:03,280 initializations we make 9592 06:14:01,440 --> 06:14:05,920 anything and everything you just want to 9593 06:14:03,280 --> 06:14:08,558 have the function pass the data get it 9594 06:14:05,920 --> 06:14:11,600 sorted and the output is done so this is 9595 06:14:08,558 --> 06:14:14,400 all about incision sort in python 9596 06:14:11,600 --> 06:14:17,200 now let's talk about insertions or time 9597 06:14:14,400 --> 06:14:19,200 complexity so in the worst case when all 9598 06:14:17,200 --> 06:14:22,080 the elements are in 9599 06:14:19,200 --> 06:14:24,480 manner and we need to sort them one by 9600 06:14:22,080 --> 06:14:26,400 one so obviously we are talking about 9601 06:14:24,480 --> 06:14:28,958 first the outer loop which runs from one 9602 06:14:26,400 --> 06:14:31,360 to n and then the inner loop which runs 9603 06:14:28,958 --> 06:14:32,958 backwards and in the last first we 9604 06:14:31,360 --> 06:14:35,040 consider in the first iteration we only 9605 06:14:32,958 --> 06:14:37,840 consider the zeroth element then as we 9606 06:14:35,040 --> 06:14:38,718 move along it will be running from end 9607 06:14:37,840 --> 06:14:40,000 to 9608 06:14:38,718 --> 06:14:42,638 zeroth element right we will be 9609 06:14:40,000 --> 06:14:45,280 considering the whole n elements so in 9610 06:14:42,638 --> 06:14:47,280 that case the time complexity the worst 9611 06:14:45,280 --> 06:14:48,718 case will be order of n squares because 9612 06:14:47,280 --> 06:14:51,360 we have two 9613 06:14:48,718 --> 06:14:53,200 nested loops that is one is for loop and 9614 06:14:51,360 --> 06:14:55,760 inside that for loop we have that we 9615 06:14:53,200 --> 06:14:58,080 have that while right so this is the in 9616 06:14:55,760 --> 06:15:00,400 the worst case and it happens also in 9617 06:14:58,080 --> 06:15:03,440 the average case where some part 9618 06:15:00,400 --> 06:15:06,160 or the sorted part is already there and 9619 06:15:03,440 --> 06:15:08,320 it is let's suppose we have five four 9620 06:15:06,160 --> 06:15:10,320 five six seven eight and then we have 9621 06:15:08,320 --> 06:15:11,920 the unsorted part so half of the 9622 06:15:10,320 --> 06:15:14,160 elements are sorted and half of the 9623 06:15:11,920 --> 06:15:16,798 elements are not sorted so it will be n 9624 06:15:14,160 --> 06:15:19,280 square by 2 so we are not considering 9625 06:15:16,798 --> 06:15:22,080 the case where we talk about constants 9626 06:15:19,280 --> 06:15:24,958 and we are negotiating the constants and 9627 06:15:22,080 --> 06:15:27,680 in that case the average time complexity 9628 06:15:24,958 --> 06:15:30,160 will be n square right 9629 06:15:27,680 --> 06:15:32,798 but the most important thing that 9630 06:15:30,160 --> 06:15:36,160 is there in this time complexity is the 9631 06:15:32,798 --> 06:15:39,600 best case that means when your elements 9632 06:15:36,160 --> 06:15:43,360 that is 5 6 7 8 9 and 10 when the 9633 06:15:39,600 --> 06:15:45,520 elements in the array are already sorted 9634 06:15:43,360 --> 06:15:48,160 what happens in this case 9635 06:15:45,520 --> 06:15:51,040 if you observe the for loop that runs 9636 06:15:48,160 --> 06:15:52,798 from 1 to n will be always there so n is 9637 06:15:51,040 --> 06:15:54,400 always there the time complexity the 9638 06:15:52,798 --> 06:15:56,958 bigger notation 9639 06:15:54,400 --> 06:15:58,638 big o of n will be always there but in 9640 06:15:56,958 --> 06:16:00,638 this while loop wherein we've been 9641 06:15:58,638 --> 06:16:06,000 checking for if j is greater than and 9642 06:16:00,638 --> 06:16:09,440 equal to 0 and if ar of j is less than m 9643 06:16:06,000 --> 06:16:12,718 right in that case this will never be 9644 06:16:09,440 --> 06:16:14,320 executed because this ar of g will 9645 06:16:12,718 --> 06:16:17,280 always be 9646 06:16:14,320 --> 06:16:18,798 less than will always be greater than 10 9647 06:16:17,280 --> 06:16:20,958 why because we are talking about this 9648 06:16:18,798 --> 06:16:23,520 element and we are checking if this 6 is 9649 06:16:20,958 --> 06:16:26,480 less than 5 no it is not if the 7 is 9650 06:16:23,520 --> 06:16:29,120 less than 6 no is it it is not so this 9651 06:16:26,480 --> 06:16:31,920 condition will always be false for all 9652 06:16:29,120 --> 06:16:34,320 the elements so in nutshell we are just 9653 06:16:31,920 --> 06:16:36,958 checking these steps only once 9654 06:16:34,320 --> 06:16:40,320 in every iteration so that is for the 9655 06:16:36,958 --> 06:16:43,520 reason that the whole time complexity in 9656 06:16:40,320 --> 06:16:46,558 the best case will be bigger of n and 9657 06:16:43,520 --> 06:16:49,280 not big o of n square in the best case 9658 06:16:46,558 --> 06:16:50,958 okay now let's talk about insertions or 9659 06:16:49,280 --> 06:16:53,600 space complexity 9660 06:16:50,958 --> 06:16:56,558 if you have observed in algorithms and 9661 06:16:53,600 --> 06:16:59,360 in implementation we never talked about 9662 06:16:56,558 --> 06:17:02,160 any auxiliary memory right we were not 9663 06:16:59,360 --> 06:17:04,878 using any extra space either in the form 9664 06:17:02,160 --> 06:17:07,440 of array linked list stack queue 9665 06:17:04,878 --> 06:17:10,718 or anything right so that is for the 9666 06:17:07,440 --> 06:17:14,480 reason the space complexity of 9667 06:17:10,718 --> 06:17:16,798 insertion sort is big o of one that is 9668 06:17:14,480 --> 06:17:19,200 constant amount of space 9669 06:17:16,798 --> 06:17:22,080 now let's talk about insertion sort 9670 06:17:19,200 --> 06:17:25,360 analysis wherein we will be analyzing 9671 06:17:22,080 --> 06:17:28,638 comparisons number of swaps stable or 9672 06:17:25,360 --> 06:17:30,638 unstable in place or outplays 9673 06:17:28,638 --> 06:17:32,718 so first let's talk about number of 9674 06:17:30,638 --> 06:17:35,840 comparisons required 9675 06:17:32,718 --> 06:17:38,080 in this we will talk about two scenarios 9676 06:17:35,840 --> 06:17:39,840 wherein we will talk about worst case 9677 06:17:38,080 --> 06:17:42,240 and average case 9678 06:17:39,840 --> 06:17:45,200 in worst case the 9679 06:17:42,240 --> 06:17:47,440 number of comparisons required is n 9680 06:17:45,200 --> 06:17:50,240 square by two 9681 06:17:47,440 --> 06:17:54,240 now if you talk about average case 9682 06:17:50,240 --> 06:17:56,000 scenario it is n square by 4 which is 9683 06:17:54,240 --> 06:18:01,760 twice 9684 06:17:56,000 --> 06:18:04,160 as much as this right it is 2 times 9685 06:18:01,760 --> 06:18:07,120 if we talk about number of swaps that 9686 06:18:04,160 --> 06:18:08,718 are required in insertion sort 9687 06:18:07,120 --> 06:18:11,520 in again we will talk about two 9688 06:18:08,718 --> 06:18:14,080 scenarios average and worst case 9689 06:18:11,520 --> 06:18:16,400 in average case it is 9690 06:18:14,080 --> 06:18:19,760 n square by eight 9691 06:18:16,400 --> 06:18:22,320 and in worst case it is n square by four 9692 06:18:19,760 --> 06:18:25,200 these are the number of swaps required 9693 06:18:22,320 --> 06:18:27,840 and if you want to check those if these 9694 06:18:25,200 --> 06:18:30,000 statements hold or not if these 9695 06:18:27,840 --> 06:18:32,080 equations hold or not you can always 9696 06:18:30,000 --> 06:18:34,718 take an example wherein you will be 9697 06:18:32,080 --> 06:18:37,520 considering both the cases even as well 9698 06:18:34,718 --> 06:18:38,958 as odd so take an example and run 9699 06:18:37,520 --> 06:18:40,958 through it 9700 06:18:38,958 --> 06:18:44,160 now if you talk about stability of 9701 06:18:40,958 --> 06:18:46,320 insertion sort it is a stable algorithm 9702 06:18:44,160 --> 06:18:48,638 what do you mean by stable so if you 9703 06:18:46,320 --> 06:18:50,478 have an area which contains one three 9704 06:18:48,638 --> 06:18:53,040 one dash and five 9705 06:18:50,478 --> 06:18:55,280 in this area the relative position of 9706 06:18:53,040 --> 06:18:57,040 these two ones that is this one and this 9707 06:18:55,280 --> 06:18:59,840 one let me change the color and let me 9708 06:18:57,040 --> 06:19:02,558 show you the relative positions of this 9709 06:18:59,840 --> 06:19:04,478 one and this one will remain 9710 06:19:02,558 --> 06:19:06,878 intact what do you mean by this thing 9711 06:19:04,478 --> 06:19:08,558 that whenever you are sorting it 9712 06:19:06,878 --> 06:19:10,080 you can sort it in two different ways 9713 06:19:08,558 --> 06:19:12,240 right this is also sorted and this is 9714 06:19:10,080 --> 06:19:15,040 also sorted that means you can either 9715 06:19:12,240 --> 06:19:18,798 have one one dash three and five or you 9716 06:19:15,040 --> 06:19:20,478 can have one dash one and three and five 9717 06:19:18,798 --> 06:19:22,638 this is obviously that this this number 9718 06:19:20,478 --> 06:19:24,798 is repeated but this is the first number 9719 06:19:22,638 --> 06:19:26,160 this is this occurred here the first 9720 06:19:24,798 --> 06:19:28,320 time and here it is the second 9721 06:19:26,160 --> 06:19:31,040 occurrence now you want to keep the 9722 06:19:28,320 --> 06:19:33,200 relative positions intact right so both 9723 06:19:31,040 --> 06:19:35,520 of these are sorted right but if you 9724 06:19:33,200 --> 06:19:39,600 talk about stability this is known as 9725 06:19:35,520 --> 06:19:40,638 stable and this is unstable okay 9726 06:19:39,600 --> 06:19:42,558 now 9727 06:19:40,638 --> 06:19:45,600 insertion sort whenever you are trying 9728 06:19:42,558 --> 06:19:48,000 to implement insertion sort it is stable 9729 06:19:45,600 --> 06:19:51,280 that means the relative positions of 9730 06:19:48,000 --> 06:19:54,160 both these ones will be intact okay so 9731 06:19:51,280 --> 06:19:56,878 if someone asks you if insertion sort is 9732 06:19:54,160 --> 06:19:59,120 stable or not you will say yes why 9733 06:19:56,878 --> 06:20:01,280 because the relative positions of the 9734 06:19:59,120 --> 06:20:04,478 number that are of the numbers that are 9735 06:20:01,280 --> 06:20:07,200 repeated remains intact now what about 9736 06:20:04,478 --> 06:20:10,000 this in place or outplays 9737 06:20:07,200 --> 06:20:12,798 since we are not using any auxiliary 9738 06:20:10,000 --> 06:20:15,600 memory right we didn't use any stack cue 9739 06:20:12,798 --> 06:20:18,240 link list or array that is the reason 9740 06:20:15,600 --> 06:20:19,680 that whenever you are not using any 9741 06:20:18,240 --> 06:20:23,120 extra memory 9742 06:20:19,680 --> 06:20:26,000 it is supposed to be in place algorithm 9743 06:20:23,120 --> 06:20:28,878 so if an algorithm is sorted within the 9744 06:20:26,000 --> 06:20:31,040 array that was there earlier that means 9745 06:20:28,878 --> 06:20:34,558 you are not using any extra space that 9746 06:20:31,040 --> 06:20:37,280 algorithm is known as in place algorithm 9747 06:20:34,558 --> 06:20:40,080 which is evident now in insertion sort 9748 06:20:37,280 --> 06:20:45,280 as we are not using any extra memory so 9749 06:20:40,080 --> 06:20:47,440 insertion sort is an in place algorithm 9750 06:20:45,280 --> 06:20:49,120 now let's look at the example wherein we 9751 06:20:47,440 --> 06:20:51,360 will implement 9752 06:20:49,120 --> 06:20:54,160 insertion sort if you can see we have 9753 06:20:51,360 --> 06:20:57,520 this example over here wherein we have 6 9754 06:20:54,160 --> 06:21:01,280 5 3 2 8 10 9755 06:20:57,520 --> 06:21:04,958 9 and 11 and we have been given this k 9756 06:21:01,280 --> 06:21:06,558 what this key signific signifies that 9757 06:21:04,958 --> 06:21:08,798 the maximum 9758 06:21:06,558 --> 06:21:11,280 swaps or comparisons 9759 06:21:08,798 --> 06:21:13,280 needed for this three 9760 06:21:11,280 --> 06:21:15,520 either on the left side or on the right 9761 06:21:13,280 --> 06:21:16,798 side right 9762 06:21:15,520 --> 06:21:20,400 the number of 9763 06:21:16,798 --> 06:21:23,440 positions that it this 3 needs to 9764 06:21:20,400 --> 06:21:26,478 get to its original position is 3 so 9765 06:21:23,440 --> 06:21:30,320 this is a question that is known as 9766 06:21:26,478 --> 06:21:32,878 nearly sorted array or k sorted array we 9767 06:21:30,320 --> 06:21:34,878 do not need to sort all the elements in 9768 06:21:32,878 --> 06:21:37,440 the array but we are specifically 9769 06:21:34,878 --> 06:21:40,400 looking for those elements which are not 9770 06:21:37,440 --> 06:21:43,040 at its original position and if we want 9771 06:21:40,400 --> 06:21:46,798 to get them to their original position 9772 06:21:43,040 --> 06:21:50,400 the maximum comparisons or swaps that we 9773 06:21:46,798 --> 06:21:53,440 require is 3 so if you see this 3 the 9774 06:21:50,400 --> 06:21:54,798 original position of this 3 is 9775 06:21:53,440 --> 06:21:56,958 this 9776 06:21:54,798 --> 06:22:00,400 5 that means in the sorted area it will 9777 06:21:56,958 --> 06:22:03,520 be here similarly if you talk about this 9778 06:22:00,400 --> 06:22:05,600 two the number of swaps that it should 9779 06:22:03,520 --> 06:22:07,760 do is one two 9780 06:22:05,600 --> 06:22:09,600 and then it will it will be at its 9781 06:22:07,760 --> 06:22:11,680 original position or you can say that 9782 06:22:09,600 --> 06:22:14,478 one two and three so max it can go to 9783 06:22:11,680 --> 06:22:16,638 three positions okay so similarly 9784 06:22:14,478 --> 06:22:19,920 it will be the same for all the elements 9785 06:22:16,638 --> 06:22:22,478 so at most three okay 9786 06:22:19,920 --> 06:22:25,200 and at least it can be that it will have 9787 06:22:22,478 --> 06:22:27,120 it doesn't need to move at any location 9788 06:22:25,200 --> 06:22:30,160 that it will have its own original 9789 06:22:27,120 --> 06:22:32,798 position is like an 11 you see 11 is at 9790 06:22:30,160 --> 06:22:36,000 its own position in the original area as 9791 06:22:32,798 --> 06:22:39,040 well as in the swapped area so at most 9792 06:22:36,000 --> 06:22:40,240 you have three positions selection sort 9793 06:22:39,040 --> 06:22:42,718 algorithm 9794 06:22:40,240 --> 06:22:45,600 now let's talk about what is selection 9795 06:22:42,718 --> 06:22:47,680 sort now before we move to this sorting 9796 06:22:45,600 --> 06:22:50,080 algorithm let's try to understand what 9797 06:22:47,680 --> 06:22:52,638 is sorting so if you have a bunch of 9798 06:22:50,080 --> 06:22:54,718 students and out of those bunch of 9799 06:22:52,638 --> 06:22:57,360 students let's suppose some of them have 9800 06:22:54,718 --> 06:22:59,680 either left the college or school or 9801 06:22:57,360 --> 06:23:02,878 they are absent right now you want to 9802 06:22:59,680 --> 06:23:06,080 arrange the remaining students either in 9803 06:23:02,878 --> 06:23:08,400 ascending order or in descending order 9804 06:23:06,080 --> 06:23:11,120 so for that reason you might require 9805 06:23:08,400 --> 06:23:13,120 sorting so this is one of such sorting 9806 06:23:11,120 --> 06:23:15,600 algorithm parent 9807 06:23:13,120 --> 06:23:18,000 which helps us to sort 9808 06:23:15,600 --> 06:23:20,878 elements or number of students it can be 9809 06:23:18,000 --> 06:23:23,040 any object right so it is a simple sort 9810 06:23:20,878 --> 06:23:26,320 algorithm that revolves around the 9811 06:23:23,040 --> 06:23:29,040 comparison so now this is 9812 06:23:26,320 --> 06:23:31,440 different from if you talk about what if 9813 06:23:29,040 --> 06:23:34,558 you talk about bubble sort 9814 06:23:31,440 --> 06:23:36,840 insertion sort and selection sort this 9815 06:23:34,558 --> 06:23:40,160 sorting algorithm is 9816 06:23:36,840 --> 06:23:42,798 predominantly based on comparison right 9817 06:23:40,160 --> 06:23:44,638 the comparisons are done more as 9818 06:23:42,798 --> 06:23:47,440 compared to swapping 9819 06:23:44,638 --> 06:23:50,000 in each iteration one element gets 9820 06:23:47,440 --> 06:23:52,638 placed right if you think about bubble 9821 06:23:50,000 --> 06:23:54,638 how it work like a bubble and at the end 9822 06:23:52,638 --> 06:23:57,360 you had your element 9823 06:23:54,638 --> 06:23:59,280 fixed at its original position which was 9824 06:23:57,360 --> 06:24:02,320 at the end when we are trying to sort 9825 06:23:59,280 --> 06:24:04,240 our array in the ascending order now in 9826 06:24:02,320 --> 06:24:06,718 the same fashion right 9827 06:24:04,240 --> 06:24:09,040 in this sorting algorithm 9828 06:24:06,718 --> 06:24:11,440 one element that is 9829 06:24:09,040 --> 06:24:13,760 that can be either the largest element 9830 06:24:11,440 --> 06:24:16,000 or the smallest element so we are going 9831 06:24:13,760 --> 06:24:18,080 to put in the case wherein we will be 9832 06:24:16,000 --> 06:24:21,520 dealing with the smallest element in 9833 06:24:18,080 --> 06:24:24,478 each iteration the smallest element get 9834 06:24:21,520 --> 06:24:27,120 its placed fixed in the original array 9835 06:24:24,478 --> 06:24:29,760 that means when we sort the array 9836 06:24:27,120 --> 06:24:31,680 wherein this element belongs that is the 9837 06:24:29,760 --> 06:24:33,520 least element that is on the first index 9838 06:24:31,680 --> 06:24:35,680 right so this element 9839 06:24:33,520 --> 06:24:38,478 will have its position after one 9840 06:24:35,680 --> 06:24:41,200 iteration we will be choosing a minimum 9841 06:24:38,478 --> 06:24:43,920 index the sorting algorithm we have one 9842 06:24:41,200 --> 06:24:45,760 minimum index and that can be named as 9843 06:24:43,920 --> 06:24:48,718 minimum right since we are dealing with 9844 06:24:45,760 --> 06:24:50,478 the smallest element so now that minimum 9845 06:24:48,718 --> 06:24:52,240 element in the array 9846 06:24:50,478 --> 06:24:54,558 is placed at the beginning right 9847 06:24:52,240 --> 06:24:56,958 beginning of the array and later on we 9848 06:24:54,558 --> 06:24:59,440 will swap we will compare it with all 9849 06:24:56,958 --> 06:25:02,000 the elements and find out the minimum 9850 06:24:59,440 --> 06:25:04,478 and then swap it with the first index 9851 06:25:02,000 --> 06:25:07,680 okay similarly we are going to do for 9852 06:25:04,478 --> 06:25:10,400 all the rest of the elements okay 9853 06:25:07,680 --> 06:25:12,718 now the selection sort is basically 9854 06:25:10,400 --> 06:25:16,638 selecting an element that is the minimum 9855 06:25:12,718 --> 06:25:19,760 element in each hydration and placing it 9856 06:25:16,638 --> 06:25:23,120 at the appropriate position so that is 9857 06:25:19,760 --> 06:25:24,320 the basic logic behind what selection 9858 06:25:23,120 --> 06:25:26,958 saw 9859 06:25:24,320 --> 06:25:29,840 now let's talk about selection sort 9860 06:25:26,958 --> 06:25:31,760 algorithm now in this 9861 06:25:29,840 --> 06:25:34,400 our main objective is to find the 9862 06:25:31,760 --> 06:25:36,878 minimum index right so let's suppose if 9863 06:25:34,400 --> 06:25:37,600 we have one headache 9864 06:25:36,878 --> 06:25:40,878 now 9865 06:25:37,600 --> 06:25:43,760 in this area there is one minimum index 9866 06:25:40,878 --> 06:25:46,080 let's suppose that it is the first it is 9867 06:25:43,760 --> 06:25:48,558 at the first index right since we are 9868 06:25:46,080 --> 06:25:50,400 keeping in mind right i told you that 9869 06:25:48,558 --> 06:25:52,320 when we talked about what is selection 9870 06:25:50,400 --> 06:25:54,478 so what at that time i told you that we 9871 06:25:52,320 --> 06:25:57,440 can have two cases either you can pick 9872 06:25:54,478 --> 06:25:59,280 up the largest element and 9873 06:25:57,440 --> 06:26:01,360 fix its position at the end or you can 9874 06:25:59,280 --> 06:26:03,600 pick the minimum element that is the 9875 06:26:01,360 --> 06:26:05,600 smallest element okay so in this case we 9876 06:26:03,600 --> 06:26:07,440 have we will be dealing it with the 9877 06:26:05,600 --> 06:26:10,240 smallest element right 9878 06:26:07,440 --> 06:26:13,360 now this is our minimum index right this 9879 06:26:10,240 --> 06:26:16,798 zeroth index now this outer loop will 9880 06:26:13,360 --> 06:26:19,360 work from zero to n minus one now why n 9881 06:26:16,798 --> 06:26:21,840 minus one okay 9882 06:26:19,360 --> 06:26:24,160 i will tell you why because 9883 06:26:21,840 --> 06:26:26,718 um then what we will do the next step is 9884 06:26:24,160 --> 06:26:28,558 that we'll have this minimum index right 9885 06:26:26,718 --> 06:26:29,680 and it is pointing to z 9886 06:26:28,558 --> 06:26:30,638 in this case 9887 06:26:29,680 --> 06:26:32,878 and 9888 06:26:30,638 --> 06:26:35,360 our iterator that is i will start from 9889 06:26:32,878 --> 06:26:37,600 this position then in the nested for 9890 06:26:35,360 --> 06:26:39,360 loop that is again this is condition 9891 06:26:37,600 --> 06:26:40,878 where we have one for loop and inside 9892 06:26:39,360 --> 06:26:43,040 that we have another for loop that is 9893 06:26:40,878 --> 06:26:45,040 what you mean what do you mean by nested 9894 06:26:43,040 --> 06:26:47,360 for loop now since it's a comparison 9895 06:26:45,040 --> 06:26:50,240 algorithm what we are going to do inside 9896 06:26:47,360 --> 06:26:52,638 this sorting sorted part or inside this 9897 06:26:50,240 --> 06:26:55,600 unsorted part that is there what you can 9898 06:26:52,638 --> 06:26:57,760 say the rest of the elements okay so we 9899 06:26:55,600 --> 06:27:00,240 have this j iterator which is pointing 9900 06:26:57,760 --> 06:27:02,718 to i plus 1 that means we will not 9901 06:27:00,240 --> 06:27:04,798 compare it doesn't make sense right if i 9902 06:27:02,718 --> 06:27:06,240 and j are at the same position and we 9903 06:27:04,798 --> 06:27:08,638 are comparing them it does make sense 9904 06:27:06,240 --> 06:27:11,840 right so we have this j placed 9905 06:27:08,638 --> 06:27:14,558 at this iterator at this index right one 9906 06:27:11,840 --> 06:27:16,638 this j iterator is at in x one now what 9907 06:27:14,558 --> 06:27:18,638 we will be doing will be comparing it 9908 06:27:16,638 --> 06:27:21,920 because it's a comparison algorithm and 9909 06:27:18,638 --> 06:27:23,920 number of swaps are less that means if 9910 06:27:21,920 --> 06:27:26,958 there are any elements there will be 9911 06:27:23,920 --> 06:27:29,440 n or n minus one swaps okay 9912 06:27:26,958 --> 06:27:32,080 so the swapping is reduced as compared 9913 06:27:29,440 --> 06:27:34,240 to bubble sort okay so now what happens 9914 06:27:32,080 --> 06:27:36,638 we will be comparing them okay and we 9915 06:27:34,240 --> 06:27:39,680 will be trying to find the minimum index 9916 06:27:36,638 --> 06:27:42,718 let's suppose this is 3 and this is 1. 9917 06:27:39,680 --> 06:27:45,520 so since 3 is greater than 1 that means 9918 06:27:42,718 --> 06:27:48,478 our minimum index will be updated so our 9919 06:27:45,520 --> 06:27:50,638 now our new minimum index is what 9920 06:27:48,478 --> 06:27:52,638 and index is the one right this is the 9921 06:27:50,638 --> 06:27:55,120 minimum index okay done 9922 06:27:52,638 --> 06:27:56,798 now once this comparison is done right 9923 06:27:55,120 --> 06:28:00,080 so again we will check 9924 06:27:56,798 --> 06:28:03,360 if 4 is less than 1 no if 5 is less than 9925 06:28:00,080 --> 06:28:06,320 1 no if 6 is less than 1 no 9926 06:28:03,360 --> 06:28:08,160 if 0 is less than 1 yes 0 is less than 1 9927 06:28:06,320 --> 06:28:10,080 that means we are again comparing it 9928 06:28:08,160 --> 06:28:11,120 right and we are not swapping them we 9929 06:28:10,080 --> 06:28:13,760 will wait 9930 06:28:11,120 --> 06:28:16,400 once this entire iteration is done so i 9931 06:28:13,760 --> 06:28:18,080 will update my minimum index to 1 5 9932 06:28:16,400 --> 06:28:20,958 because that is the minimum index then 9933 06:28:18,080 --> 06:28:23,520 we have let's suppose we have eight okay 9934 06:28:20,958 --> 06:28:25,040 so now once this entire for loop is 9935 06:28:23,520 --> 06:28:27,840 exhausted right 9936 06:28:25,040 --> 06:28:31,280 now what we will do we will swap this 9937 06:28:27,840 --> 06:28:32,558 minimum indexed element with the 9938 06:28:31,280 --> 06:28:34,558 this with the 9939 06:28:32,558 --> 06:28:36,718 with the minimum index that was earlier 9940 06:28:34,558 --> 06:28:39,280 there that is the zeroth index okay now 9941 06:28:36,718 --> 06:28:41,280 we'll swap it with the zeroth index okay 9942 06:28:39,280 --> 06:28:43,760 so now what happened 9943 06:28:41,280 --> 06:28:46,160 this we will get here zero and we will 9944 06:28:43,760 --> 06:28:49,120 get here three so only one swapping gets 9945 06:28:46,160 --> 06:28:51,200 done so now after first iteration all 9946 06:28:49,120 --> 06:28:53,280 the elements will be at its own position 9947 06:28:51,200 --> 06:28:55,600 because since we didn't change we need 9948 06:28:53,280 --> 06:28:57,360 to swap any elements we swapped only a 9949 06:28:55,600 --> 06:29:00,000 single element that has these two 9950 06:28:57,360 --> 06:29:03,280 indexes that is fifth and zero okay so 9951 06:29:00,000 --> 06:29:05,680 now what we will have zero one four five 9952 06:29:03,280 --> 06:29:07,200 six and three and at the end we have 9953 06:29:05,680 --> 06:29:09,840 eight okay 9954 06:29:07,200 --> 06:29:12,638 and then we will hydrate through again 9955 06:29:09,840 --> 06:29:14,558 so now our iterator will be here and our 9956 06:29:12,638 --> 06:29:16,878 j will be here and this time around 9957 06:29:14,558 --> 06:29:19,120 minimum index will be right earlier it 9958 06:29:16,878 --> 06:29:21,440 was zero now since we will not take care 9959 06:29:19,120 --> 06:29:23,680 of this or we will not will not think 9960 06:29:21,440 --> 06:29:26,478 about this index now because this is 9961 06:29:23,680 --> 06:29:28,638 already at its original position the 9962 06:29:26,478 --> 06:29:31,200 element is at its appropriate position 9963 06:29:28,638 --> 06:29:34,080 that means when we sort the array zero 9964 06:29:31,200 --> 06:29:35,840 will be at zeroth index so we will not 9965 06:29:34,080 --> 06:29:37,840 will not think about it and it doesn't 9966 06:29:35,840 --> 06:29:40,080 matter to us now because it is at its 9967 06:29:37,840 --> 06:29:42,958 original position so minimum index will 9968 06:29:40,080 --> 06:29:45,280 be now one okay so now let's try to see 9969 06:29:42,958 --> 06:29:48,478 an example wherein we get the 9970 06:29:45,280 --> 06:29:50,878 better understanding of this algorithm 9971 06:29:48,478 --> 06:29:55,120 so if you observe carefully this is our 9972 06:29:50,878 --> 06:29:57,040 input right so we have 23 10 16 11 and 9973 06:29:55,120 --> 06:29:59,920 20. 9974 06:29:57,040 --> 06:30:02,320 now what happens we'll make this our 9975 06:29:59,920 --> 06:30:04,638 first step and this is our iteration 9976 06:30:02,320 --> 06:30:07,440 number one okay so now what happens in 9977 06:30:04,638 --> 06:30:08,478 this we have our minimum index that is 9978 06:30:07,440 --> 06:30:11,440 at c 9979 06:30:08,478 --> 06:30:14,400 this is our i and this is our g 9980 06:30:11,440 --> 06:30:17,680 now we'll compare right is 10 greater 9981 06:30:14,400 --> 06:30:19,600 than 23 no it is not so still we have 9982 06:30:17,680 --> 06:30:22,160 minimum index as zero 9983 06:30:19,600 --> 06:30:26,400 is 10 greater than 60 no 9984 06:30:22,160 --> 06:30:28,000 is 10 greater than 11 no okay now this 9985 06:30:26,400 --> 06:30:30,558 is the first step okay just a minute 9986 06:30:28,000 --> 06:30:32,718 guys so let me erase this so our i will 9987 06:30:30,558 --> 06:30:34,638 be here and j will be here and minimum 9988 06:30:32,718 --> 06:30:37,280 index is at zero okay 9989 06:30:34,638 --> 06:30:38,558 because uh that is the first index okay 9990 06:30:37,280 --> 06:30:40,478 so since we are taking into 9991 06:30:38,558 --> 06:30:43,120 consideration the smallest element so 9992 06:30:40,478 --> 06:30:45,360 now we'll compare is 23 greater than 10 9993 06:30:43,120 --> 06:30:48,400 yes what we will do we'll update our 9994 06:30:45,360 --> 06:30:50,638 minimum that means this is now index one 9995 06:30:48,400 --> 06:30:53,440 now what we will do we'll have our index 9996 06:30:50,638 --> 06:30:56,638 updated as 1 and then what we will do we 9997 06:30:53,440 --> 06:30:59,600 compare 10 with 16 10 with 11 and 10 9998 06:30:56,638 --> 06:31:01,600 with 20. since this is our minimum index 9999 06:30:59,600 --> 06:31:04,718 after first iteration now what we will 10000 06:31:01,600 --> 06:31:06,478 do we'll swap it with the first index 10001 06:31:04,718 --> 06:31:08,798 that is this and this element will be 10002 06:31:06,478 --> 06:31:11,600 swapped so that is where we have 10 here 10003 06:31:08,798 --> 06:31:14,478 and 280 rest of the elements are 10004 06:31:11,600 --> 06:31:16,080 done so this is our first step and our i 10005 06:31:14,478 --> 06:31:19,280 was at 0 10006 06:31:16,080 --> 06:31:21,200 now our i is at 1 so this is our minimum 10007 06:31:19,280 --> 06:31:23,760 index right this is our minimum index in 10008 06:31:21,200 --> 06:31:25,680 this case now 11 10009 06:31:23,760 --> 06:31:28,320 in this case what is our minimum index 10010 06:31:25,680 --> 06:31:31,840 that is doing one right so main is here 10011 06:31:28,320 --> 06:31:34,638 and 23. now 23 will be compared it is le 10012 06:31:31,840 --> 06:31:36,798 greater than 60 so this will be our main 10013 06:31:34,638 --> 06:31:37,920 again 11 will be compared so this is our 10014 06:31:36,798 --> 06:31:40,240 main 10015 06:31:37,920 --> 06:31:42,160 now 11 will be compared with 20 so this 10016 06:31:40,240 --> 06:31:44,400 is not our minimum element now this is 10017 06:31:42,160 --> 06:31:47,600 our minimum element and it will be 10018 06:31:44,400 --> 06:31:50,478 swapped with 23. so 23 and 11 will be 10019 06:31:47,600 --> 06:31:53,520 swept so that is what you see here so 10 10020 06:31:50,478 --> 06:31:55,840 11 and 23 is swapped with this so 23 10021 06:31:53,520 --> 06:31:57,680 here and 11 here right so 23 will be 10022 06:31:55,840 --> 06:32:02,320 here and rest of the elements will be as 10023 06:31:57,680 --> 06:32:03,920 it is so this is after iteration number 10024 06:32:02,320 --> 06:32:06,160 now what happens in iteration number 10025 06:32:03,920 --> 06:32:07,920 three 10026 06:32:06,160 --> 06:32:09,120 so we have 0 10027 06:32:07,920 --> 06:32:11,280 11 10028 06:32:09,120 --> 06:32:14,718 at its original position right now let's 10029 06:32:11,280 --> 06:32:17,040 swap back and let's erase this 10030 06:32:14,718 --> 06:32:19,440 so now what happens so this is our 10031 06:32:17,040 --> 06:32:22,000 minimum index right and now 16 will be 10032 06:32:19,440 --> 06:32:23,040 compared with 23 nothing will happen 23 10033 06:32:22,000 --> 06:32:25,120 will become 10034 06:32:23,040 --> 06:32:27,520 16 will be compared with 20 nothing will 10035 06:32:25,120 --> 06:32:29,920 happen so this is our minimum index and 10036 06:32:27,520 --> 06:32:32,000 this is the ith index where we will be 10037 06:32:29,920 --> 06:32:34,240 swapping it so 16 will be swapped with 10038 06:32:32,000 --> 06:32:38,160 itself and rest of the elements will be 10039 06:32:34,240 --> 06:32:40,638 as it is so you will see 16 and then 23 10040 06:32:38,160 --> 06:32:43,120 as it is and 20 as it is considering 10041 06:32:40,638 --> 06:32:45,040 this iteration number one okay so after 10042 06:32:43,120 --> 06:32:47,200 iteration number three 10043 06:32:45,040 --> 06:32:49,920 so after iteration number three this is 10044 06:32:47,200 --> 06:32:52,798 our input array so similarly after third 10045 06:32:49,920 --> 06:32:54,718 iteration this thing will happen as 23 10046 06:32:52,798 --> 06:32:56,840 is greater than 20 so these elements 10047 06:32:54,718 --> 06:33:00,240 will be swapped and in the four 10048 06:32:56,840 --> 06:33:02,798 iteration is the required as last 10049 06:33:00,240 --> 06:33:05,520 element is already sorted okay so no 10050 06:33:02,798 --> 06:33:07,040 fourth iteration is required as the last 10051 06:33:05,520 --> 06:33:09,200 element that is there which is already 10052 06:33:07,040 --> 06:33:12,798 sorted okay so this is the whole 10053 06:33:09,200 --> 06:33:14,958 demonstration of this selection sort 10054 06:33:12,798 --> 06:33:17,520 algorithm after learning what is 10055 06:33:14,958 --> 06:33:19,360 selection sort let's quickly hop into 10056 06:33:17,520 --> 06:33:21,840 the implementation part we are 10057 06:33:19,360 --> 06:33:24,080 implementing selection sort in python on 10058 06:33:21,840 --> 06:33:27,840 google collab so let's quickly switch to 10059 06:33:24,080 --> 06:33:29,440 google collab ide so here as you can see 10060 06:33:27,840 --> 06:33:31,440 i have just briefed with the simple 10061 06:33:29,440 --> 06:33:33,920 steps which is easy for people to 10062 06:33:31,440 --> 06:33:36,000 understand so step one what we are doing 10063 06:33:33,920 --> 06:33:38,320 in this particular coding is we are 10064 06:33:36,000 --> 06:33:40,638 declaring a variable called min so that 10065 06:33:38,320 --> 06:33:43,920 is minimum that is always located in the 10066 06:33:40,638 --> 06:33:46,080 location 0 of an array right so that 10067 06:33:43,920 --> 06:33:48,478 first particular element in the array is 10068 06:33:46,080 --> 06:33:50,878 considered as a minimum element 10069 06:33:48,478 --> 06:33:52,958 that particular element is compared with 10070 06:33:50,878 --> 06:33:55,680 all the other elements left over in the 10071 06:33:52,958 --> 06:33:58,000 array right any element which is found 10072 06:33:55,680 --> 06:34:00,558 which is lesser than the minimum element 10073 06:33:58,000 --> 06:34:04,400 then we swap the places of 10074 06:34:00,558 --> 06:34:07,440 and the values of minimum with the found 10075 06:34:04,400 --> 06:34:09,200 element right so we always consider the 10076 06:34:07,440 --> 06:34:11,520 array should have the first place should 10077 06:34:09,200 --> 06:34:13,840 have the smallest value so this is how 10078 06:34:11,520 --> 06:34:16,320 generally selection sort will work 10079 06:34:13,840 --> 06:34:19,040 right so after doing this process one 10080 06:34:16,320 --> 06:34:20,638 swap in order to continue the iteration 10081 06:34:19,040 --> 06:34:23,520 we'll always 10082 06:34:20,638 --> 06:34:27,760 put min value plus 1 that means the 10083 06:34:23,520 --> 06:34:29,440 variable minimum will be added by 1 that 10084 06:34:27,760 --> 06:34:32,878 transfers the 10085 06:34:29,440 --> 06:34:35,040 place of 0 to 1 in the index of the 10086 06:34:32,878 --> 06:34:37,280 array right so the second element will 10087 06:34:35,040 --> 06:34:39,120 be considered as minimum that second 10088 06:34:37,280 --> 06:34:40,878 element will be compared with all the 10089 06:34:39,120 --> 06:34:42,638 other leftover elements in the array 10090 06:34:40,878 --> 06:34:46,320 except the first one which is already 10091 06:34:42,638 --> 06:34:48,400 considered as a smallest number so once 10092 06:34:46,320 --> 06:34:50,400 it is done the same iteration will be 10093 06:34:48,400 --> 06:34:53,440 proceeding until unless all the elements 10094 06:34:50,400 --> 06:34:55,520 in the array has been sorted right this 10095 06:34:53,440 --> 06:34:57,760 is how selection sort will work so the 10096 06:34:55,520 --> 06:34:59,600 same thing is implemented with the help 10097 06:34:57,760 --> 06:35:01,040 of python 10098 06:34:59,600 --> 06:35:04,798 if you quickly see this particular 10099 06:35:01,040 --> 06:35:06,558 program so here selection sort is the 10100 06:35:04,798 --> 06:35:09,120 name of the function which is being 10101 06:35:06,558 --> 06:35:11,760 given in order to keep it relevant then 10102 06:35:09,120 --> 06:35:14,240 you have an array and size of an array 10103 06:35:11,760 --> 06:35:17,200 what happens is minimum number that is 10104 06:35:14,240 --> 06:35:19,440 minimum variable will be compared with 10105 06:35:17,200 --> 06:35:22,718 the other elements of the array with the 10106 06:35:19,440 --> 06:35:25,680 help of the for loop every step it will 10107 06:35:22,718 --> 06:35:28,558 jump every index it will jump array 0 10108 06:35:25,680 --> 06:35:32,000 array 1 array 2 ra 3 up to the elements 10109 06:35:28,558 --> 06:35:34,718 here if you could see the data is having 10110 06:35:32,000 --> 06:35:36,240 five elements that means the array value 10111 06:35:34,718 --> 06:35:38,958 starts from zero 10112 06:35:36,240 --> 06:35:41,040 up to four we have right that particular 10113 06:35:38,958 --> 06:35:43,840 iterations will be done at the first 10114 06:35:41,040 --> 06:35:46,240 part if it is not finding any element 10115 06:35:43,840 --> 06:35:48,558 which is minimum then it will consider 10116 06:35:46,240 --> 06:35:51,280 the first element itself as a minimum 10117 06:35:48,558 --> 06:35:54,878 then what happens we will just 10118 06:35:51,280 --> 06:35:56,958 put the minimum value iteration plus 1 10119 06:35:54,878 --> 06:35:58,558 that means it is going to the next one 10120 06:35:56,958 --> 06:36:01,840 and checking for the 10121 06:35:58,558 --> 06:36:04,320 same kinds of in similar way and it is 10122 06:36:01,840 --> 06:36:05,920 also considering whether the minimum 10123 06:36:04,320 --> 06:36:08,160 element is 10124 06:36:05,920 --> 06:36:10,320 smaller than any other element or it is 10125 06:36:08,160 --> 06:36:11,520 greater than so accordingly it will try 10126 06:36:10,320 --> 06:36:14,478 to 10127 06:36:11,520 --> 06:36:15,920 find out and then it will quickly swap 10128 06:36:14,478 --> 06:36:18,080 again and continue with the third 10129 06:36:15,920 --> 06:36:20,160 element so these things will happen with 10130 06:36:18,080 --> 06:36:22,558 the help of this particular function 10131 06:36:20,160 --> 06:36:24,958 having two fonts and it will always i 10132 06:36:22,558 --> 06:36:27,680 told you compare with the help of if 10133 06:36:24,958 --> 06:36:29,680 condition here if you want that in 10134 06:36:27,680 --> 06:36:30,718 descending order you should just change 10135 06:36:29,680 --> 06:36:33,200 the 10136 06:36:30,718 --> 06:36:34,798 value here that is lesser than you have 10137 06:36:33,200 --> 06:36:37,920 you can make it as greater than it will 10138 06:36:34,798 --> 06:36:40,958 sort it in a descending order as well 10139 06:36:37,920 --> 06:36:43,360 right so this particular 10140 06:36:40,958 --> 06:36:45,520 step you have to notice so why do we use 10141 06:36:43,360 --> 06:36:48,080 this is because in order to 10142 06:36:45,520 --> 06:36:50,958 swap the minimum number to the correct 10143 06:36:48,080 --> 06:36:52,878 position right so whatever the position 10144 06:36:50,958 --> 06:36:55,120 has been found to be the correct it is 10145 06:36:52,878 --> 06:36:56,400 formed with the help of this particular 10146 06:36:55,120 --> 06:36:58,558 line of code 10147 06:36:56,400 --> 06:37:01,440 after that you could find you have a set 10148 06:36:58,558 --> 06:37:04,240 of data with five elements including the 10149 06:37:01,440 --> 06:37:06,160 minus value as well and it will try to 10150 06:37:04,240 --> 06:37:08,080 find the length of the 10151 06:37:06,160 --> 06:37:10,400 data what is the length is nothing but 10152 06:37:08,080 --> 06:37:12,638 size how many elements are there 10153 06:37:10,400 --> 06:37:14,718 accordingly it will sort science is very 10154 06:37:12,638 --> 06:37:16,320 important in order to compare between 10155 06:37:14,718 --> 06:37:19,200 all the elements it should have an end 10156 06:37:16,320 --> 06:37:22,400 value it cannot be infinity right so 10157 06:37:19,200 --> 06:37:26,320 immediately let's check out how do we 10158 06:37:22,400 --> 06:37:26,320 sort using selection sort 10159 06:37:27,200 --> 06:37:31,120 so here you could see 10160 06:37:28,958 --> 06:37:33,280 sorted array using selection sort is 10161 06:37:31,120 --> 06:37:37,120 done in ascending order starting from 10162 06:37:33,280 --> 06:37:39,520 minus 20 12 19 20 to 45 so this is how 10163 06:37:37,120 --> 06:37:41,840 the ascending order sorting by using 10164 06:37:39,520 --> 06:37:43,440 selection sort is done so let's quickly 10165 06:37:41,840 --> 06:37:46,000 have a check for descending order 10166 06:37:43,440 --> 06:37:48,478 immediately now 10167 06:37:46,000 --> 06:37:50,558 just have to change this if condition it 10168 06:37:48,478 --> 06:37:55,040 is greater than you will get descending 10169 06:37:50,558 --> 06:37:55,040 order i'm just changing here as well 10170 06:37:59,120 --> 06:38:02,558 right 10171 06:38:00,000 --> 06:38:04,718 so the change has been made here and 10172 06:38:02,558 --> 06:38:07,718 let's quickly run the program 10173 06:38:04,718 --> 06:38:07,718 now 10174 06:38:08,240 --> 06:38:13,040 right see it is sorting in descending 10175 06:38:10,958 --> 06:38:15,520 order that means greatest number first 10176 06:38:13,040 --> 06:38:18,478 and the followed by the smaller numbers 10177 06:38:15,520 --> 06:38:21,200 right greatest to smaller so this is how 10178 06:38:18,478 --> 06:38:22,160 selection sort will work in python now 10179 06:38:21,200 --> 06:38:25,040 let's see 10180 06:38:22,160 --> 06:38:27,440 the time complexity of selection sort 10181 06:38:25,040 --> 06:38:31,360 now if you talk about worst case that is 10182 06:38:27,440 --> 06:38:33,440 if the array is unsorted or it is 10183 06:38:31,360 --> 06:38:35,920 we are trying to sort an area which is 10184 06:38:33,440 --> 06:38:37,200 reversed in nature okay so let's suppose 10185 06:38:35,920 --> 06:38:39,120 we are trying to sort they are in 10186 06:38:37,200 --> 06:38:41,600 ascending order and we get the 10187 06:38:39,120 --> 06:38:44,478 a in ascending order or descending order 10188 06:38:41,600 --> 06:38:47,280 okay so that is 10 9 8 10189 06:38:44,478 --> 06:38:50,320 7 and 6. so this is the worst case right 10190 06:38:47,280 --> 06:38:53,760 and now what happens in this case 10191 06:38:50,320 --> 06:38:55,840 every index the minimum index will be 10192 06:38:53,760 --> 06:38:57,840 updated for every 10193 06:38:55,840 --> 06:38:59,680 element okay so 10 will be compared with 10194 06:38:57,840 --> 06:39:02,160 nine so minimum index will change from 10195 06:38:59,680 --> 06:39:04,558 zero to one then zero two 3 and so on 10196 06:39:02,160 --> 06:39:06,478 okay so find the minimum index that is 10197 06:39:04,558 --> 06:39:08,718 this element so this is the worst case 10198 06:39:06,478 --> 06:39:10,718 now what happens we have the outer loop 10199 06:39:08,718 --> 06:39:12,718 that is working from 0 to n minus 1 and 10200 06:39:10,718 --> 06:39:16,400 the inner loop that is working from 10201 06:39:12,718 --> 06:39:18,558 z from j plus 1 to the size so in any 10202 06:39:16,400 --> 06:39:20,638 case whether we have this 10203 06:39:18,558 --> 06:39:23,360 unsorted array that is here 10204 06:39:20,638 --> 06:39:25,440 we will still be traversing because n 10205 06:39:23,360 --> 06:39:28,240 square times because we have these two 10206 06:39:25,440 --> 06:39:30,160 for nested for loops which are working 10207 06:39:28,240 --> 06:39:32,000 up to the size of the area 10208 06:39:30,160 --> 06:39:34,718 done and same thing happens in the 10209 06:39:32,000 --> 06:39:37,120 average case as well whether we have 10210 06:39:34,718 --> 06:39:38,878 some elements in the sorted format let's 10211 06:39:37,120 --> 06:39:41,200 suppose we have half of the array in 10212 06:39:38,878 --> 06:39:44,400 this way and then the rest of the 10213 06:39:41,200 --> 06:39:47,280 elements are unsorted still we will not 10214 06:39:44,400 --> 06:39:50,320 update our minimum index but rest of the 10215 06:39:47,280 --> 06:39:53,360 elements will change for the uh not for 10216 06:39:50,320 --> 06:39:56,080 these three but for the rest our minimum 10217 06:39:53,360 --> 06:39:58,798 index will keep on updating okay so it 10218 06:39:56,080 --> 06:40:00,718 doesn't matter so average case type if 10219 06:39:58,798 --> 06:40:03,840 you talk about every time complexity it 10220 06:40:00,718 --> 06:40:07,600 will also be n square but what happens 10221 06:40:03,840 --> 06:40:10,320 if our array is still sorted right 10222 06:40:07,600 --> 06:40:13,840 or if our array that is the array given 10223 06:40:10,320 --> 06:40:16,320 is already in sorted format will 10224 06:40:13,840 --> 06:40:19,520 there be if we modify our bubble sort we 10225 06:40:16,320 --> 06:40:22,320 can get this down to below of n but this 10226 06:40:19,520 --> 06:40:24,558 is not the case in selection sort still 10227 06:40:22,320 --> 06:40:27,600 we will be comparing in the outer loop 10228 06:40:24,558 --> 06:40:30,160 from 0 to n minus 1 or size minus 1 and 10229 06:40:27,600 --> 06:40:32,558 in the loop will be again going from j 10230 06:40:30,160 --> 06:40:35,040 plus 1 to less than size 10231 06:40:32,558 --> 06:40:35,840 without even if we are not going in this 10232 06:40:35,040 --> 06:40:37,920 for 10233 06:40:35,840 --> 06:40:39,360 in this if condition that is the 10234 06:40:37,920 --> 06:40:42,080 comparison condition will not be 10235 06:40:39,360 --> 06:40:44,878 executed and the minimum index will be 10236 06:40:42,080 --> 06:40:47,440 as it is still we are going through this 10237 06:40:44,878 --> 06:40:49,680 for loop okay whether we are not even if 10238 06:40:47,440 --> 06:40:52,080 we are not executing this a block okay 10239 06:40:49,680 --> 06:40:54,558 it doesn't matter still we are going 10240 06:40:52,080 --> 06:40:59,120 the n square time complexity will be 10241 06:40:54,558 --> 06:41:01,600 there even in the best case as well okay 10242 06:40:59,120 --> 06:41:04,320 so this is all about the time complexity 10243 06:41:01,600 --> 06:41:06,798 of selection sort now let's talk about 10244 06:41:04,320 --> 06:41:09,200 the space complexity of 10245 06:41:06,798 --> 06:41:11,600 selection sort since we are not using 10246 06:41:09,200 --> 06:41:14,240 any auxiliary memory so auxiliary memory 10247 06:41:11,600 --> 06:41:18,000 can be in the form of arrays linked less 10248 06:41:14,240 --> 06:41:20,558 stack hues or even strings since we are 10249 06:41:18,000 --> 06:41:23,280 not using any of these extra memories in 10250 06:41:20,558 --> 06:41:25,680 order to sort our array so the reason 10251 06:41:23,280 --> 06:41:28,320 for that is because we are doing this 10252 06:41:25,680 --> 06:41:30,798 within the array that is provided to us 10253 06:41:28,320 --> 06:41:32,718 right so the space complexity is 10254 06:41:30,798 --> 06:41:36,080 constant that means 10255 06:41:32,718 --> 06:41:39,040 one as we are not using any auxiliary 10256 06:41:36,080 --> 06:41:42,320 memory now let's talk about selection 10257 06:41:39,040 --> 06:41:44,160 sort analysis if you observe carefully 10258 06:41:42,320 --> 06:41:45,680 in this array let's take an example 10259 06:41:44,160 --> 06:41:46,718 first 10 10260 06:41:45,680 --> 06:41:49,360 9 10261 06:41:46,718 --> 06:41:52,878 8 7 6 10262 06:41:49,360 --> 06:41:52,878 okay let me put a 6 here 10263 06:41:54,240 --> 06:41:59,520 so this is our array right and in this 10264 06:41:57,440 --> 06:42:01,280 array what we are trying to do we are 10265 06:41:59,520 --> 06:42:04,638 trying to 10266 06:42:01,280 --> 06:42:06,798 arrange it in an ascending order so now 10267 06:42:04,638 --> 06:42:08,320 if you remember the algorithm the outer 10268 06:42:06,798 --> 06:42:11,920 loop work from 10269 06:42:08,320 --> 06:42:14,240 0 to n minus 1 and then inner loop work 10270 06:42:11,920 --> 06:42:14,240 from 10271 06:42:14,320 --> 06:42:19,120 that means 0 plus 1 that is let's 10272 06:42:16,558 --> 06:42:22,080 suppose if this is an iterator if we use 10273 06:42:19,120 --> 06:42:24,240 in outer loop we used i and then in in a 10274 06:42:22,080 --> 06:42:27,200 loop we use j and j 10275 06:42:24,240 --> 06:42:31,040 work from i plus one to 10276 06:42:27,200 --> 06:42:31,040 size that is it right 10277 06:42:31,520 --> 06:42:36,798 now and then inside this we used to 10278 06:42:33,840 --> 06:42:40,478 compare so now in worst case 10279 06:42:36,798 --> 06:42:42,638 every element was compared right j was 10280 06:42:40,478 --> 06:42:44,000 compared with i plus one all right our i 10281 06:42:42,638 --> 06:42:46,878 was compared with 10282 06:42:44,000 --> 06:42:48,878 j plus one so it was compared with this 10283 06:42:46,878 --> 06:42:49,920 this this and this 10284 06:42:48,878 --> 06:42:52,400 and we 10285 06:42:49,920 --> 06:42:55,440 we're selecting the minimum element in 10286 06:42:52,400 --> 06:42:57,520 the array the minimum index rather we 10287 06:42:55,440 --> 06:43:00,320 were selecting the minimum index in the 10288 06:42:57,520 --> 06:43:02,958 entire array so this was working for n 10289 06:43:00,320 --> 06:43:05,760 times and this work from big o of n 10290 06:43:02,958 --> 06:43:08,478 times right and in worst case 10291 06:43:05,760 --> 06:43:12,240 in worst case the number of comparisons 10292 06:43:08,478 --> 06:43:15,120 required will be before n square because 10293 06:43:12,240 --> 06:43:18,080 we are trying to find the minimum index 10294 06:43:15,120 --> 06:43:20,878 in the entire area right and once we 10295 06:43:18,080 --> 06:43:22,718 have that minimum index and later on 10296 06:43:20,878 --> 06:43:25,600 we'll swap them right 10297 06:43:22,718 --> 06:43:28,080 worst case it will go from 0 to n that 10298 06:43:25,600 --> 06:43:30,958 means big o of n and the inner loop will 10299 06:43:28,080 --> 06:43:34,000 also go from big o of n 10300 06:43:30,958 --> 06:43:36,080 that means 0 to n so in worst case since 10301 06:43:34,000 --> 06:43:38,798 these are nested for loops so the worst 10302 06:43:36,080 --> 06:43:41,440 case will be because n square 10303 06:43:38,798 --> 06:43:42,718 and for comparing them we'll always 10304 06:43:41,440 --> 06:43:45,040 compare 10305 06:43:42,718 --> 06:43:47,440 all the elements and it will be again it 10306 06:43:45,040 --> 06:43:49,440 will boil it will boil down to what we 10307 06:43:47,440 --> 06:43:51,120 call n square when we talk about number 10308 06:43:49,440 --> 06:43:53,280 of comparisons 10309 06:43:51,120 --> 06:43:55,680 and what happens when we are trying to 10310 06:43:53,280 --> 06:43:58,400 swap them since we have this minimum 10311 06:43:55,680 --> 06:44:01,440 index that is there only one swap was 10312 06:43:58,400 --> 06:44:04,000 required that was outside this 10313 06:44:01,440 --> 06:44:06,478 inner for loop right 10314 06:44:04,000 --> 06:44:09,200 so we have this in for loop and here we 10315 06:44:06,478 --> 06:44:11,520 were doing the swapping inverse is the 10316 06:44:09,200 --> 06:44:12,958 number of slabs that are required is big 10317 06:44:11,520 --> 06:44:15,040 o of n 10318 06:44:12,958 --> 06:44:17,920 okay in worst case that means we were 10319 06:44:15,040 --> 06:44:21,120 swapping only single time and it will be 10320 06:44:17,920 --> 06:44:23,760 the case right that in worst case that 10321 06:44:21,120 --> 06:44:26,000 all the elements right at the end will 10322 06:44:23,760 --> 06:44:29,920 be swapped so we will have at the end 10323 06:44:26,000 --> 06:44:32,878 what five six seven eight and nine right 10324 06:44:29,920 --> 06:44:35,280 and we will swap them only once in the 10325 06:44:32,878 --> 06:44:37,520 entire area that is once for every 10326 06:44:35,280 --> 06:44:38,958 element so in worst case it will be pick 10327 06:44:37,520 --> 06:44:41,200 off n square 10328 06:44:38,958 --> 06:44:44,080 now what about stability if you talk 10329 06:44:41,200 --> 06:44:45,760 about stable algorithm it means that if 10330 06:44:44,080 --> 06:44:47,440 you have this array let's take an 10331 06:44:45,760 --> 06:44:51,200 example it is 10 10332 06:44:47,440 --> 06:44:53,920 8 9 and 10 again and then 6. if you talk 10333 06:44:51,200 --> 06:44:56,478 about this the sleep stability will boil 10334 06:44:53,920 --> 06:44:59,680 down to two things the relative position 10335 06:44:56,478 --> 06:45:02,958 of these two elements 10 and 10 dash now 10336 06:44:59,680 --> 06:45:04,638 if we sort this it can be six 10337 06:45:02,958 --> 06:45:07,680 it will be rather 10338 06:45:04,638 --> 06:45:08,878 it will be 6 8 9 10 and 10 10339 06:45:07,680 --> 06:45:11,600 dash right 10340 06:45:08,878 --> 06:45:13,920 so it will be 10 and 10 but uh just to 10341 06:45:11,600 --> 06:45:16,000 separate these two tens we are taking 10342 06:45:13,920 --> 06:45:18,160 this we are taking as an example we are 10343 06:45:16,000 --> 06:45:20,958 taking this assuming this 10 to be 10 10344 06:45:18,160 --> 06:45:24,478 and this tends to be 10 dash and it can 10345 06:45:20,958 --> 06:45:27,200 also be that it will be 8 9 and then 10 10346 06:45:24,478 --> 06:45:29,280 and 10 dash and 10 so both of these are 10347 06:45:27,200 --> 06:45:31,840 sorted right but 10348 06:45:29,280 --> 06:45:34,638 if you talk about stability this is a 10349 06:45:31,840 --> 06:45:37,600 stable algorithm why because the 10350 06:45:34,638 --> 06:45:39,760 relative position of this 10 and this 10 10351 06:45:37,600 --> 06:45:41,600 is intact and this is an unstable 10352 06:45:39,760 --> 06:45:44,160 algorithm 10353 06:45:41,600 --> 06:45:46,558 now when we compare 10354 06:45:44,160 --> 06:45:49,920 or when we do the swapping in 10355 06:45:46,558 --> 06:45:52,240 selection sort it is not guaranteed that 10356 06:45:49,920 --> 06:45:53,920 it will be stable right and 10357 06:45:52,240 --> 06:45:55,840 the elements can be 10358 06:45:53,920 --> 06:45:58,320 unstable that means we will the 10359 06:45:55,840 --> 06:45:59,920 resultant array will be in this format 10360 06:45:58,320 --> 06:46:03,440 that 10 10361 06:45:59,920 --> 06:46:05,440 may be at first and then we have 10 so 10362 06:46:03,440 --> 06:46:08,478 it is not guaranteed that it will be 10363 06:46:05,440 --> 06:46:11,040 always stable but yes we can make it 10364 06:46:08,478 --> 06:46:12,798 stable by modifying our selection sort 10365 06:46:11,040 --> 06:46:14,958 but if we talk about traditional 10366 06:46:12,798 --> 06:46:17,120 selection sort it is an unstable 10367 06:46:14,958 --> 06:46:20,320 algorithm right 10368 06:46:17,120 --> 06:46:23,520 now what about the 10369 06:46:20,320 --> 06:46:26,718 in place that means are we using any 10370 06:46:23,520 --> 06:46:29,120 extra memory or extra space 10371 06:46:26,718 --> 06:46:30,240 no we are not using any extra space 10372 06:46:29,120 --> 06:46:32,400 right 10373 06:46:30,240 --> 06:46:35,120 extra space can be a link list it can be 10374 06:46:32,400 --> 06:46:37,920 an array it can be a stack it can be a 10375 06:46:35,120 --> 06:46:40,958 cube right we are not using any of these 10376 06:46:37,920 --> 06:46:44,718 extra spaces so that is for the same 10377 06:46:40,958 --> 06:46:48,000 reason this selection sort algorithm is 10378 06:46:44,718 --> 06:46:49,920 an in place algorithm right because we 10379 06:46:48,000 --> 06:46:52,160 are swapping our elements and we are 10380 06:46:49,920 --> 06:46:54,240 comparing them within the given array 10381 06:46:52,160 --> 06:46:57,280 right we are not using any extra memory 10382 06:46:54,240 --> 06:47:00,000 right so for that reason this is an in 10383 06:46:57,280 --> 06:47:02,240 place algorithm let's try to reiterate 10384 06:47:00,000 --> 06:47:03,760 what we learned so far 10385 06:47:02,240 --> 06:47:06,558 so if we talk about number of 10386 06:47:03,760 --> 06:47:08,878 comparisons inverse is it can go to big 10387 06:47:06,558 --> 06:47:10,400 o of n square in terms of number of 10388 06:47:08,878 --> 06:47:14,958 swaps it is 10389 06:47:10,400 --> 06:47:18,000 big o of n it is an unstable algorithm 10390 06:47:14,958 --> 06:47:20,080 and it is an inplacement 10391 06:47:18,000 --> 06:47:22,000 now before we understand what is quake 10392 06:47:20,080 --> 06:47:24,718 sort let's try to understand what is 10393 06:47:22,000 --> 06:47:26,798 sorting and why do we require sorting so 10394 06:47:24,718 --> 06:47:27,600 sorting is a mechanism wherein we will 10395 06:47:26,798 --> 06:47:30,558 be 10396 06:47:27,600 --> 06:47:33,360 sorting or arranging our data either in 10397 06:47:30,558 --> 06:47:35,760 ascending order or in descending order 10398 06:47:33,360 --> 06:47:37,520 right so let's suppose you have a 10399 06:47:35,760 --> 06:47:39,040 students you have 10 students and all 10400 06:47:37,520 --> 06:47:41,920 those students have 10401 06:47:39,040 --> 06:47:44,320 roll numbers allocated from 1 to 100 and 10402 06:47:41,920 --> 06:47:47,040 you want to know which role numbers are 10403 06:47:44,320 --> 06:47:49,840 present and which are absent and which 10404 06:47:47,040 --> 06:47:51,760 have left the college or school right so 10405 06:47:49,840 --> 06:47:53,840 in that scenario you can easily 10406 06:47:51,760 --> 06:47:57,440 implement sorting right and you can 10407 06:47:53,840 --> 06:47:59,760 understand when you have that sorting 10408 06:47:57,440 --> 06:48:01,600 arrangement in place you can easily 10409 06:47:59,760 --> 06:48:03,360 detect which 10410 06:48:01,600 --> 06:48:07,280 elements or which 10411 06:48:03,360 --> 06:48:10,000 students are absent or not right so 10412 06:48:07,280 --> 06:48:11,680 herein you can use sorting so in this 10413 06:48:10,000 --> 06:48:14,000 tutorial we are going to understand 10414 06:48:11,680 --> 06:48:17,120 quick sort equalism it is one of the 10415 06:48:14,000 --> 06:48:20,478 most widely used algorithm it follows a 10416 06:48:17,120 --> 06:48:23,360 paradigm of divide and conquer what do 10417 06:48:20,478 --> 06:48:26,160 you mean by divide and conquer basically 10418 06:48:23,360 --> 06:48:29,440 we will be dividing our array in such a 10419 06:48:26,160 --> 06:48:31,040 way that every time we will be dividing 10420 06:48:29,440 --> 06:48:32,878 let's suppose this is an array and now 10421 06:48:31,040 --> 06:48:35,120 we will be dividing into two 10422 06:48:32,878 --> 06:48:36,958 then further we'll divide it into two 10423 06:48:35,120 --> 06:48:39,520 then further we will divide it into two 10424 06:48:36,958 --> 06:48:42,000 and so on right so 10425 06:48:39,520 --> 06:48:43,840 we'll see in the algorithm part how we 10426 06:48:42,000 --> 06:48:46,478 can implement this 10427 06:48:43,840 --> 06:48:48,798 divide and conquer paradigm and in this 10428 06:48:46,478 --> 06:48:52,080 tutorial we will be implementing this 10429 06:48:48,798 --> 06:48:55,120 quick sort using recursion we'll see how 10430 06:48:52,080 --> 06:48:57,600 we will recursively call those functions 10431 06:48:55,120 --> 06:49:00,320 based on some pivot element now in this 10432 06:48:57,600 --> 06:49:02,000 recursive call we'll choose a pivot 10433 06:49:00,320 --> 06:49:04,400 element let's suppose you have this 10434 06:49:02,000 --> 06:49:06,320 array and we're choosing this element as 10435 06:49:04,400 --> 06:49:09,120 pivot obviously you can choose any 10436 06:49:06,320 --> 06:49:11,920 element as pivot right so it can be 10437 06:49:09,120 --> 06:49:15,040 first element it can be uh last element 10438 06:49:11,920 --> 06:49:18,000 it can be any random element but once we 10439 06:49:15,040 --> 06:49:19,600 have chosen those that pivot now what we 10440 06:49:18,000 --> 06:49:22,558 will do 10441 06:49:19,600 --> 06:49:24,558 in each iteration right in quick sort 10442 06:49:22,558 --> 06:49:27,280 what happens in each iteration this 10443 06:49:24,558 --> 06:49:29,120 pivot will have its original position 10444 06:49:27,280 --> 06:49:31,760 that means this will be the position in 10445 06:49:29,120 --> 06:49:34,000 the original array as well let's suppose 10446 06:49:31,760 --> 06:49:35,920 this is our pivot now this pivot will 10447 06:49:34,000 --> 06:49:38,638 have its original position after one 10448 06:49:35,920 --> 06:49:41,280 iteration after that iteration is over 10449 06:49:38,638 --> 06:49:42,718 and all the elements that are less than 10450 06:49:41,280 --> 06:49:44,558 this pivot 10451 06:49:42,718 --> 06:49:46,080 are on the left hand side and all the 10452 06:49:44,558 --> 06:49:48,558 elements that are greater than will be 10453 06:49:46,080 --> 06:49:50,558 on the right hand side now 10454 06:49:48,558 --> 06:49:52,160 then we will be choosing another pivot 10455 06:49:50,558 --> 06:49:54,718 now what are those pivots we'll see in 10456 06:49:52,160 --> 06:49:57,120 the algorithm move uh more extensively 10457 06:49:54,718 --> 06:49:58,798 what uh how we can choose that pivot now 10458 06:49:57,120 --> 06:50:01,680 let's suppose we chosen that we chose 10459 06:49:58,798 --> 06:50:04,160 this pivot and this pivot is here and 10460 06:50:01,680 --> 06:50:06,638 after the second iteration what happens 10461 06:50:04,160 --> 06:50:08,478 this pivot this will be our next pivot 10462 06:50:06,638 --> 06:50:10,718 and this will be our next pivot now we 10463 06:50:08,478 --> 06:50:12,958 will be having two pivots so this is how 10464 06:50:10,718 --> 06:50:16,320 we induce that we are 10465 06:50:12,958 --> 06:50:19,440 implementing divide and conquer approach 10466 06:50:16,320 --> 06:50:21,680 okay with each step our problem gets 10467 06:50:19,440 --> 06:50:23,840 reduced to two which leads to quick 10468 06:50:21,680 --> 06:50:26,558 sorting quick sort right or quick 10469 06:50:23,840 --> 06:50:28,798 sorting algorithm okay so now we will be 10470 06:50:26,558 --> 06:50:30,558 dealing with this sub array and we'll be 10471 06:50:28,798 --> 06:50:32,478 dealing with this sub array and now 10472 06:50:30,558 --> 06:50:34,240 we'll be implementing the same procedure 10473 06:50:32,478 --> 06:50:37,040 on this sub array that means this is the 10474 06:50:34,240 --> 06:50:38,718 pivot and this is the pivot right 10475 06:50:37,040 --> 06:50:42,400 now let's try to understand the 10476 06:50:38,718 --> 06:50:43,600 algorithm of quake sort so now we have 10477 06:50:42,400 --> 06:50:46,000 this 10478 06:50:43,600 --> 06:50:48,798 first of the method that is there that 10479 06:50:46,000 --> 06:50:51,440 is known as quick sort in which we will 10480 06:50:48,798 --> 06:50:54,240 be calling this quick sort recursively 10481 06:50:51,440 --> 06:50:56,400 again and again but first time around 10482 06:50:54,240 --> 06:50:58,240 what happens we will check okay now we 10483 06:50:56,400 --> 06:50:59,920 have this array always we will check 10484 06:50:58,240 --> 06:51:02,320 beginning should be less than end 10485 06:50:59,920 --> 06:51:04,558 because that way we can keep the track 10486 06:51:02,320 --> 06:51:06,638 of things that okay this is the part 10487 06:51:04,558 --> 06:51:09,280 that is already sorted and this is the 10488 06:51:06,638 --> 06:51:11,200 part that is unsorted right and now we 10489 06:51:09,280 --> 06:51:12,878 will be checking and after checking that 10490 06:51:11,200 --> 06:51:14,478 we will be calling this method we will 10491 06:51:12,878 --> 06:51:16,878 see what this method is we will see the 10492 06:51:14,478 --> 06:51:19,200 algorithm and we will see how this 10493 06:51:16,878 --> 06:51:21,520 partition happens and we will get the 10494 06:51:19,200 --> 06:51:24,080 index of the let's suppose we pick this 10495 06:51:21,520 --> 06:51:26,240 element as pivot and after partition 10496 06:51:24,080 --> 06:51:29,840 what happens this pivot has its original 10497 06:51:26,240 --> 06:51:31,680 position at index 3 right and 10498 06:51:29,840 --> 06:51:34,080 that will be returned and that will be 10499 06:51:31,680 --> 06:51:36,160 contained in this pivot index right and 10500 06:51:34,080 --> 06:51:38,558 now what happens now we know that this 10501 06:51:36,160 --> 06:51:40,320 is its original position in the original 10502 06:51:38,558 --> 06:51:42,638 array wherein we will get the sorted 10503 06:51:40,320 --> 06:51:44,558 array this will be its original position 10504 06:51:42,638 --> 06:51:47,280 that means this element lets the phone 10505 06:51:44,558 --> 06:51:48,958 is eight it will be at index three and 10506 06:51:47,280 --> 06:51:50,798 this will have its original position 10507 06:51:48,958 --> 06:51:52,558 after each iteration now first time 10508 06:51:50,798 --> 06:51:54,878 around what happens this partition is 10509 06:51:52,558 --> 06:51:57,360 called next time around what happens 10510 06:51:54,878 --> 06:52:01,040 this quick sort algorithm is called 10511 06:51:57,360 --> 06:52:04,080 again recursively first time on the 10512 06:52:01,040 --> 06:52:06,878 left hand side that means this portion 10513 06:52:04,080 --> 06:52:07,840 now in this portion this will be your 10514 06:52:06,878 --> 06:52:10,160 pivot 10515 06:52:07,840 --> 06:52:12,400 okay you see beginning 10516 06:52:10,160 --> 06:52:14,878 is uh we are sending the arguments as 10517 06:52:12,400 --> 06:52:17,120 beginning and pivot index minus one that 10518 06:52:14,878 --> 06:52:19,120 means we are not including this element 10519 06:52:17,120 --> 06:52:20,798 because this has been already sorted we 10520 06:52:19,120 --> 06:52:22,878 are not including this element and we 10521 06:52:20,798 --> 06:52:24,718 are calling this function on this sub 10522 06:52:22,878 --> 06:52:26,718 array again and this time around this 10523 06:52:24,718 --> 06:52:28,478 will be our pivot and same thing happens 10524 06:52:26,718 --> 06:52:30,400 similarly when we are done on the left 10525 06:52:28,478 --> 06:52:33,040 uh with the left hand side now we will 10526 06:52:30,400 --> 06:52:35,520 be moving to the right hand side that is 10527 06:52:33,040 --> 06:52:37,920 we will be implementing it on pivot 10528 06:52:35,520 --> 06:52:39,360 index plus one that means this element 10529 06:52:37,920 --> 06:52:42,080 from this element 10530 06:52:39,360 --> 06:52:44,000 that is there to the end of the array 10531 06:52:42,080 --> 06:52:46,000 and this time around this will be our 10532 06:52:44,000 --> 06:52:48,558 pivot okay 10533 06:52:46,000 --> 06:52:50,558 now with that being said this is what 10534 06:52:48,558 --> 06:52:53,200 happens when we 10535 06:52:50,558 --> 06:52:55,760 are implementing quick sort but now what 10536 06:52:53,200 --> 06:52:57,200 about this partition method let's see 10537 06:52:55,760 --> 06:52:59,920 how that happens 10538 06:52:57,200 --> 06:53:02,878 so in partition what happens we will be 10539 06:52:59,920 --> 06:53:04,320 setting up the pivot element that is 10540 06:53:02,878 --> 06:53:06,400 setting up the element which is our 10541 06:53:04,320 --> 06:53:08,240 pivot obviously you can choose any 10542 06:53:06,400 --> 06:53:10,080 element but in this tutorial what i am 10543 06:53:08,240 --> 06:53:12,080 going to use and 10544 06:53:10,080 --> 06:53:14,958 what you should try first 10545 06:53:12,080 --> 06:53:16,798 that we should try to pick pivot as the 10546 06:53:14,958 --> 06:53:17,840 last element obviously you can pick any 10547 06:53:16,798 --> 06:53:21,120 element 10548 06:53:17,840 --> 06:53:23,280 and its time complexity depends on which 10549 06:53:21,120 --> 06:53:25,760 pivot you will be choosing we'll see 10550 06:53:23,280 --> 06:53:28,080 that in the time complexity part okay 10551 06:53:25,760 --> 06:53:30,638 now we have set this pivot as the last 10552 06:53:28,080 --> 06:53:33,360 element and now what we are doing we are 10553 06:53:30,638 --> 06:53:36,000 saying that okay the pivot index this uh 10554 06:53:33,360 --> 06:53:37,760 this step refers to what 10555 06:53:36,000 --> 06:53:39,200 this is this is the index 10556 06:53:37,760 --> 06:53:41,440 from let's suppose this is the pivot 10557 06:53:39,200 --> 06:53:44,160 index and what happens this will be our 10558 06:53:41,440 --> 06:53:46,400 pivot okay what happens this pivot index 10559 06:53:44,160 --> 06:53:48,638 maintains that order okay from this 10560 06:53:46,400 --> 06:53:51,040 index from this index everything on the 10561 06:53:48,638 --> 06:53:53,040 left hand side is less than the pivot 10562 06:53:51,040 --> 06:53:55,120 and everything on the right hand side is 10563 06:53:53,040 --> 06:53:57,680 greater than the private so we'll see 10564 06:53:55,120 --> 06:54:00,478 when we we will see an example and then 10565 06:53:57,680 --> 06:54:03,520 i will show you how this pivot index is 10566 06:54:00,478 --> 06:54:05,520 very important okay now what we will do 10567 06:54:03,520 --> 06:54:07,440 obviously at start it is at this 10568 06:54:05,520 --> 06:54:10,080 position that means we are not we have 10569 06:54:07,440 --> 06:54:12,718 no such scenario wherein we have some 10570 06:54:10,080 --> 06:54:14,080 elements that are less than pivot and we 10571 06:54:12,718 --> 06:54:15,680 have some elements that are greater than 10572 06:54:14,080 --> 06:54:17,440 pivot okay 10573 06:54:15,680 --> 06:54:19,920 so now let's suppose this is our array 10574 06:54:17,440 --> 06:54:21,760 and this is our pivot right and this is 10575 06:54:19,920 --> 06:54:24,240 our p index 10576 06:54:21,760 --> 06:54:26,558 that is the index period index and it is 10577 06:54:24,240 --> 06:54:28,320 minus one right now okay now these two 10578 06:54:26,558 --> 06:54:30,160 steps are done now what happens in the 10579 06:54:28,320 --> 06:54:32,320 third step now we will iterate from 10580 06:54:30,160 --> 06:54:35,040 beginning that is this point and we will 10581 06:54:32,320 --> 06:54:37,280 check if any element is less than pivot 10582 06:54:35,040 --> 06:54:39,040 if that is the case then what we will do 10583 06:54:37,280 --> 06:54:40,638 we increment this 10584 06:54:39,040 --> 06:54:43,520 and 10585 06:54:40,638 --> 06:54:45,360 swap those elements that is the error 10586 06:54:43,520 --> 06:54:48,558 the first element and the 10587 06:54:45,360 --> 06:54:50,240 index that is present at that means now 10588 06:54:48,558 --> 06:54:52,320 if you see this step now we have 10589 06:54:50,240 --> 06:54:54,878 incremented it first right now let's 10590 06:54:52,320 --> 06:54:57,280 suppose if any element that is less than 10591 06:54:54,878 --> 06:54:59,360 pivot we first increment the pivot index 10592 06:54:57,280 --> 06:55:02,160 that means that pivot index will be here 10593 06:54:59,360 --> 06:55:04,958 and we will be swapping it with a rr of 10594 06:55:02,160 --> 06:55:07,280 i and ar of i is also at this location 10595 06:55:04,958 --> 06:55:08,958 so this element will be swap with itself 10596 06:55:07,280 --> 06:55:11,040 now you might be thinking okay so why we 10597 06:55:08,958 --> 06:55:13,120 are doing this right why we are doing 10598 06:55:11,040 --> 06:55:15,360 why we are swapping this with 10599 06:55:13,120 --> 06:55:17,760 its own uh with its own position you 10600 06:55:15,360 --> 06:55:19,600 won't get the intuition in this step but 10601 06:55:17,760 --> 06:55:21,520 in the next step you will definitely get 10602 06:55:19,600 --> 06:55:23,040 the intuition now let's suppose this is 10603 06:55:21,520 --> 06:55:25,600 the thing that happens in the for loop 10604 06:55:23,040 --> 06:55:27,120 now let's try to reiterate this now if 10605 06:55:25,600 --> 06:55:29,440 an element is 10606 06:55:27,120 --> 06:55:31,520 not less than pivot let's suppose there 10607 06:55:29,440 --> 06:55:34,080 was here we had five and here we had 10608 06:55:31,520 --> 06:55:35,520 three so it was less than and we 10609 06:55:34,080 --> 06:55:37,520 swapped it for yourself now let's 10610 06:55:35,520 --> 06:55:38,958 suppose we have this element six and it 10611 06:55:37,520 --> 06:55:41,520 is not less than 10612 06:55:38,958 --> 06:55:43,680 pivot right and what happens over here 10613 06:55:41,520 --> 06:55:46,080 so we will not be we will not execute 10614 06:55:43,680 --> 06:55:48,958 this if block right and then we'll have 10615 06:55:46,080 --> 06:55:50,320 this arr of i now i will be here now i 10616 06:55:48,958 --> 06:55:52,718 will be incrementing and this time 10617 06:55:50,320 --> 06:55:55,120 around we have 2 and p index is still 10618 06:55:52,718 --> 06:55:57,440 here right now this time around it is 10619 06:55:55,120 --> 06:56:00,080 less than 2 right and now what we will 10620 06:55:57,440 --> 06:56:02,160 do will increment first the p index it 10621 06:56:00,080 --> 06:56:05,200 will be pointing here and then what we 10622 06:56:02,160 --> 06:56:07,360 will do will swap 10623 06:56:05,200 --> 06:56:09,280 swap these two elements right these two 10624 06:56:07,360 --> 06:56:11,360 elements will be swap so now you have 10625 06:56:09,280 --> 06:56:14,400 two here and you have six over here 10626 06:56:11,360 --> 06:56:17,440 right so you see this is the reason why 10627 06:56:14,400 --> 06:56:19,520 we have this pivot index at in place and 10628 06:56:17,440 --> 06:56:20,958 why we are swapping them so in the first 10629 06:56:19,520 --> 06:56:23,360 step it was 10630 06:56:20,958 --> 06:56:25,600 uh it was that it happened 10631 06:56:23,360 --> 06:56:28,478 due to the fact that the element was 10632 06:56:25,600 --> 06:56:29,920 less than pivot and if the element would 10633 06:56:28,478 --> 06:56:32,160 have wouldn't have been less than the 10634 06:56:29,920 --> 06:56:34,160 pivot then we have incremented the i 10635 06:56:32,160 --> 06:56:35,920 pointer and p index would have remained 10636 06:56:34,160 --> 06:56:38,718 on minus one 10637 06:56:35,920 --> 06:56:40,798 now finally what happens now when once 10638 06:56:38,718 --> 06:56:42,958 the entire iteration is complete and 10639 06:56:40,798 --> 06:56:45,200 let's suppose we have eight over here 10640 06:56:42,958 --> 06:56:47,280 and we have then ten now once the 10641 06:56:45,200 --> 06:56:49,440 iteration is completed now what we will 10642 06:56:47,280 --> 06:56:51,280 do we will swap these two elements that 10643 06:56:49,440 --> 06:56:53,120 means five and six will be swapped and 10644 06:56:51,280 --> 06:56:55,120 we have five here we have six here we 10645 06:56:53,120 --> 06:56:57,280 have eight here we have ten here and we 10646 06:56:55,120 --> 06:56:58,160 have three here and we have two here so 10647 06:56:57,280 --> 06:57:00,558 you see 10648 06:56:58,160 --> 06:57:02,478 after one iteration all the elements 10649 06:57:00,558 --> 06:57:04,638 that are less than pivot will be on the 10650 06:57:02,478 --> 06:57:06,080 left hand side and all the elements that 10651 06:57:04,638 --> 06:57:07,440 are greater than will be on the right 10652 06:57:06,080 --> 06:57:10,000 hand side 10653 06:57:07,440 --> 06:57:12,240 and finally we will uh return 10654 06:57:10,000 --> 06:57:13,680 pivot index uh that is p index plus one 10655 06:57:12,240 --> 06:57:14,400 that means we will be returning this 10656 06:57:13,680 --> 06:57:17,120 index 10657 06:57:14,400 --> 06:57:19,760 so that this element is not considered 10658 06:57:17,120 --> 06:57:22,080 or will not participate in any further 10659 06:57:19,760 --> 06:57:24,400 iterations or any further recursive 10660 06:57:22,080 --> 06:57:27,440 calls because you see if you observe 10661 06:57:24,400 --> 06:57:30,400 carefully that we we're sending pivot 10662 06:57:27,440 --> 06:57:32,878 index minus 1 that is without 5 all the 10663 06:57:30,400 --> 06:57:34,878 elements on the left hand side and plus 10664 06:57:32,878 --> 06:57:36,878 1 that means without this index all the 10665 06:57:34,878 --> 06:57:40,000 elements on the right hand side 10666 06:57:36,878 --> 06:57:42,240 okay this is how partition works now you 10667 06:57:40,000 --> 06:57:44,638 might be confused a little bit now let's 10668 06:57:42,240 --> 06:57:47,440 try to demonstrate this with the help of 10669 06:57:44,638 --> 06:57:51,280 example so you see we have an example 10670 06:57:47,440 --> 06:57:54,878 over here right we have 5 10 10671 06:57:51,280 --> 06:57:58,160 9 6 and 7 these are the elements in the 10672 06:57:54,878 --> 06:58:00,240 array and we have this pivot here the 10673 06:57:58,160 --> 06:58:02,718 last element we have chosen last element 10674 06:58:00,240 --> 06:58:04,958 to be the pivot and after that what we 10675 06:58:02,718 --> 06:58:07,120 are doing we have this end pointer and 10676 06:58:04,958 --> 06:58:09,520 we have this beginning pointer 10677 06:58:07,120 --> 06:58:11,520 also we have that pivot index which will 10678 06:58:09,520 --> 06:58:13,920 be somewhere around here right 10679 06:58:11,520 --> 06:58:16,798 that pivot index which will be minus one 10680 06:58:13,920 --> 06:58:19,440 now this seven will be checked okay is 10681 06:58:16,798 --> 06:58:21,680 five less than seven yes five is less 10682 06:58:19,440 --> 06:58:23,600 than seven so it will be swapped with 10683 06:58:21,680 --> 06:58:25,680 itself and pivot index will be 10684 06:58:23,600 --> 06:58:28,160 incremented first and then swap within 10685 06:58:25,680 --> 06:58:30,878 itself now pivot index will be here 10686 06:58:28,160 --> 06:58:32,798 next time around our a our i pointer 10687 06:58:30,878 --> 06:58:35,040 will be here first it will be here and 10688 06:58:32,798 --> 06:58:38,320 what it will be incremented now will be 10689 06:58:35,040 --> 06:58:39,440 again we will again check okay 10690 06:58:38,320 --> 06:58:43,200 is 7 10691 06:58:39,440 --> 06:58:45,680 is 7 less than 10 no it has not so our i 10692 06:58:43,200 --> 06:58:47,360 will be incremented i will be now here 10693 06:58:45,680 --> 06:58:50,000 at this position right 10694 06:58:47,360 --> 06:58:52,478 now again it will be checked no again it 10695 06:58:50,000 --> 06:58:55,200 will be checked yes so now what happens 10696 06:58:52,478 --> 06:58:57,680 seven and six uh the six will be 10697 06:58:55,200 --> 06:59:00,400 replaced with 10698 06:58:57,680 --> 06:59:01,600 what 10 so you have this six in here 10699 06:59:00,400 --> 06:59:04,400 obviously pivot index will be 10700 06:59:01,600 --> 06:59:06,798 incremented first and then we have the 10701 06:59:04,400 --> 06:59:09,600 six over here and it will be swapped 10702 06:59:06,798 --> 06:59:12,958 with 10 so 10 will be here right 10703 06:59:09,600 --> 06:59:15,120 done and finally when we are the end 10704 06:59:12,958 --> 06:59:17,360 once the entire iteration this is the 10705 06:59:15,120 --> 06:59:20,638 step one once the entire iteration is 10706 06:59:17,360 --> 06:59:21,680 completed we have 5 six and then seven 10707 06:59:20,638 --> 06:59:23,680 will be 10708 06:59:21,680 --> 06:59:26,320 the last swapping that we did the last 10709 06:59:23,680 --> 06:59:28,080 swap that we did if you observe here 10710 06:59:26,320 --> 06:59:30,320 carefully this swap 10711 06:59:28,080 --> 06:59:32,400 that we are doing 10712 06:59:30,320 --> 06:59:34,878 this is the one that is responsible for 10713 06:59:32,400 --> 06:59:37,280 swapping the seven with the pivot index 10714 06:59:34,878 --> 06:59:39,360 that is pivot index plus one that is 10715 06:59:37,280 --> 06:59:41,200 this location and we have this seven 10716 06:59:39,360 --> 06:59:43,120 over here and it will be replaced with 10717 06:59:41,200 --> 06:59:46,400 nine so that is why we have nine over 10718 06:59:43,120 --> 06:59:48,638 here and ten was here and this is the 10719 06:59:46,400 --> 06:59:50,160 array after first 10720 06:59:48,638 --> 06:59:52,240 iteration 10721 06:59:50,160 --> 06:59:53,840 now you might be thinking okay now this 10722 06:59:52,240 --> 06:59:55,520 element is fixed now we will not never 10723 06:59:53,840 --> 06:59:57,520 talk about this element because this has 10724 06:59:55,520 --> 06:59:59,360 its original position in the sorted 10725 06:59:57,520 --> 07:00:00,718 array as well now what we will be 10726 06:59:59,360 --> 07:00:02,958 dealing with we will be dealing with 10727 07:00:00,718 --> 07:00:04,240 this left part and will be dealing with 10728 07:00:02,958 --> 07:00:06,798 this right part 10729 07:00:04,240 --> 07:00:09,200 so now what happens in this part right 10730 07:00:06,798 --> 07:00:11,360 and what happens in this part 10731 07:00:09,200 --> 07:00:13,600 you see now we have new this is our 10732 07:00:11,360 --> 07:00:15,120 beginning and this is our pivot because 10733 07:00:13,600 --> 07:00:17,440 this is the last element that we will be 10734 07:00:15,120 --> 07:00:19,120 picking and this is our end similarly 10735 07:00:17,440 --> 07:00:20,878 this is our beginning this will be our 10736 07:00:19,120 --> 07:00:23,280 pivot the last element in the in this 10737 07:00:20,878 --> 07:00:25,440 sub array and the end will be here now 10738 07:00:23,280 --> 07:00:27,040 we'll be again doing the same step and 10739 07:00:25,440 --> 07:00:28,400 this time around we'll be checking okay 10740 07:00:27,040 --> 07:00:30,798 pivot is less than no nothing will 10741 07:00:28,400 --> 07:00:32,638 happen and then we will be we will be 10742 07:00:30,798 --> 07:00:34,878 swapping this thing 10743 07:00:32,638 --> 07:00:37,200 with itself right and now once this 10744 07:00:34,878 --> 07:00:38,798 entire suburb is completed will not go 10745 07:00:37,200 --> 07:00:41,120 any further because this time around 10746 07:00:38,798 --> 07:00:43,840 beginning is not less than end both 10747 07:00:41,120 --> 07:00:46,558 elements are at 0 and 0 is not less than 10748 07:00:43,840 --> 07:00:49,040 0 and now if you observe carefully this 10749 07:00:46,558 --> 07:00:50,718 is the condition that we were setting at 10750 07:00:49,040 --> 07:00:52,478 the start of the 10751 07:00:50,718 --> 07:00:54,400 function that is the quick sort function 10752 07:00:52,478 --> 07:00:56,958 and we'll be checking we're checking if 10753 07:00:54,400 --> 07:00:59,280 beginning is less than end right 10754 07:00:56,958 --> 07:01:00,878 so this is the importance of that 10755 07:00:59,280 --> 07:01:02,958 similarly the same thing will happen 10756 07:01:00,878 --> 07:01:05,600 over from this this side and again 10757 07:01:02,958 --> 07:01:08,878 beginning will not be less than index 10758 07:01:05,600 --> 07:01:12,718 end part and we will not go any further 10759 07:01:08,878 --> 07:01:14,798 so after two iterations our entire array 10760 07:01:12,718 --> 07:01:17,440 is sorted 10761 07:01:14,798 --> 07:01:19,920 right so this is the step one after step 10762 07:01:17,440 --> 07:01:21,520 two our entire heading is sorted after 10763 07:01:19,920 --> 07:01:24,000 learning what is quick sort let's 10764 07:01:21,520 --> 07:01:26,638 quickly implement the same in python so 10765 07:01:24,000 --> 07:01:29,520 here we are using python ide that does 10766 07:01:26,638 --> 07:01:31,360 google collab one of the ide mean to say 10767 07:01:29,520 --> 07:01:34,478 and then we'll implement that particular 10768 07:01:31,360 --> 07:01:36,400 program there so let's quickly hop into 10769 07:01:34,478 --> 07:01:39,200 the ide now 10770 07:01:36,400 --> 07:01:41,840 so here is the program for quick sort 10771 07:01:39,200 --> 07:01:44,558 in python so let's understand how this 10772 07:01:41,840 --> 07:01:47,280 program works right the first part we 10773 07:01:44,558 --> 07:01:49,360 need partition to be made right any 10774 07:01:47,280 --> 07:01:51,280 array in quick sort to be broken into 10775 07:01:49,360 --> 07:01:54,080 two halves and we will 10776 07:01:51,280 --> 07:01:56,878 start sorting in that particular 10777 07:01:54,080 --> 07:01:58,878 different pieces so 10778 07:01:56,878 --> 07:02:00,080 partition positioning 10779 07:01:58,878 --> 07:02:03,280 will be 10780 07:02:00,080 --> 07:02:06,320 done with the help of array low and high 10781 07:02:03,280 --> 07:02:08,958 variables so at the right most 10782 07:02:06,320 --> 07:02:11,680 always will consider the element of 10783 07:02:08,958 --> 07:02:13,680 pivot element right most element of the 10784 07:02:11,680 --> 07:02:16,240 array is a pivot element that is the 10785 07:02:13,680 --> 07:02:19,760 consideration so in order to do that we 10786 07:02:16,240 --> 07:02:23,120 will use p out is equal to a r r of h 10787 07:02:19,760 --> 07:02:24,958 right so then pointer for greater 10788 07:02:23,120 --> 07:02:26,638 element so whatever the element is 10789 07:02:24,958 --> 07:02:29,520 greater in order to compare will be 10790 07:02:26,638 --> 07:02:32,320 using this pointer in order to traverse 10791 07:02:29,520 --> 07:02:33,920 from all the elements inside an array 10792 07:02:32,320 --> 07:02:36,400 keeping one payout element in 10793 07:02:33,920 --> 07:02:39,040 consideration with comparing with that 10794 07:02:36,400 --> 07:02:42,478 particular element we use this for loop 10795 07:02:39,040 --> 07:02:45,360 system right if smaller than element is 10796 07:02:42,478 --> 07:02:47,840 present which is uh smaller than p out 10797 07:02:45,360 --> 07:02:50,558 we'll use this i is equal to i plus 1 10798 07:02:47,840 --> 07:02:53,280 and immediately will swap the element 10799 07:02:50,558 --> 07:02:55,840 in the position which is there in i with 10800 07:02:53,280 --> 07:03:00,000 j right that will be done with the help 10801 07:02:55,840 --> 07:03:03,040 of arr of i and j is equal to j and i 10802 07:03:00,000 --> 07:03:05,680 will exchange if you could see here i j 10803 07:03:03,040 --> 07:03:08,400 is being changed to j and i so when 10804 07:03:05,680 --> 07:03:10,240 exchanging the elements if it is smaller 10805 07:03:08,400 --> 07:03:13,200 than the pivot element 10806 07:03:10,240 --> 07:03:15,280 then swap p o with i if it's greater 10807 07:03:13,200 --> 07:03:17,600 than pure right if 10808 07:03:15,280 --> 07:03:20,400 any element which is greater than p out 10809 07:03:17,600 --> 07:03:23,280 element wherever the i is pointing to 10810 07:03:20,400 --> 07:03:25,520 that element will be swapped between the 10811 07:03:23,280 --> 07:03:26,320 element and put right in order to do 10812 07:03:25,520 --> 07:03:28,638 that 10813 07:03:26,320 --> 07:03:29,760 we will be using this particular 10814 07:03:28,638 --> 07:03:32,000 condition 10815 07:03:29,760 --> 07:03:33,920 then we will get back to the initial 10816 07:03:32,000 --> 07:03:36,000 position where we started the 10817 07:03:33,920 --> 07:03:37,840 partitioning right where we broke that 10818 07:03:36,000 --> 07:03:38,638 array into two parts the partitioning is 10819 07:03:37,840 --> 07:03:41,680 done 10820 07:03:38,638 --> 07:03:43,600 there will go back and will try to start 10821 07:03:41,680 --> 07:03:46,000 initial position 10822 07:03:43,600 --> 07:03:49,280 then the quick sort function will come 10823 07:03:46,000 --> 07:03:50,958 right so here 10824 07:03:49,280 --> 07:03:54,080 in quick sort again we need three 10825 07:03:50,958 --> 07:03:54,798 different elements array low and high 10826 07:03:54,080 --> 07:03:56,958 if 10827 07:03:54,798 --> 07:03:58,958 low is less than high that is smaller 10828 07:03:56,958 --> 07:04:01,200 element than p vote is present it will 10829 07:03:58,958 --> 07:04:03,920 all go towards the left side if there is 10830 07:04:01,200 --> 07:04:06,798 greater element than period is present 10831 07:04:03,920 --> 07:04:09,120 it will go to right side so partitioning 10832 07:04:06,798 --> 07:04:11,440 is done accordingly 10833 07:04:09,120 --> 07:04:14,160 so this is a recursive call which we 10834 07:04:11,440 --> 07:04:16,400 follow for quick sort right we'll be 10835 07:04:14,160 --> 07:04:21,520 having again array low 10836 07:04:16,400 --> 07:04:23,200 p i minus 1 pi is pot minus 1 so 10837 07:04:21,520 --> 07:04:25,760 again for the right of the period we 10838 07:04:23,200 --> 07:04:28,558 have a recursive call function which is 10839 07:04:25,760 --> 07:04:30,958 declared here once all these things are 10840 07:04:28,558 --> 07:04:32,878 done we have to give data in order to 10841 07:04:30,958 --> 07:04:35,440 sort something right we are here 10842 07:04:32,878 --> 07:04:38,160 presently concentrating on sorting the 10843 07:04:35,440 --> 07:04:41,040 array which is given in the ascending 10844 07:04:38,160 --> 07:04:44,240 order right so the data set here is 10845 07:04:41,040 --> 07:04:47,200 mentioned and it has been assigned as d 10846 07:04:44,240 --> 07:04:51,920 right so the set has been assigned as d 10847 07:04:47,200 --> 07:04:54,160 9 comma 8 7 2 10 20 and 1 so these are 10848 07:04:51,920 --> 07:04:56,798 the elements which we are trying to sort 10849 07:04:54,160 --> 07:04:58,798 right we are printing the unsorted array 10850 07:04:56,798 --> 07:05:01,840 that means however the input is present 10851 07:04:58,798 --> 07:05:03,840 here that is printed as it is unsorted 10852 07:05:01,840 --> 07:05:05,520 is array is equal to so and so which is 10853 07:05:03,840 --> 07:05:06,638 already there which which we are not 10854 07:05:05,520 --> 07:05:10,718 performing 10855 07:05:06,638 --> 07:05:12,798 any functions then we have print d that 10856 07:05:10,718 --> 07:05:15,200 means immediately it will print 10857 07:05:12,798 --> 07:05:17,360 then size is equal to length of d we 10858 07:05:15,200 --> 07:05:19,200 will consider in order to print while we 10859 07:05:17,360 --> 07:05:22,240 are printing right we have to print 10860 07:05:19,200 --> 07:05:23,120 element wise so again we have to print 10861 07:05:22,240 --> 07:05:25,600 it 10862 07:05:23,120 --> 07:05:27,600 nine first eight next seven next and 10863 07:05:25,600 --> 07:05:29,520 then two followed by 10864 07:05:27,600 --> 07:05:32,320 up to one 10865 07:05:29,520 --> 07:05:34,718 so after that is done we will send this 10866 07:05:32,320 --> 07:05:37,760 particular data raw data which is 10867 07:05:34,718 --> 07:05:40,240 unsorted data to the function called 10868 07:05:37,760 --> 07:05:43,120 quick sort which we have created here 10869 07:05:40,240 --> 07:05:45,520 right so that has been sent once that is 10870 07:05:43,120 --> 07:05:47,120 sent it will follow all the procedures 10871 07:05:45,520 --> 07:05:50,400 which is mentioned here all the 10872 07:05:47,120 --> 07:05:53,120 functions will be passed with the data 10873 07:05:50,400 --> 07:05:55,600 and then finally we will print sorted 10874 07:05:53,120 --> 07:05:58,240 array in ascending order which is uh 10875 07:05:55,600 --> 07:06:00,638 sorted using quick sort right so let's 10876 07:05:58,240 --> 07:06:03,680 quickly run this program and check out 10877 07:06:00,638 --> 07:06:03,680 what is the output 10878 07:06:04,798 --> 07:06:09,360 so it will take some time in order to 10879 07:06:07,040 --> 07:06:11,520 take the output so let's quickly see 10880 07:06:09,360 --> 07:06:13,520 okay so that is what i mentioned 10881 07:06:11,520 --> 07:06:16,080 unsorted array is nothing but the array 10882 07:06:13,520 --> 07:06:18,958 which has been given by the user 10883 07:06:16,080 --> 07:06:20,718 and sorted array is also given after 10884 07:06:18,958 --> 07:06:23,120 performing all the functions assigned 10885 07:06:20,718 --> 07:06:25,920 for the quick sort so if you could see 10886 07:06:23,120 --> 07:06:28,958 it is in ascending order starting from 1 10887 07:06:25,920 --> 07:06:31,520 and ending at 20. so this is all about 10888 07:06:28,958 --> 07:06:33,440 quick sort in python now let's try to 10889 07:06:31,520 --> 07:06:35,360 understand the time complexity of 10890 07:06:33,440 --> 07:06:37,440 quicksort algorithm in quicksort 10891 07:06:35,360 --> 07:06:39,920 algorithm we have now seen that 10892 07:06:37,440 --> 07:06:41,760 partitioning of elements takes place and 10893 07:06:39,920 --> 07:06:44,320 we are partitioning all the elements 10894 07:06:41,760 --> 07:06:46,478 that means all the n elements if there 10895 07:06:44,320 --> 07:06:48,878 are eight elements all the eight 10896 07:06:46,478 --> 07:06:51,280 elements will be we iterate through all 10897 07:06:48,878 --> 07:06:53,920 the eight elements right so partitioning 10898 07:06:51,280 --> 07:06:56,798 them takes n time that is order of n 10899 07:06:53,920 --> 07:06:59,200 time and then quick sort problem divides 10900 07:06:56,798 --> 07:07:01,280 it into the factor by the factor of two 10901 07:06:59,200 --> 07:07:04,320 right every time we are dividing it by 10902 07:07:01,280 --> 07:07:06,478 two so the entire 10903 07:07:04,320 --> 07:07:09,920 process or the time complexity of quick 10904 07:07:06,478 --> 07:07:12,958 sort in best case and in average case 10905 07:07:09,920 --> 07:07:14,478 takes order of n time that is big o of 10906 07:07:12,958 --> 07:07:16,478 log n 10907 07:07:14,478 --> 07:07:19,040 and same thing happens when we are 10908 07:07:16,478 --> 07:07:22,558 talking about the average case as well 10909 07:07:19,040 --> 07:07:24,798 but why this is n square in worst case 10910 07:07:22,558 --> 07:07:27,600 that is the question right so let me 10911 07:07:24,798 --> 07:07:29,840 clear it out so the question is that 10912 07:07:27,600 --> 07:07:33,600 why this thing happens if you are 10913 07:07:29,840 --> 07:07:36,400 picking either the smallest element 10914 07:07:33,600 --> 07:07:39,600 in the array or the largest element in 10915 07:07:36,400 --> 07:07:41,680 the array as pivot in that case 10916 07:07:39,600 --> 07:07:44,160 you are traversing through all the 10917 07:07:41,680 --> 07:07:46,000 elements again that means this n is 10918 07:07:44,160 --> 07:07:47,360 already there for partitioning them that 10919 07:07:46,000 --> 07:07:50,718 means you will be i trading through the 10920 07:07:47,360 --> 07:07:52,958 array but the extra n and that means 10921 07:07:50,718 --> 07:07:55,280 inside that and you're again 10922 07:07:52,958 --> 07:07:57,280 traversing through all the elements and 10923 07:07:55,280 --> 07:08:00,000 swapping them because you have picked 10924 07:07:57,280 --> 07:08:01,760 your pivot in worst case you can either 10925 07:08:00,000 --> 07:08:04,400 pick it as smallest or the largest 10926 07:08:01,760 --> 07:08:06,240 element in the array in both these cases 10927 07:08:04,400 --> 07:08:08,400 you are you will be swapping all those 10928 07:08:06,240 --> 07:08:10,320 elements with itself that this element 10929 07:08:08,400 --> 07:08:12,160 will be swapped right this is this is 10930 07:08:10,320 --> 07:08:13,920 the largest element right this is let's 10931 07:08:12,160 --> 07:08:15,920 suppose this is eight so nothing will 10932 07:08:13,920 --> 07:08:17,760 happen right so these are this this is 10933 07:08:15,920 --> 07:08:19,520 smallest then it will be swapped with 10934 07:08:17,760 --> 07:08:21,360 itself this is smallest this is this 10935 07:08:19,520 --> 07:08:23,200 will be swapped with itself this will be 10936 07:08:21,360 --> 07:08:24,798 swapped with itself this will be swapped 10937 07:08:23,200 --> 07:08:26,878 itself so all the elements will be 10938 07:08:24,798 --> 07:08:29,520 swapped and finally this element will 10939 07:08:26,878 --> 07:08:32,080 have its original position at the end 10940 07:08:29,520 --> 07:08:34,240 right so this thing will happen if you 10941 07:08:32,080 --> 07:08:36,958 are picking your pivot as the smallest 10942 07:08:34,240 --> 07:08:39,680 element or as the largest element in the 10943 07:08:36,958 --> 07:08:41,200 array okay so in this in these two cases 10944 07:08:39,680 --> 07:08:44,160 this is not the case you are picking 10945 07:08:41,200 --> 07:08:46,160 your pivots as random you are picking 10946 07:08:44,160 --> 07:08:48,638 your pivots randomly okay 10947 07:08:46,160 --> 07:08:50,878 in nutshell when you are picking your 10948 07:08:48,638 --> 07:08:53,280 element that is your pivot element as 10949 07:08:50,878 --> 07:08:55,840 smallest are or the largest element in 10950 07:08:53,280 --> 07:08:58,000 the array in that case that will be your 10951 07:08:55,840 --> 07:09:00,958 worst case time complexity and it will 10952 07:08:58,000 --> 07:09:02,798 be big o of n square 10953 07:09:00,958 --> 07:09:05,200 now let's talk about the space 10954 07:09:02,798 --> 07:09:07,200 complexity of quake sort now you might 10955 07:09:05,200 --> 07:09:09,280 be thinking okay we are not using any 10956 07:09:07,200 --> 07:09:11,280 extra space right we are not using any 10957 07:09:09,280 --> 07:09:14,478 auxiliary memory like in the form of 10958 07:09:11,280 --> 07:09:15,920 array stack q link list or anything 10959 07:09:14,478 --> 07:09:18,320 right but 10960 07:09:15,920 --> 07:09:20,240 for calling this function that is the 10961 07:09:18,320 --> 07:09:22,240 quick sort function we are using 10962 07:09:20,240 --> 07:09:24,638 recursion right we are calling this weak 10963 07:09:22,240 --> 07:09:26,878 sort again and again i do quick sort 10964 07:09:24,638 --> 07:09:31,120 calls are there for maintaining the call 10965 07:09:26,878 --> 07:09:33,440 stack we require order of n space 10966 07:09:31,120 --> 07:09:34,638 that is the time complexity will be big 10967 07:09:33,440 --> 07:09:36,878 off and 10968 07:09:34,638 --> 07:09:38,958 when we are using this approach and in 10969 07:09:36,878 --> 07:09:40,878 the worst case this will be the scenario 10970 07:09:38,958 --> 07:09:43,680 that all the elements will be on the 10971 07:09:40,878 --> 07:09:46,240 call stack okay so in worst case the 10972 07:09:43,680 --> 07:09:49,200 space complexity will be bigger and but 10973 07:09:46,240 --> 07:09:51,200 if we modify this approach of of storing 10974 07:09:49,200 --> 07:09:53,200 the elements and calling the call stack 10975 07:09:51,200 --> 07:09:55,520 and maintaining the call stack we can 10976 07:09:53,200 --> 07:09:57,600 reduce it to big o 10977 07:09:55,520 --> 07:09:59,600 of log n 10978 07:09:57,600 --> 07:10:01,680 now let's try to analyze quick sort 10979 07:09:59,600 --> 07:10:04,240 algorithm let's first try to understand 10980 07:10:01,680 --> 07:10:06,958 the stability so let's suppose if you 10981 07:10:04,240 --> 07:10:08,080 have this array one three one dash and 10982 07:10:06,958 --> 07:10:11,280 four 10983 07:10:08,080 --> 07:10:14,160 now an algorithm is said to be stable if 10984 07:10:11,280 --> 07:10:15,280 both these one and this one 10985 07:10:14,160 --> 07:10:17,360 both 10986 07:10:15,280 --> 07:10:20,798 these 10987 07:10:17,360 --> 07:10:22,958 in the sorted area will maintain their 10988 07:10:20,798 --> 07:10:25,840 relative positions now you have this 10989 07:10:22,958 --> 07:10:27,680 sorted area right and both one this one 10990 07:10:25,840 --> 07:10:30,798 and this one are maintaining their 10991 07:10:27,680 --> 07:10:33,360 relative positions which were earlier in 10992 07:10:30,798 --> 07:10:35,520 the unsorted area right so if that thing 10993 07:10:33,360 --> 07:10:37,840 is maintained right 10994 07:10:35,520 --> 07:10:40,478 that thing is maintained the algorithm 10995 07:10:37,840 --> 07:10:42,000 is stable as it is not stable obviously 10996 07:10:40,478 --> 07:10:44,400 you can have another 10997 07:10:42,000 --> 07:10:47,040 way with which this is also sorted but 10998 07:10:44,400 --> 07:10:49,040 this is not a stable this is unstable 10999 07:10:47,040 --> 07:10:50,558 algorithm and if you are sorting in such 11000 07:10:49,040 --> 07:10:51,440 a manner and you have these things 11001 07:10:50,558 --> 07:10:54,718 placed 11002 07:10:51,440 --> 07:10:56,638 this algorithm is unstable so if you 11003 07:10:54,718 --> 07:10:59,760 talk about quick sort algorithm quick 11004 07:10:56,638 --> 07:11:01,360 sort algorithm is an unstable algorithm 11005 07:10:59,760 --> 07:11:03,680 although we can do 11006 07:11:01,360 --> 07:11:05,680 some modifications and we can stabilize 11007 07:11:03,680 --> 07:11:08,400 it or we can add we can make this 11008 07:11:05,680 --> 07:11:10,080 algorithm as stable but 11009 07:11:08,400 --> 07:11:12,400 as of now 11010 07:11:10,080 --> 07:11:15,520 if you talk about quicksort algorithm it 11011 07:11:12,400 --> 07:11:18,000 is an unstable algorithm what about in 11012 07:11:15,520 --> 07:11:20,400 place and outplays since we are not 11013 07:11:18,000 --> 07:11:23,200 using any auxiliary memory right we are 11014 07:11:20,400 --> 07:11:25,120 not using any extra space explicitly 11015 07:11:23,200 --> 07:11:28,240 right in the form of array or linked 11016 07:11:25,120 --> 07:11:30,240 list or stack right or even cute we're 11017 07:11:28,240 --> 07:11:33,200 not using any extra 11018 07:11:30,240 --> 07:11:35,360 memory right so 11019 07:11:33,200 --> 07:11:37,440 this algorithm quick sort algorithm 11020 07:11:35,360 --> 07:11:39,760 although we are maintaining a call stack 11021 07:11:37,440 --> 07:11:41,600 wherein we are you uh maintaining a call 11022 07:11:39,760 --> 07:11:42,878 stack and we have a space complexity of 11023 07:11:41,600 --> 07:11:44,958 big o of n 11024 07:11:42,878 --> 07:11:46,718 but since we are not explicitly 11025 07:11:44,958 --> 07:11:49,120 mentioning this these 11026 07:11:46,718 --> 07:11:53,440 uh these auxiliary memories this 11027 07:11:49,120 --> 07:11:55,840 algorithm is an in place algorithm and 11028 07:11:53,440 --> 07:11:57,920 these are the two analysis that can be 11029 07:11:55,840 --> 07:12:00,000 done on quick sort 11030 07:11:57,920 --> 07:12:01,840 so in nutshell if you talk about quick 11031 07:12:00,000 --> 07:12:05,440 sort right 11032 07:12:01,840 --> 07:12:08,718 it is unstable algorithm 11033 07:12:05,440 --> 07:12:11,120 and it is in place algorithm now we'll 11034 07:12:08,718 --> 07:12:13,040 be learning regarding divide and conquer 11035 07:12:11,120 --> 07:12:15,840 approach to programming 11036 07:12:13,040 --> 07:12:18,558 so every complex program can be divided 11037 07:12:15,840 --> 07:12:21,520 into sub programs and solved to make it 11038 07:12:18,558 --> 07:12:23,360 more simple and precise enough so let's 11039 07:12:21,520 --> 07:12:24,400 quickly see what is divide and conquer 11040 07:12:23,360 --> 07:12:27,120 approach 11041 07:12:24,400 --> 07:12:30,638 so as the name suggests divide and 11042 07:12:27,120 --> 07:12:33,440 conquer any complex program for example 11043 07:12:30,638 --> 07:12:36,000 right i am writing complex program which 11044 07:12:33,440 --> 07:12:38,320 is having 11045 07:12:36,000 --> 07:12:39,360 so many applications in that so what do 11046 07:12:38,320 --> 07:12:41,680 we prefer 11047 07:12:39,360 --> 07:12:44,080 we prefer 11048 07:12:41,680 --> 07:12:48,160 breaking that particular complex program 11049 07:12:44,080 --> 07:12:52,080 into the sub problems or sub programs 11050 07:12:48,160 --> 07:12:54,400 and solving together right so here in 11051 07:12:52,080 --> 07:12:55,520 sub program one we'll be getting one 11052 07:12:54,400 --> 07:12:57,600 answer 11053 07:12:55,520 --> 07:12:58,638 in sub program two we'll get one more 11054 07:12:57,600 --> 07:13:00,718 answer 11055 07:12:58,638 --> 07:13:02,240 in sub program three we get another 11056 07:13:00,718 --> 07:13:04,798 answer 11057 07:13:02,240 --> 07:13:08,958 combining all these answers 11058 07:13:04,798 --> 07:13:12,000 we get the final result right so this is 11059 07:13:08,958 --> 07:13:13,520 what divide and conquer approach is 11060 07:13:12,000 --> 07:13:15,760 right so 11061 07:13:13,520 --> 07:13:18,558 why do we use this particular divide and 11062 07:13:15,760 --> 07:13:21,200 conquer approach generally 11063 07:13:18,558 --> 07:13:24,558 the first thing is the problem solving 11064 07:13:21,200 --> 07:13:27,760 becomes easy right for everybody it is 11065 07:13:24,558 --> 07:13:30,798 easy to understand and easy to look at 11066 07:13:27,760 --> 07:13:33,120 the branching say for example if in this 11067 07:13:30,798 --> 07:13:35,680 simple example which i gave you 11068 07:13:33,120 --> 07:13:38,240 people can figure out one complex 11069 07:13:35,680 --> 07:13:40,718 program was divided into three different 11070 07:13:38,240 --> 07:13:44,478 sub programs we got three different 11071 07:13:40,718 --> 07:13:47,600 answers combining all these answers 11072 07:13:44,478 --> 07:13:50,320 we ended up with the final result so 11073 07:13:47,600 --> 07:13:52,958 this is simple and it is less time 11074 07:13:50,320 --> 07:13:56,240 consuming because parallely people will 11075 07:13:52,958 --> 07:13:59,040 be working say one big project you have 11076 07:13:56,240 --> 07:14:00,400 got in order to work with you can divide 11077 07:13:59,040 --> 07:14:02,958 amongst 11078 07:14:00,400 --> 07:14:05,120 three people right all the three people 11079 07:14:02,958 --> 07:14:07,440 simultaneously will work 11080 07:14:05,120 --> 07:14:08,478 say for example after two hours you'll 11081 07:14:07,440 --> 07:14:11,520 be getting 11082 07:14:08,478 --> 07:14:14,400 answer one also answer two also answer 11083 07:14:11,520 --> 07:14:16,878 three also then quickly you can just 11084 07:14:14,400 --> 07:14:17,840 combine all these answers get the final 11085 07:14:16,878 --> 07:14:20,798 result 11086 07:14:17,840 --> 07:14:23,680 right so hope this is clear so this is 11087 07:14:20,798 --> 07:14:25,840 why we use divide and conquer approach 11088 07:14:23,680 --> 07:14:28,160 and it is very easy 11089 07:14:25,840 --> 07:14:31,120 then coming up to the applications where 11090 07:14:28,160 --> 07:14:33,120 do we apply these approaches in data 11091 07:14:31,120 --> 07:14:35,440 structures we'll be applying that 11092 07:14:33,120 --> 07:14:36,958 majorly in merge sort 11093 07:14:35,440 --> 07:14:39,760 quick sort 11094 07:14:36,958 --> 07:14:42,638 by research and many more places 11095 07:14:39,760 --> 07:14:44,400 so let me quickly give you a 11096 07:14:42,638 --> 07:14:47,520 crisp knowledge 11097 07:14:44,400 --> 07:14:51,200 how do we solve this divide and conquer 11098 07:14:47,520 --> 07:14:53,920 approach with an example so i'm taking 11099 07:14:51,200 --> 07:14:55,760 merge sort let's quickly switch to the 11100 07:14:53,920 --> 07:14:58,240 ide and see 11101 07:14:55,760 --> 07:15:00,718 how does this particular divide and 11102 07:14:58,240 --> 07:15:03,520 conquer approach help us in order to 11103 07:15:00,718 --> 07:15:06,400 solve merge sort or solve or sort the 11104 07:15:03,520 --> 07:15:08,798 elements inside the array right 11105 07:15:06,400 --> 07:15:11,200 what is merge sort if we talk about 11106 07:15:08,798 --> 07:15:14,080 merge sort let's try to understand first 11107 07:15:11,200 --> 07:15:16,558 sorting so sorting is a mechanism of 11108 07:15:14,080 --> 07:15:18,718 giving order to your 11109 07:15:16,558 --> 07:15:22,798 values right so let's suppose you have 11110 07:15:18,718 --> 07:15:25,360 some values random values 10 30 11111 07:15:22,798 --> 07:15:27,840 and then you have 5 to 11112 07:15:25,360 --> 07:15:30,160 one and so on right you have these 11113 07:15:27,840 --> 07:15:32,320 values and now you want to 11114 07:15:30,160 --> 07:15:34,160 maintain some order so in order to 11115 07:15:32,320 --> 07:15:37,120 visualize this data let's suppose you 11116 07:15:34,160 --> 07:15:39,120 want to see uh the ascending order of it 11117 07:15:37,120 --> 07:15:41,280 or the descending order of it that is 11118 07:15:39,120 --> 07:15:43,760 what you mean by sorting so let's 11119 07:15:41,280 --> 07:15:46,320 suppose you have a class and in that 11120 07:15:43,760 --> 07:15:48,798 class you have several role numbers and 11121 07:15:46,320 --> 07:15:50,798 some of the roll numbers are not present 11122 07:15:48,798 --> 07:15:53,120 and then you want to 11123 07:15:50,798 --> 07:15:54,878 sort those roll numbers 11124 07:15:53,120 --> 07:15:58,080 in terms of ascending or descending 11125 07:15:54,878 --> 07:16:01,040 order that is when you require sorting 11126 07:15:58,080 --> 07:16:03,600 so this is the basic intuition behind 11127 07:16:01,040 --> 07:16:05,680 sorting trying to give order to some 11128 07:16:03,600 --> 07:16:08,958 kind of values or some kind of a data 11129 07:16:05,680 --> 07:16:10,718 set right so in this particular tutorial 11130 07:16:08,958 --> 07:16:13,760 we are going to talk about merge source 11131 07:16:10,718 --> 07:16:16,878 so merge sort is a classical sorting 11132 07:16:13,760 --> 07:16:19,440 algorithm in this sorting 11133 07:16:16,878 --> 07:16:21,440 every time your problem is divided into 11134 07:16:19,440 --> 07:16:23,760 sub problems so that 11135 07:16:21,440 --> 07:16:26,080 your problem set is reduced and then you 11136 07:16:23,760 --> 07:16:27,520 will be focusing on that sub problem 11137 07:16:26,080 --> 07:16:29,680 similarly every time when you are 11138 07:16:27,520 --> 07:16:32,000 dividing your sub problems you will keep 11139 07:16:29,680 --> 07:16:34,638 on dividing it unless and until there is 11140 07:16:32,000 --> 07:16:36,240 only one element left right if you 11141 07:16:34,638 --> 07:16:38,400 compare it with simpler sorting 11142 07:16:36,240 --> 07:16:40,718 algorithms like bubble is there 11143 07:16:38,400 --> 07:16:43,200 insertion is there selection is there 11144 07:16:40,718 --> 07:16:44,878 quake is there when you talk about its 11145 07:16:43,200 --> 07:16:47,840 time complexity as compared to these 11146 07:16:44,878 --> 07:16:50,478 algorithms this is very much efficient 11147 07:16:47,840 --> 07:16:52,478 now it follows a paradigm of divide and 11148 07:16:50,478 --> 07:16:54,638 conquer what does this mean this means 11149 07:16:52,478 --> 07:16:57,920 that first you keep on dividing your sub 11150 07:16:54,638 --> 07:17:00,240 problems and then you will conquer those 11151 07:16:57,920 --> 07:17:02,638 problems and then you will combine those 11152 07:17:00,240 --> 07:17:04,160 things okay so here and we'll see when 11153 07:17:02,638 --> 07:17:06,478 you are trying to divide your sub 11154 07:17:04,160 --> 07:17:08,798 problems and then when you have your 11155 07:17:06,478 --> 07:17:11,600 problem set and those problem sets are 11156 07:17:08,798 --> 07:17:12,638 conquered that means those problems are 11157 07:17:11,600 --> 07:17:14,160 further 11158 07:17:12,638 --> 07:17:16,320 when you talk about in this example 11159 07:17:14,160 --> 07:17:18,398 those sub problems are sorted in this 11160 07:17:16,320 --> 07:17:20,160 case and then you have conquered them 11161 07:17:18,398 --> 07:17:22,160 and then you will combine them that is 11162 07:17:20,160 --> 07:17:24,638 your merge phase wherein you will be 11163 07:17:22,160 --> 07:17:26,160 combining your problem again and then 11164 07:17:24,638 --> 07:17:29,200 for forming 11165 07:17:26,160 --> 07:17:30,638 again a single sub problem so every time 11166 07:17:29,200 --> 07:17:32,398 you will be dividing that sub problem 11167 07:17:30,638 --> 07:17:34,878 you will be conquering it and combining 11168 07:17:32,398 --> 07:17:36,958 it so this is how this divide and 11169 07:17:34,878 --> 07:17:38,558 conquer paradigm works 11170 07:17:36,958 --> 07:17:40,718 so basically 11171 07:17:38,558 --> 07:17:42,718 when you're dealing with merge sort you 11172 07:17:40,718 --> 07:17:45,840 are focusing on two functions that is 11173 07:17:42,718 --> 07:17:48,160 your merge function and your merge sort 11174 07:17:45,840 --> 07:17:50,080 function so now let's talk about this 11175 07:17:48,160 --> 07:17:52,478 divide that means you're dividing your 11176 07:17:50,080 --> 07:17:54,878 sub problems which continues unless and 11177 07:17:52,478 --> 07:17:57,520 until there is only one element left 11178 07:17:54,878 --> 07:17:59,360 because one element in itself is sorted 11179 07:17:57,520 --> 07:18:01,200 right now this is your divide phase what 11180 07:17:59,360 --> 07:18:03,040 about conquer basically you are 11181 07:18:01,200 --> 07:18:05,840 conquering those individual sets and 11182 07:18:03,040 --> 07:18:08,320 then merging those two sub problems into 11183 07:18:05,840 --> 07:18:10,958 a single problem and finally you will be 11184 07:18:08,320 --> 07:18:15,200 doing it on each step and finally you 11185 07:18:10,958 --> 07:18:16,878 have your original array which is sorted 11186 07:18:15,200 --> 07:18:18,160 now let's talk about merge sort 11187 07:18:16,878 --> 07:18:18,958 algorithm 11188 07:18:18,160 --> 07:18:22,240 so 11189 07:18:18,958 --> 07:18:24,798 first let's talk about merge sort method 11190 07:18:22,240 --> 07:18:27,280 so in this method what we are doing we 11191 07:18:24,798 --> 07:18:30,558 are dividing our 11192 07:18:27,280 --> 07:18:32,878 array into further subarrays how we are 11193 07:18:30,558 --> 07:18:35,280 going to do that we are basically if we 11194 07:18:32,878 --> 07:18:36,718 have this array right and this array 11195 07:18:35,280 --> 07:18:38,958 let's suppose this contains eight 11196 07:18:36,718 --> 07:18:40,718 elements right this is our array and 11197 07:18:38,958 --> 07:18:42,638 let's suppose this is our left pointer 11198 07:18:40,718 --> 07:18:44,958 and this is our right pointer and now 11199 07:18:42,638 --> 07:18:46,558 what we are doing we are dividing it so 11200 07:18:44,958 --> 07:18:47,440 we need some kind of 11201 07:18:46,558 --> 07:18:50,558 in 11202 07:18:47,440 --> 07:18:52,638 iterator wherein we will store the 11203 07:18:50,558 --> 07:18:54,558 sum of and we try to calculate the mid 11204 07:18:52,638 --> 07:18:56,798 value how would you do it in simple 11205 07:18:54,558 --> 07:18:58,718 words we calculate left plus right 11206 07:18:56,798 --> 07:19:00,878 divided by 2 that's it right 11207 07:18:58,718 --> 07:19:03,360 so we have this division and then we 11208 07:19:00,878 --> 07:19:05,680 will divide this part because we are 11209 07:19:03,360 --> 07:19:08,080 calling this function again 11210 07:19:05,680 --> 07:19:10,718 right on the left hand side so this is 11211 07:19:08,080 --> 07:19:12,558 going to call on this side that is we 11212 07:19:10,718 --> 07:19:15,200 will be talking about now only three 11213 07:19:12,558 --> 07:19:16,798 elements so this is let's suppose 11214 07:19:15,200 --> 07:19:19,040 let's take four elements on this side 11215 07:19:16,798 --> 07:19:21,120 and four elements on this side so our 11216 07:19:19,040 --> 07:19:22,878 mid will be three point five so we'll be 11217 07:19:21,120 --> 07:19:24,878 talking about elements from zero to 11218 07:19:22,878 --> 07:19:27,360 three so we will be talking about four 11219 07:19:24,878 --> 07:19:30,160 elements and then further 11220 07:19:27,360 --> 07:19:33,120 these two steps are remain why because 11221 07:19:30,160 --> 07:19:35,920 we are implementing this in recursive 11222 07:19:33,120 --> 07:19:37,680 fashion so now let's suppose this is our 11223 07:19:35,920 --> 07:19:40,320 first function call and you have these 11224 07:19:37,680 --> 07:19:42,558 three steps one let's name it one 11225 07:19:40,320 --> 07:19:44,240 two and three okay so in the first 11226 07:19:42,558 --> 07:19:45,760 function called one two and three so 11227 07:19:44,240 --> 07:19:47,520 this is the first function call and in 11228 07:19:45,760 --> 07:19:49,120 this we're calling again this is a 11229 07:19:47,520 --> 07:19:51,360 second function call and we are calling 11230 07:19:49,120 --> 07:19:53,920 again these two remain right and we are 11231 07:19:51,360 --> 07:19:56,000 calling again one two and three right so 11232 07:19:53,920 --> 07:19:57,440 in this case again we are calling it on 11233 07:19:56,000 --> 07:19:59,360 these four elements and it will be 11234 07:19:57,440 --> 07:20:01,840 divided into further two elements zero 11235 07:19:59,360 --> 07:20:03,680 and one right and in this case again 11236 07:20:01,840 --> 07:20:04,558 this is called this will be called on 11237 07:20:03,680 --> 07:20:05,840 this 11238 07:20:04,558 --> 07:20:07,360 these two elements right here we are 11239 07:20:05,840 --> 07:20:08,878 talking about only two elements so this 11240 07:20:07,360 --> 07:20:10,878 is the third function called and again 11241 07:20:08,878 --> 07:20:13,600 we will be dividing it these two and 11242 07:20:10,878 --> 07:20:14,718 three steps are still remaining 11243 07:20:13,600 --> 07:20:16,320 so 11244 07:20:14,718 --> 07:20:18,558 in the fourth step what we are doing 11245 07:20:16,320 --> 07:20:20,320 with only single element right and in 11246 07:20:18,558 --> 07:20:22,958 this case we are talking about only this 11247 07:20:20,320 --> 07:20:24,000 element right and further we will not be 11248 07:20:22,958 --> 07:20:27,040 able to 11249 07:20:24,000 --> 07:20:29,040 divide it and in that case our left is 11250 07:20:27,040 --> 07:20:31,360 not greater than our left will be 11251 07:20:29,040 --> 07:20:33,360 greater than or equal to right and in 11252 07:20:31,360 --> 07:20:36,160 this case it will be equal to so we will 11253 07:20:33,360 --> 07:20:38,478 return so now it will be returned right 11254 07:20:36,160 --> 07:20:40,320 and then whatever was the left over 11255 07:20:38,478 --> 07:20:41,840 right now we talk about this single 11256 07:20:40,320 --> 07:20:44,080 element the other element that was left 11257 07:20:41,840 --> 07:20:46,558 behind we'll talk about that so again 11258 07:20:44,080 --> 07:20:48,798 that will be divided into one 11259 07:20:46,558 --> 07:20:51,200 right and again this will the second 11260 07:20:48,798 --> 07:20:52,798 option just erase it because it looks a 11261 07:20:51,200 --> 07:20:55,840 little bit messy 11262 07:20:52,798 --> 07:20:55,840 so now what happens 11263 07:20:59,120 --> 07:21:03,040 let me just 11264 07:21:00,398 --> 07:21:04,798 put it again in red so here we call this 11265 07:21:03,040 --> 07:21:05,520 fourth time and this time it return 11266 07:21:04,798 --> 07:21:07,360 right 11267 07:21:05,520 --> 07:21:09,920 so in this case now we will be on the 11268 07:21:07,360 --> 07:21:11,680 second step now again it will be called 11269 07:21:09,920 --> 07:21:12,798 on that single element 11270 07:21:11,680 --> 07:21:15,680 and again 11271 07:21:12,798 --> 07:21:17,440 left is not great will be greater than 11272 07:21:15,680 --> 07:21:19,680 or equal to in this case it will be 11273 07:21:17,440 --> 07:21:21,760 equal to and then we return so again we 11274 07:21:19,680 --> 07:21:23,920 are returning so we have these two 11275 07:21:21,760 --> 07:21:26,240 elements one and one 11276 07:21:23,920 --> 07:21:29,040 that means not one and one element but 11277 07:21:26,240 --> 07:21:30,878 there is only a single element in both 11278 07:21:29,040 --> 07:21:32,958 these arrays right 11279 07:21:30,878 --> 07:21:35,040 why i'm saying that in these two arrays 11280 07:21:32,958 --> 07:21:37,440 will be we will check when we talk about 11281 07:21:35,040 --> 07:21:39,440 merge okay we'll see how that is 11282 07:21:37,440 --> 07:21:40,558 implemented okay 11283 07:21:39,440 --> 07:21:42,718 so now 11284 07:21:40,558 --> 07:21:44,558 these these two function calls are done 11285 07:21:42,718 --> 07:21:47,280 and then we deal with 11286 07:21:44,558 --> 07:21:49,600 merge now before going into the merge 11287 07:21:47,280 --> 07:21:50,798 let me show you a demonstration of how 11288 07:21:49,600 --> 07:21:53,200 things look 11289 07:21:50,798 --> 07:21:54,798 so you have these elements and here you 11290 07:21:53,200 --> 07:21:56,160 have how many elements you have five 11291 07:21:54,798 --> 07:21:58,240 elements right 11292 07:21:56,160 --> 07:21:59,280 and now you're dividing it into three 11293 07:21:58,240 --> 07:22:01,840 and two 11294 07:21:59,280 --> 07:22:03,440 now this is your first step the second 11295 07:22:01,840 --> 07:22:06,160 step will be this 11296 07:22:03,440 --> 07:22:08,320 so now will you go ahead and create this 11297 07:22:06,160 --> 07:22:10,798 as your third step that means you will 11298 07:22:08,320 --> 07:22:13,600 move on to this no because we saw unless 11299 07:22:10,798 --> 07:22:15,360 and until left is not there is 11300 07:22:13,600 --> 07:22:16,160 no left left 11301 07:22:15,360 --> 07:22:18,160 right 11302 07:22:16,160 --> 07:22:20,160 we will not go to the right so this is 11303 07:22:18,160 --> 07:22:22,798 your second step then this will be your 11304 07:22:20,160 --> 07:22:25,680 third step this will be your fourth step 11305 07:22:22,798 --> 07:22:27,040 now you will move on to your fifth step 11306 07:22:25,680 --> 07:22:29,200 right 11307 07:22:27,040 --> 07:22:32,080 and now once you don't have anything on 11308 07:22:29,200 --> 07:22:34,878 the left nothing on the right then what 11309 07:22:32,080 --> 07:22:37,840 happens will this be your sixth step no 11310 07:22:34,878 --> 07:22:40,958 your sixth step will be merge so let's 11311 07:22:37,840 --> 07:22:42,958 move on to merge now 11312 07:22:40,958 --> 07:22:45,360 so in the merge function if you see the 11313 07:22:42,958 --> 07:22:48,000 algorithm for that is simple that you 11314 07:22:45,360 --> 07:22:49,840 create two sub arrays that is the one is 11315 07:22:48,000 --> 07:22:51,920 your left array and another is your 11316 07:22:49,840 --> 07:22:53,680 right sub array now in this you have 11317 07:22:51,920 --> 07:22:56,000 obviously in the last case if you have 11318 07:22:53,680 --> 07:22:57,520 seen we have a single element here and a 11319 07:22:56,000 --> 07:23:00,878 single element here 11320 07:22:57,520 --> 07:23:02,478 now once we have deduced out the 11321 07:23:00,878 --> 07:23:04,240 length of these arrays what should be 11322 07:23:02,478 --> 07:23:06,638 the length of these sub arrays and we 11323 07:23:04,240 --> 07:23:08,320 have declared the length or declared 11324 07:23:06,638 --> 07:23:10,320 these arrays and then we have 11325 07:23:08,320 --> 07:23:12,878 initialized these arrays once these 11326 07:23:10,320 --> 07:23:14,878 three steps are done but then what we 11327 07:23:12,878 --> 07:23:18,160 are going to do we are going to create 11328 07:23:14,878 --> 07:23:20,958 three iterators i j and k and those 11329 07:23:18,160 --> 07:23:23,680 iterators deal with i iterator will deal 11330 07:23:20,958 --> 07:23:25,920 with the left array j iterator deal with 11331 07:23:23,680 --> 07:23:27,680 right array and k with the original 11332 07:23:25,920 --> 07:23:29,680 array which helps us to insert the 11333 07:23:27,680 --> 07:23:32,000 elements so 11334 07:23:29,680 --> 07:23:33,600 once we we have everything in place what 11335 07:23:32,000 --> 07:23:36,080 we are going to do the next step is 11336 07:23:33,600 --> 07:23:38,000 comparing the values right if this 11337 07:23:36,080 --> 07:23:40,160 element that is the element in left 11338 07:23:38,000 --> 07:23:42,000 array is less than the element in the 11339 07:23:40,160 --> 07:23:43,760 right array we are going to insert that 11340 07:23:42,000 --> 07:23:46,000 in the original array so now let's 11341 07:23:43,760 --> 07:23:47,520 suppose you have 10 here and 23 here so 11342 07:23:46,000 --> 07:23:49,440 10 is less than so we are going to 11343 07:23:47,520 --> 07:23:52,000 insert this and we increment the k 11344 07:23:49,440 --> 07:23:53,600 pointer and now also our i pointer 11345 07:23:52,000 --> 07:23:55,200 pointer will be incremented it was 11346 07:23:53,600 --> 07:23:57,200 earlier it was zero and it will go to 11347 07:23:55,200 --> 07:23:59,120 one right and 11348 07:23:57,200 --> 07:24:02,398 now what happens now our 11349 07:23:59,120 --> 07:24:05,120 our i is pointing to one and our length 11350 07:24:02,398 --> 07:24:08,638 is also one so now that in that case 11351 07:24:05,120 --> 07:24:10,080 when one of the array is exhausted the 11352 07:24:08,638 --> 07:24:11,920 next array whichever is the layer 11353 07:24:10,080 --> 07:24:14,320 whichever is left right it can be either 11354 07:24:11,920 --> 07:24:16,478 the left array or the right array those 11355 07:24:14,320 --> 07:24:18,958 elements will be directly inserted in 11356 07:24:16,478 --> 07:24:21,920 the original area because we know for 11357 07:24:18,958 --> 07:24:25,600 the fact that both of these left as well 11358 07:24:21,920 --> 07:24:28,638 as right arrays will be sorted in itself 11359 07:24:25,600 --> 07:24:30,958 okay so let's see what is the next step 11360 07:24:28,638 --> 07:24:33,520 in the demonstration so 11361 07:24:30,958 --> 07:24:35,200 here we had our steps right and this 11362 07:24:33,520 --> 07:24:36,798 will be the sixth step wherein we are 11363 07:24:35,200 --> 07:24:39,040 going to merge this thing 11364 07:24:36,798 --> 07:24:41,920 now will we will this be your seventh 11365 07:24:39,040 --> 07:24:44,840 step no your seventh step will not be 11366 07:24:41,920 --> 07:24:47,440 this your seventh step will be 11367 07:24:44,840 --> 07:24:49,280 here this will be your seventh step now 11368 07:24:47,440 --> 07:24:51,360 six step is done now you will be 11369 07:24:49,280 --> 07:24:53,920 dividing it and you will be creating all 11370 07:24:51,360 --> 07:24:55,680 those arrays now once you have your 11371 07:24:53,920 --> 07:24:57,920 right array and there is no right 11372 07:24:55,680 --> 07:25:00,080 because this left is already done and 11373 07:24:57,920 --> 07:25:02,958 now you had your like right left now 11374 07:25:00,080 --> 07:25:06,080 this is also done now your eighth step 11375 07:25:02,958 --> 07:25:06,878 will be this 11376 07:25:06,080 --> 07:25:08,798 that 11377 07:25:06,878 --> 07:25:10,878 you will be merging it this will be your 11378 07:25:08,798 --> 07:25:13,520 eighth step then you will be merging it 11379 07:25:10,878 --> 07:25:15,280 now will this be your ninth step no you 11380 07:25:13,520 --> 07:25:17,120 have your this array that is your left 11381 07:25:15,280 --> 07:25:19,840 array in place but what about the right 11382 07:25:17,120 --> 07:25:22,240 arrow is this in place no it is not so 11383 07:25:19,840 --> 07:25:24,718 now let's try to calculate that 11384 07:25:22,240 --> 07:25:27,440 now what will be your ninth step this 11385 07:25:24,718 --> 07:25:29,200 left is done this is your ninth step 11386 07:25:27,440 --> 07:25:30,638 then what will be your tenth step this 11387 07:25:29,200 --> 07:25:33,520 is your tenth step 11388 07:25:30,638 --> 07:25:35,600 well now there is no left right now 11389 07:25:33,520 --> 07:25:38,878 we'll move on to this right so this is 11390 07:25:35,600 --> 07:25:41,040 your 11th step because this is the right 11391 07:25:38,878 --> 07:25:43,280 side of it right and now when you don't 11392 07:25:41,040 --> 07:25:45,280 have anything on the right now you will 11393 07:25:43,280 --> 07:25:48,798 be merging these two steps and this will 11394 07:25:45,280 --> 07:25:51,680 be your 12th step which is over here 11395 07:25:48,798 --> 07:25:54,000 so this is your 12th step 11396 07:25:51,680 --> 07:25:56,478 that means you will be merging these two 11397 07:25:54,000 --> 07:25:58,798 and the final sorted array 11398 07:25:56,478 --> 07:26:01,600 is this array and it you will get this 11399 07:25:58,798 --> 07:26:04,080 array in the 12th step so you see 10 11 11400 07:26:01,600 --> 07:26:05,760 16 20 and 30. 11401 07:26:04,080 --> 07:26:08,478 now if you observe carefully you have 11402 07:26:05,760 --> 07:26:10,718 these individual arrays one and one so 11403 07:26:08,478 --> 07:26:13,440 now while you are merging them you are 11404 07:26:10,718 --> 07:26:15,440 also sorting them so the left array 11405 07:26:13,440 --> 07:26:17,440 is sorted and same thing happens on the 11406 07:26:15,440 --> 07:26:18,478 right hand side as well if you see three 11407 07:26:17,440 --> 07:26:20,558 and four 11408 07:26:18,478 --> 07:26:23,040 these two elements are sorted in this 11409 07:26:20,558 --> 07:26:24,718 left array so this is the reason in the 11410 07:26:23,040 --> 07:26:26,878 right area right 11411 07:26:24,718 --> 07:26:29,760 not the left array so now when you are 11412 07:26:26,878 --> 07:26:32,398 merging them you will get again an array 11413 07:26:29,760 --> 07:26:35,040 which is sorted in itself you see 10 16 11414 07:26:32,398 --> 07:26:38,478 and 23. so if one of the array is 11415 07:26:35,040 --> 07:26:41,200 exhausted the next array elements can be 11416 07:26:38,478 --> 07:26:43,760 directly inserted in your original array 11417 07:26:41,200 --> 07:26:45,840 let me erase this and you see you have 11418 07:26:43,760 --> 07:26:47,440 your left array which is sorted and then 11419 07:26:45,840 --> 07:26:49,920 you are merging it with the right array 11420 07:26:47,440 --> 07:26:52,798 which is also sorted now if one of the 11421 07:26:49,920 --> 07:26:54,558 arrays is exhausted the next array 11422 07:26:52,798 --> 07:26:56,798 either it can be left array or the right 11423 07:26:54,558 --> 07:26:58,878 array the elements from that array can 11424 07:26:56,798 --> 07:27:01,120 be directly inserted in the original 11425 07:26:58,878 --> 07:27:03,360 array because we know the elements 11426 07:27:01,120 --> 07:27:06,398 itself in either of the arrays either 11427 07:27:03,360 --> 07:27:09,520 the left or the right are sorted okay so 11428 07:27:06,398 --> 07:27:10,558 this is how you execute your merge 11429 07:27:09,520 --> 07:27:13,840 function 11430 07:27:10,558 --> 07:27:16,638 so here is the program for merge sort in 11431 07:27:13,840 --> 07:27:19,040 python so how does this merge sort work 11432 07:27:16,638 --> 07:27:21,200 generally one single array will be 11433 07:27:19,040 --> 07:27:23,680 broken into two different pieces again 11434 07:27:21,200 --> 07:27:26,718 those two different sub arrays will be 11435 07:27:23,680 --> 07:27:29,280 broken into sub sub arrays so 11436 07:27:26,718 --> 07:27:31,200 after that whatever the answers we get 11437 07:27:29,280 --> 07:27:33,360 at the last will be combined together in 11438 07:27:31,200 --> 07:27:36,080 order to finish the sorting of that 11439 07:27:33,360 --> 07:27:38,398 particular array so we are merging all 11440 07:27:36,080 --> 07:27:41,600 the answers which we got from the sub 11441 07:27:38,398 --> 07:27:44,320 arrays to make a final result so quickly 11442 07:27:41,600 --> 07:27:46,240 let's see what do we do in order to have 11443 07:27:44,320 --> 07:27:48,478 a merge sort in python 11444 07:27:46,240 --> 07:27:51,040 so first we want an array which has been 11445 07:27:48,478 --> 07:27:53,120 passed through this merge sort function 11446 07:27:51,040 --> 07:27:55,040 so what happens inside this function 11447 07:27:53,120 --> 07:27:57,120 first the length of the 11448 07:27:55,040 --> 07:27:59,840 array has been calculated once that is 11449 07:27:57,120 --> 07:28:02,718 calculated it has been divided by two so 11450 07:27:59,840 --> 07:28:04,000 it gets left and right parts of an array 11451 07:28:02,718 --> 07:28:06,958 right 11452 07:28:04,000 --> 07:28:09,920 so after sorting uh the array into two 11453 07:28:06,958 --> 07:28:11,600 different halves we have merge sorting 11454 07:28:09,920 --> 07:28:14,080 left side of an array merge sorting 11455 07:28:11,600 --> 07:28:16,798 right side of an array right then we'll 11456 07:28:14,080 --> 07:28:18,558 perform the while operation 11457 07:28:16,798 --> 07:28:21,600 here with the help of the looping 11458 07:28:18,558 --> 07:28:24,718 systems so we'll first try to 11459 07:28:21,600 --> 07:28:26,398 check out whether we have the 11460 07:28:24,718 --> 07:28:28,638 right array 11461 07:28:26,398 --> 07:28:32,558 less than the length of an array of the 11462 07:28:28,638 --> 07:28:34,958 left and then again left array it is 11463 07:28:32,558 --> 07:28:36,798 less than length of the right array so 11464 07:28:34,958 --> 07:28:40,080 we try to 11465 07:28:36,798 --> 07:28:43,360 merge and we try to solve the elements 11466 07:28:40,080 --> 07:28:46,080 then and there itself so later we'll go 11467 07:28:43,360 --> 07:28:48,240 back to the left and right parts of 11468 07:28:46,080 --> 07:28:50,160 while loop here we have length of an 11469 07:28:48,240 --> 07:28:52,000 array towards the left side we are 11470 07:28:50,160 --> 07:28:54,080 checking whether it is 11471 07:28:52,000 --> 07:28:56,478 lesser than or greater than and 11472 07:28:54,080 --> 07:28:59,760 accordingly we are deciding where we 11473 07:28:56,478 --> 07:29:03,120 have to merge the answers what we have 11474 07:28:59,760 --> 07:29:06,718 got from the sub arrays right so then we 11475 07:29:03,120 --> 07:29:09,120 will always have a printing option of 11476 07:29:06,718 --> 07:29:11,360 this particular arrays we will do that 11477 07:29:09,120 --> 07:29:13,440 in the last before that in order to 11478 07:29:11,360 --> 07:29:15,680 merge all the answers we have got from 11479 07:29:13,440 --> 07:29:18,718 all the subarrays we'll be using for 11480 07:29:15,680 --> 07:29:19,680 loop here right so all the array answers 11481 07:29:18,718 --> 07:29:22,558 will be 11482 07:29:19,680 --> 07:29:25,920 submerged and we'll get the final sorted 11483 07:29:22,558 --> 07:29:28,320 array which is of uh so many elements 11484 07:29:25,920 --> 07:29:30,638 which is there in the uh input given by 11485 07:29:28,320 --> 07:29:32,398 the user say for example five different 11486 07:29:30,638 --> 07:29:34,718 elements but they're in an array so 11487 07:29:32,398 --> 07:29:37,040 after combining all the sub arrays 11488 07:29:34,718 --> 07:29:38,638 answers we'll get the five sorted 11489 07:29:37,040 --> 07:29:41,040 ascending order 11490 07:29:38,638 --> 07:29:44,000 elements in the array by using merge 11491 07:29:41,040 --> 07:29:47,200 sorter so let's quickly have a 11492 07:29:44,000 --> 07:29:49,920 look at it how does this particular 11493 07:29:47,200 --> 07:29:51,840 merge sort will work so uh this is set 11494 07:29:49,920 --> 07:29:54,000 of an array with eight different 11495 07:29:51,840 --> 07:29:56,478 elements inside that which is not sorted 11496 07:29:54,000 --> 07:29:58,398 we have to sort that once this array has 11497 07:29:56,478 --> 07:30:00,320 been passed through the merge sorting 11498 07:29:58,398 --> 07:30:03,040 function it will perform all the 11499 07:30:00,320 --> 07:30:05,600 operations finally it will merge all the 11500 07:30:03,040 --> 07:30:09,520 sorted arrays and it will display 11501 07:30:05,600 --> 07:30:11,760 in the print list right so 11502 07:30:09,520 --> 07:30:13,920 let's quickly run this program and check 11503 07:30:11,760 --> 07:30:13,920 out 11504 07:30:14,000 --> 07:30:17,920 even though if it is we are mentioning 11505 07:30:16,080 --> 07:30:19,840 the words array but we are using list 11506 07:30:17,920 --> 07:30:20,878 here in python in order to store it 11507 07:30:19,840 --> 07:30:23,600 right 11508 07:30:20,878 --> 07:30:25,760 so this is the sorted array which we get 11509 07:30:23,600 --> 07:30:28,000 so here we could see we don't have a 11510 07:30:25,760 --> 07:30:30,398 sorted array but here it is sorted in 11511 07:30:28,000 --> 07:30:32,638 ascending order that is smallest to the 11512 07:30:30,398 --> 07:30:35,040 highest so this is all about the merge 11513 07:30:32,638 --> 07:30:39,520 sort now let's quickly check out what is 11514 07:30:35,040 --> 07:30:41,520 greedy approach in programming right so 11515 07:30:39,520 --> 07:30:43,840 greedy approach is nothing but a simple 11516 07:30:41,520 --> 07:30:46,798 solution for the problem which will give 11517 07:30:43,840 --> 07:30:50,558 you exact correct solution it is a 11518 07:30:46,798 --> 07:30:53,760 step-by-step approach it is finding the 11519 07:30:50,558 --> 07:30:57,600 answer in step by step way and minimal 11520 07:30:53,760 --> 07:31:00,320 time right so efficiency plus minimal 11521 07:30:57,600 --> 07:31:02,718 time is equal to greedy approach so that 11522 07:31:00,320 --> 07:31:05,280 is what i can let you know 11523 07:31:02,718 --> 07:31:06,320 apart from that we have a benefit here 11524 07:31:05,280 --> 07:31:11,280 so 11525 07:31:06,320 --> 07:31:14,398 we can get a simple solution very quick 11526 07:31:11,280 --> 07:31:16,718 manner and we can also trace out very 11527 07:31:14,398 --> 07:31:18,878 well so some of the applications of 11528 07:31:16,718 --> 07:31:21,680 greedy approach are 11529 07:31:18,878 --> 07:31:26,320 prim's spanning tree algorithm travel 11530 07:31:21,680 --> 07:31:29,440 salesman problem etc so in order to have 11531 07:31:26,320 --> 07:31:31,840 a connect let me quickly give you an 11532 07:31:29,440 --> 07:31:34,558 introduction to prim's panning tree 11533 07:31:31,840 --> 07:31:37,040 algorithm so what is this all about the 11534 07:31:34,558 --> 07:31:39,920 spanning tree algorithm so 11535 07:31:37,040 --> 07:31:43,760 generally a tree is one of the kind of 11536 07:31:39,920 --> 07:31:44,638 graph which is having vertices and edges 11537 07:31:43,760 --> 07:31:48,080 and 11538 07:31:44,638 --> 07:31:50,478 mention there is no cycle specially one 11539 07:31:48,080 --> 07:31:54,240 vertex has not been joined to another 11540 07:31:50,478 --> 07:31:56,638 vertex forming the cycle right so a 11541 07:31:54,240 --> 07:31:58,638 spanning tree next word so what is the 11542 07:31:56,638 --> 07:32:00,398 spanning tree it is having all the 11543 07:31:58,638 --> 07:32:03,280 vertices connected 11544 07:32:00,398 --> 07:32:05,040 in a constructed format right 11545 07:32:03,280 --> 07:32:06,398 uh when you have a connections of 11546 07:32:05,040 --> 07:32:09,280 advertisers 11547 07:32:06,398 --> 07:32:12,160 constructively forming a graph 11548 07:32:09,280 --> 07:32:14,638 then that particular graph can make 11549 07:32:12,160 --> 07:32:17,200 spanning trees it can be multiple 11550 07:32:14,638 --> 07:32:19,600 spanning trees say for example 11551 07:32:17,200 --> 07:32:21,440 so this is one spanning tree 11552 07:32:19,600 --> 07:32:23,120 and you have one more spanning tree 11553 07:32:21,440 --> 07:32:24,320 which is 11554 07:32:23,120 --> 07:32:25,440 like this 11555 07:32:24,320 --> 07:32:28,080 so 11556 07:32:25,440 --> 07:32:31,760 connects between the vertices forms a 11557 07:32:28,080 --> 07:32:33,760 spanning tree right so next what is 11558 07:32:31,760 --> 07:32:36,638 minimal spanning tree 11559 07:32:33,760 --> 07:32:39,440 so that means every node to node 11560 07:32:36,638 --> 07:32:44,000 connection vertex to vertex connection 11561 07:32:39,440 --> 07:32:49,120 will be having certain values say v1 v2 11562 07:32:44,000 --> 07:32:52,080 v3 and so on vn right so 11563 07:32:49,120 --> 07:32:54,798 reaching a source to destination or 11564 07:32:52,080 --> 07:32:58,638 visiting the every node in the tree or 11565 07:32:54,798 --> 07:33:01,520 the graph at least once using a minimal 11566 07:32:58,638 --> 07:33:03,760 value for the edges given then that 11567 07:33:01,520 --> 07:33:06,478 particular tree is known as minimal 11568 07:33:03,760 --> 07:33:09,200 spanning tree so whose sum of weights of 11569 07:33:06,478 --> 07:33:10,878 edges is minimum so from 11570 07:33:09,200 --> 07:33:13,440 the source through the destination if 11571 07:33:10,878 --> 07:33:15,680 you calculate the sum of the edges was 11572 07:33:13,440 --> 07:33:18,000 always minimum then we 11573 07:33:15,680 --> 07:33:21,600 let that particular tree known as 11574 07:33:18,000 --> 07:33:24,398 minimal spanning tree so this is a brief 11575 07:33:21,600 --> 07:33:26,958 introduction towards prims minimal 11576 07:33:24,398 --> 07:33:28,718 spanning tree algorithm so let me 11577 07:33:26,958 --> 07:33:31,360 quickly see 11578 07:33:28,718 --> 07:33:35,120 with an example let you know 11579 07:33:31,360 --> 07:33:38,080 how does this work right now this 11580 07:33:35,120 --> 07:33:39,680 particular example is a spanning tree it 11581 07:33:38,080 --> 07:33:42,080 is not having 11582 07:33:39,680 --> 07:33:45,200 anything 11583 07:33:42,080 --> 07:33:47,680 mentioned minimum or maximum we are here 11584 07:33:45,200 --> 07:33:50,160 in order to find minimal spanning tree 11585 07:33:47,680 --> 07:33:53,280 so what we have to consider we have to 11586 07:33:50,160 --> 07:33:55,680 consider any one node and we have to 11587 07:33:53,280 --> 07:33:58,638 start traveling from one node to another 11588 07:33:55,680 --> 07:34:01,040 node in a minimal possible way 11589 07:33:58,638 --> 07:34:03,680 the edges is having values edge value 11590 07:34:01,040 --> 07:34:06,478 should be minimum always because we call 11591 07:34:03,680 --> 07:34:08,320 it as weight right edge weight should be 11592 07:34:06,478 --> 07:34:10,478 minimum when you 11593 07:34:08,320 --> 07:34:13,760 add all the 11594 07:34:10,478 --> 07:34:16,798 edges you have to get the minimum number 11595 07:34:13,760 --> 07:34:19,920 not the highest number so in order to 11596 07:34:16,798 --> 07:34:24,320 give a clear picture let me quickly take 11597 07:34:19,920 --> 07:34:27,600 one node here so i'm taking node b so 11598 07:34:24,320 --> 07:34:30,080 you have to see this is iteration 1 11599 07:34:27,600 --> 07:34:32,878 right so node b is taken into 11600 07:34:30,080 --> 07:34:35,440 consideration if you could see node b 11601 07:34:32,878 --> 07:34:38,638 has two different ways i mean two 11602 07:34:35,440 --> 07:34:41,280 different sub nodes it is there so one 11603 07:34:38,638 --> 07:34:45,680 we have d one we have c what is the 11604 07:34:41,280 --> 07:34:48,558 rules we should have the value h value 11605 07:34:45,680 --> 07:34:52,398 as minimum as possible so 11606 07:34:48,558 --> 07:34:56,478 11 or 5 minimum 11 is greater 5 is 11607 07:34:52,398 --> 07:34:57,760 smaller so i am traveling from b to d 11608 07:34:56,478 --> 07:35:00,558 okay 11609 07:34:57,760 --> 07:35:01,440 b to d is 5 11610 07:35:00,558 --> 07:35:02,878 10 11611 07:35:01,440 --> 07:35:04,558 coming to d 11612 07:35:02,878 --> 07:35:06,478 what are the different 11613 07:35:04,558 --> 07:35:08,718 sub nodes you have or the connections 11614 07:35:06,478 --> 07:35:10,160 you have edges you have for d 11615 07:35:08,718 --> 07:35:13,280 d has 11616 07:35:10,160 --> 07:35:16,958 the connection from that node to c again 11617 07:35:13,280 --> 07:35:20,320 d to e if you compare both 11618 07:35:16,958 --> 07:35:22,638 d2 is having the value 2 d2c is having 11619 07:35:20,320 --> 07:35:25,760 the value 3 we are having the minimum 11620 07:35:22,638 --> 07:35:26,878 value in the path d to e the edge is 11621 07:35:25,760 --> 07:35:29,920 having 11622 07:35:26,878 --> 07:35:32,718 the minimum value so what do you do from 11623 07:35:29,920 --> 07:35:33,840 d you are going to e 11624 07:35:32,718 --> 07:35:37,680 okay 11625 07:35:33,840 --> 07:35:39,360 so with the edge value 2 11626 07:35:37,680 --> 07:35:40,080 hope this is clear 11627 07:35:39,360 --> 07:35:43,600 so 11628 07:35:40,080 --> 07:35:44,798 from e if you take how you can travel 11629 07:35:43,600 --> 07:35:46,958 you have 11630 07:35:44,798 --> 07:35:51,360 only one single 11631 07:35:46,958 --> 07:35:53,200 path to travel from e to c or further 11632 07:35:51,360 --> 07:35:56,240 right you don't have any other 11633 07:35:53,200 --> 07:35:59,360 probability in order to reach c 11634 07:35:56,240 --> 07:36:03,520 right so also you can you can go back 11635 07:35:59,360 --> 07:36:05,760 and check so it it forms five again so 11636 07:36:03,520 --> 07:36:10,240 without that we can also have the 11637 07:36:05,760 --> 07:36:13,600 connections so e to c is having seven 11638 07:36:10,240 --> 07:36:16,878 right so when you have 7 uh and if you 11639 07:36:13,600 --> 07:36:20,718 add 2 plus 3 it is 5 so think how you 11640 07:36:16,878 --> 07:36:22,240 will proceed so from e to d back again 11641 07:36:20,718 --> 07:36:27,520 and then c 11642 07:36:22,240 --> 07:36:30,478 or e to c so you have to have a minimum 11643 07:36:27,520 --> 07:36:33,680 value and the thing is you should not 11644 07:36:30,478 --> 07:36:37,040 repeat the nodes again so here there is 11645 07:36:33,680 --> 07:36:40,160 no possibility way you have to go to c 11646 07:36:37,040 --> 07:36:43,200 with 7 only you can't think the way i 11647 07:36:40,160 --> 07:36:46,320 thought you now so it was just a leading 11648 07:36:43,200 --> 07:36:49,200 you into a thought that can you go back 11649 07:36:46,320 --> 07:36:52,398 add together like 2 plus 3 is 5 right 5 11650 07:36:49,200 --> 07:36:55,520 is lesser than 7 how i can go back in 11651 07:36:52,398 --> 07:36:58,398 just go to c no you cannot do that right 11652 07:36:55,520 --> 07:37:00,478 you should visit once at least once it 11653 07:36:58,398 --> 07:37:05,200 is visited you cannot go back 11654 07:37:00,478 --> 07:37:08,000 so it is seven done so next from c to a 11655 07:37:05,200 --> 07:37:10,000 you have one connection so see today 11656 07:37:08,000 --> 07:37:11,200 only one connection so again you are 11657 07:37:10,000 --> 07:37:13,280 going 11658 07:37:11,200 --> 07:37:15,600 to a with the help of 11659 07:37:13,280 --> 07:37:17,200 four so you don't have any other nodes 11660 07:37:15,600 --> 07:37:20,638 which is connected to c in order to 11661 07:37:17,200 --> 07:37:24,240 think how to reach a right so if you add 11662 07:37:20,638 --> 07:37:30,160 all together you will get a minimum 11663 07:37:24,240 --> 07:37:31,280 spanning tree say for example 7 plus 4 11664 07:37:30,160 --> 07:37:32,478 plus 11665 07:37:31,280 --> 07:37:35,360 2 11666 07:37:32,478 --> 07:37:37,760 again you will have 5 11667 07:37:35,360 --> 07:37:40,320 right so 11668 07:37:37,760 --> 07:37:43,760 this is 7 so you have 11669 07:37:40,320 --> 07:37:47,520 11 so the answer is 18 11670 07:37:43,760 --> 07:37:50,478 right so this is how the minimum 11671 07:37:47,520 --> 07:37:52,398 spanning tree will work right prim's 11672 07:37:50,478 --> 07:37:54,320 minimum spanning tree 11673 07:37:52,398 --> 07:37:55,920 so let's quickly implement this 11674 07:37:54,320 --> 07:37:58,558 particular 11675 07:37:55,920 --> 07:38:01,440 problem or algorithm into a python 11676 07:37:58,558 --> 07:38:03,680 programming code so let's implement and 11677 07:38:01,440 --> 07:38:06,478 check out on google collab how does this 11678 07:38:03,680 --> 07:38:09,600 prims spanning tree will work 11679 07:38:06,478 --> 07:38:12,718 so here is an ide that is google collab 11680 07:38:09,600 --> 07:38:14,080 where we have prim's algorithm program 11681 07:38:12,718 --> 07:38:16,718 in python 11682 07:38:14,080 --> 07:38:20,638 so let's understand how this program 11683 07:38:16,718 --> 07:38:23,840 flow works so first we'll be considering 11684 07:38:20,638 --> 07:38:25,840 the infinity term so we are mentioning 11685 07:38:23,840 --> 07:38:29,200 infinity to 11686 07:38:25,840 --> 07:38:31,920 so many nine together in order to have 11687 07:38:29,200 --> 07:38:33,680 as many as combinations as possible so 11688 07:38:31,920 --> 07:38:36,558 next we are considering the number of 11689 07:38:33,680 --> 07:38:40,000 vertices for the graphs so we have five 11690 07:38:36,558 --> 07:38:42,320 vertices for the graph so v is always 11691 07:38:40,000 --> 07:38:44,398 indicating the vertices of the graph so 11692 07:38:42,320 --> 07:38:46,638 number of vertices is five 11693 07:38:44,398 --> 07:38:49,440 after that we'll always form an 11694 07:38:46,638 --> 07:38:52,478 adjacency matrix of rows versus column 11695 07:38:49,440 --> 07:38:54,478 2d that is rows versus columns phi cross 11696 07:38:52,478 --> 07:38:57,440 prime matrix will be 11697 07:38:54,478 --> 07:38:59,440 present so here i've formed a graph with 11698 07:38:57,440 --> 07:39:02,080 adjacency matrix 11699 07:38:59,440 --> 07:39:05,040 which is represented what is adjacency 11700 07:39:02,080 --> 07:39:08,478 i'll just quickly tell you we are having 11701 07:39:05,040 --> 07:39:11,920 zeros in the diagonal and the 11702 07:39:08,478 --> 07:39:15,440 parallel elements are same so if you 11703 07:39:11,920 --> 07:39:18,798 fold this particular square paper for 11704 07:39:15,440 --> 07:39:20,878 example into exact diagonally from 11705 07:39:18,798 --> 07:39:22,798 left top corner to right bottom corner 11706 07:39:20,878 --> 07:39:25,600 all the elements will match to each 11707 07:39:22,798 --> 07:39:28,798 other so this kind of matrix is known as 11708 07:39:25,600 --> 07:39:29,760 adjacency matrix so after that what do 11709 07:39:28,798 --> 07:39:31,680 we do 11710 07:39:29,760 --> 07:39:34,240 we need to have an array in order to 11711 07:39:31,680 --> 07:39:36,160 track the vertex which we select so 11712 07:39:34,240 --> 07:39:38,160 whatever the vertex we select we have to 11713 07:39:36,160 --> 07:39:39,760 have the track of it whether is it 11714 07:39:38,160 --> 07:39:42,240 matching whether it is having the 11715 07:39:39,760 --> 07:39:44,320 minimal edge how it is doing what it is 11716 07:39:42,240 --> 07:39:48,000 doing so in order to track we have to 11717 07:39:44,320 --> 07:39:51,200 have an uh vertex uh selected 11718 07:39:48,000 --> 07:39:54,398 array where uh it is having um 11719 07:39:51,200 --> 07:39:56,558 vertexes which we have selected then 11720 07:39:54,398 --> 07:39:58,878 selected will be true else it will be 11721 07:39:56,558 --> 07:40:01,440 false whatever you select will be true 11722 07:39:58,878 --> 07:40:03,680 first initially and then it will make 11723 07:40:01,440 --> 07:40:06,718 the comparisons and accordingly it will 11724 07:40:03,680 --> 07:40:09,440 change the values so the empty selected 11725 07:40:06,718 --> 07:40:12,478 array will be having five zeros inside 11726 07:40:09,440 --> 07:40:14,958 that so after that we should always set 11727 07:40:12,478 --> 07:40:17,280 the number of edges to zero it does not 11728 07:40:14,958 --> 07:40:20,000 mention how many edges it is there we 11729 07:40:17,280 --> 07:40:23,440 have to keep it zero because always 11730 07:40:20,000 --> 07:40:26,000 edges will be a vertex minus one we have 11731 07:40:23,440 --> 07:40:29,040 uh five vertex and edges will be four so 11732 07:40:26,000 --> 07:40:30,958 that is the logic it will work with so 11733 07:40:29,040 --> 07:40:34,000 that is in minimal spanning tree 11734 07:40:30,958 --> 07:40:36,558 especially so we have to choose 11735 07:40:34,000 --> 07:40:39,440 zeroth vertex and make it true always we 11736 07:40:36,558 --> 07:40:41,600 have to choose the vertex 0 and we have 11737 07:40:39,440 --> 07:40:44,798 to consider that as a true it is an 11738 07:40:41,600 --> 07:40:47,520 assumption in order to start the working 11739 07:40:44,798 --> 07:40:50,638 after that we have to check how do we 11740 07:40:47,520 --> 07:40:53,520 check we have to check the um 11741 07:40:50,638 --> 07:40:55,840 complete elements inside the matrix 11742 07:40:53,520 --> 07:40:58,558 accordingly row vice 11743 07:40:55,840 --> 07:41:01,120 column wise and we have to find the edge 11744 07:40:58,558 --> 07:41:04,160 which is having the minimal number 11745 07:41:01,120 --> 07:41:06,320 in order to traverse right 11746 07:41:04,160 --> 07:41:09,280 so after finishing all these things if 11747 07:41:06,320 --> 07:41:12,080 it is not selected if there is no edge 11748 07:41:09,280 --> 07:41:14,398 then it will consider the neighboring 11749 07:41:12,080 --> 07:41:17,120 vertex so if it is not forming if it is 11750 07:41:14,398 --> 07:41:19,440 not getting any weighted uh edge which 11751 07:41:17,120 --> 07:41:21,920 is having minimal weight then it will 11752 07:41:19,440 --> 07:41:24,718 try to correspondently 11753 07:41:21,920 --> 07:41:27,520 see the neighbor elements inside the 11754 07:41:24,718 --> 07:41:30,160 matrix right so then 11755 07:41:27,520 --> 07:41:32,718 after doing that will be printing the 11756 07:41:30,160 --> 07:41:34,398 edge and how it is traversing what is 11757 07:41:32,718 --> 07:41:37,440 the minimal value 11758 07:41:34,398 --> 07:41:39,360 at the end right so let's quickly print 11759 07:41:37,440 --> 07:41:40,638 this particular 11760 07:41:39,360 --> 07:41:44,958 output 11761 07:41:40,638 --> 07:41:44,958 execute and see how it will work 11762 07:41:45,520 --> 07:41:51,680 so here we have edge and weight so at 11763 07:41:49,120 --> 07:41:53,760 zero the weight you can you can go from 11764 07:41:51,680 --> 07:41:55,520 two to five again from two you can go to 11765 07:41:53,760 --> 07:41:57,520 three to one again for three you can go 11766 07:41:55,520 --> 07:42:00,718 for four to one and for four you can go 11767 07:41:57,520 --> 07:42:03,840 to one to two so these are the traversal 11768 07:42:00,718 --> 07:42:05,760 of minimum spanning tree that you can 11769 07:42:03,840 --> 07:42:08,000 make in python 11770 07:42:05,760 --> 07:42:11,280 now we'll be learning regarding dynamic 11771 07:42:08,000 --> 07:42:13,520 programming so dynamic programming is an 11772 07:42:11,280 --> 07:42:15,680 approach in order to solve a problem in 11773 07:42:13,520 --> 07:42:17,680 data structures so 11774 07:42:15,680 --> 07:42:20,080 what does this particular dynamic 11775 07:42:17,680 --> 07:42:22,718 programming will do it will give you the 11776 07:42:20,080 --> 07:42:24,320 best and the most optimal solution to 11777 07:42:22,718 --> 07:42:26,958 the problem 11778 07:42:24,320 --> 07:42:29,440 how it will give you it will give you by 11779 07:42:26,958 --> 07:42:32,240 recursively working on the problem 11780 07:42:29,440 --> 07:42:35,840 comparing the solution one to another 11781 07:42:32,240 --> 07:42:39,040 and finding the best and ultimate 11782 07:42:35,840 --> 07:42:42,000 solution for you at the end so this is 11783 07:42:39,040 --> 07:42:45,440 how dynamic programming will work 11784 07:42:42,000 --> 07:42:48,320 it is not letting you on with only one 11785 07:42:45,440 --> 07:42:51,680 single solution it will always give you 11786 07:42:48,320 --> 07:42:54,478 multiple solution which is comparatively 11787 07:42:51,680 --> 07:42:56,478 common and which is most efficient it 11788 07:42:54,478 --> 07:42:59,200 will give you amongst that it will 11789 07:42:56,478 --> 07:43:01,440 consider to be the best one so 11790 07:42:59,200 --> 07:43:04,000 applications of this dynamic programming 11791 07:43:01,440 --> 07:43:07,040 so where do we apply this particular 11792 07:43:04,000 --> 07:43:09,600 programming approach so matrix chain 11793 07:43:07,040 --> 07:43:11,600 multiplication is there and 11794 07:43:09,600 --> 07:43:13,920 one famous element that is stubborn of 11795 07:43:11,600 --> 07:43:16,798 hanoi so people would have heard about 11796 07:43:13,920 --> 07:43:18,240 aware of ni it is a very simple 11797 07:43:16,798 --> 07:43:20,638 mathematical problem statements 11798 07:43:18,240 --> 07:43:23,440 generally we use dynamic programming in 11799 07:43:20,638 --> 07:43:26,638 order to have a best 11800 07:43:23,440 --> 07:43:28,798 result out of this particular program so 11801 07:43:26,638 --> 07:43:30,878 let's quickly check out what is cover of 11802 07:43:28,798 --> 07:43:32,958 hanoi as an example for dynamic 11803 07:43:30,878 --> 07:43:35,360 programming so 11804 07:43:32,958 --> 07:43:38,080 as i mentioned tower of hanoi is a 11805 07:43:35,360 --> 07:43:39,200 mathematical puzzle right so it is 11806 07:43:38,080 --> 07:43:41,840 having 11807 07:43:39,200 --> 07:43:44,000 pegs and this text in the sense people 11808 07:43:41,840 --> 07:43:46,398 who are listening to the first time so 11809 07:43:44,000 --> 07:43:49,360 i'm just drawing a picture here so you 11810 07:43:46,398 --> 07:43:52,240 are having a stand here and 11811 07:43:49,360 --> 07:43:54,798 you have three different 11812 07:43:52,240 --> 07:43:55,680 rods stick like things there will be 11813 07:43:54,798 --> 07:43:58,478 disc 11814 07:43:55,680 --> 07:44:01,200 which is arranged on this uh usually the 11815 07:43:58,478 --> 07:44:05,040 children who play uh small children will 11816 07:44:01,200 --> 07:44:07,680 be having this particular stack in this 11817 07:44:05,040 --> 07:44:09,520 triangle format which is having the 11818 07:44:07,680 --> 07:44:11,520 biggest ring upon that there's the 11819 07:44:09,520 --> 07:44:14,398 smallest one at the end you will find 11820 07:44:11,520 --> 07:44:18,000 the very smallest ring so 11821 07:44:14,398 --> 07:44:20,558 like that we have x and diff arrangement 11822 07:44:18,000 --> 07:44:22,478 for tower of nine so 11823 07:44:20,558 --> 07:44:24,638 there are three different pecs which we 11824 07:44:22,478 --> 07:44:28,080 could consider which i wrote here ah 11825 07:44:24,638 --> 07:44:31,120 name it as x y and z okay three 11826 07:44:28,080 --> 07:44:33,760 different pegs and three different disks 11827 07:44:31,120 --> 07:44:37,440 you are only having 11828 07:44:33,760 --> 07:44:40,718 pex three and disc 3 here in 11829 07:44:37,440 --> 07:44:43,200 tavrophenoid and you can increase this 11830 07:44:40,718 --> 07:44:46,080 how much ever you want it is always in 11831 07:44:43,200 --> 07:44:49,280 the format n okay you have a famous 11832 07:44:46,080 --> 07:44:54,638 formula for towards the phenoid that is 11833 07:44:49,280 --> 07:44:57,040 2 to the power -1 so in order to find uh 11834 07:44:54,638 --> 07:45:00,000 2 from the source to the destination if 11835 07:44:57,040 --> 07:45:02,478 you want to put all the rings without 11836 07:45:00,000 --> 07:45:05,120 merging without putting the largest on 11837 07:45:02,478 --> 07:45:06,958 the smallest or without repeating or any 11838 07:45:05,120 --> 07:45:07,920 other mistakes without breaking the 11839 07:45:06,958 --> 07:45:10,478 rules 11840 07:45:07,920 --> 07:45:13,440 you can find how many moves you want to 11841 07:45:10,478 --> 07:45:15,920 do that say for example uh 2 to the 11842 07:45:13,440 --> 07:45:18,478 power of 3 you have 3 different discs n 11843 07:45:15,920 --> 07:45:21,280 is always the how many disc you use in 11844 07:45:18,478 --> 07:45:24,080 this particular problem so i'm 11845 07:45:21,280 --> 07:45:25,760 considering 3 here so 2 to the power of 11846 07:45:24,080 --> 07:45:29,360 3 is 11847 07:45:25,760 --> 07:45:33,680 8 right so when you minus 1 from that 11848 07:45:29,360 --> 07:45:36,798 the answer is 7 so in order to traverse 11849 07:45:33,680 --> 07:45:39,360 three this from x to z from source to 11850 07:45:36,798 --> 07:45:42,638 destination you need seven different 11851 07:45:39,360 --> 07:45:45,040 moves so this is how you will calculate 11852 07:45:42,638 --> 07:45:47,120 the moves the movements you have to make 11853 07:45:45,040 --> 07:45:49,440 okay moves is nothing but moments you 11854 07:45:47,120 --> 07:45:51,600 have to make all the three this in order 11855 07:45:49,440 --> 07:45:55,920 to transfer from source to the 11856 07:45:51,600 --> 07:45:59,040 destination hope that's clear so 11857 07:45:55,920 --> 07:46:00,958 uh next you have to consider uh what are 11858 07:45:59,040 --> 07:46:03,600 the different rules you have in order to 11859 07:46:00,958 --> 07:46:06,798 solve this game or puzzle or problem 11860 07:46:03,600 --> 07:46:09,200 anything so the first thing is only one 11861 07:46:06,798 --> 07:46:12,080 this can be removed at a time from a 11862 07:46:09,200 --> 07:46:14,638 stand from a peg you can remove only one 11863 07:46:12,080 --> 07:46:16,958 disk at a time that is the first rule 11864 07:46:14,638 --> 07:46:19,680 the second rule is the larger this 11865 07:46:16,958 --> 07:46:21,920 cannot stand on the smaller tests say 11866 07:46:19,680 --> 07:46:24,478 for example you have a smaller disk that 11867 07:46:21,920 --> 07:46:28,718 is disk one here you can't put a larger 11868 07:46:24,478 --> 07:46:30,798 disk on the smaller disk so this is a 11869 07:46:28,718 --> 07:46:33,040 a second rule for this particular 11870 07:46:30,798 --> 07:46:36,320 problem statement so considering all 11871 07:46:33,040 --> 07:46:39,200 this let's see how do we implement tower 11872 07:46:36,320 --> 07:46:41,440 of hanai problem let's quickly hop into 11873 07:46:39,200 --> 07:46:44,398 the ide now 11874 07:46:41,440 --> 07:46:46,798 here we are on the ide so let's quickly 11875 07:46:44,398 --> 07:46:49,040 check out what a source named as what is 11876 07:46:46,798 --> 07:46:52,160 auxiliary is nothing but temporary which 11877 07:46:49,040 --> 07:46:53,520 is used in order uh for to transfer all 11878 07:46:52,160 --> 07:46:56,000 the three disks from the source to the 11879 07:46:53,520 --> 07:46:58,160 destination you want to have a place 11880 07:46:56,000 --> 07:47:02,240 where you can keep all the disks at 11881 07:46:58,160 --> 07:47:04,798 least for a iteration or for a moment 11882 07:47:02,240 --> 07:47:06,878 right so source is named as x i'm 11883 07:47:04,798 --> 07:47:08,718 talking about the pegs right the stands 11884 07:47:06,878 --> 07:47:10,398 three different stands you have the 11885 07:47:08,718 --> 07:47:11,360 first stand that is source 11886 07:47:10,398 --> 07:47:14,080 is 11887 07:47:11,360 --> 07:47:15,920 x and the temporary stand that is in the 11888 07:47:14,080 --> 07:47:18,958 middle the second one is y that is 11889 07:47:15,920 --> 07:47:22,240 auxiliary and the target destination is 11890 07:47:18,958 --> 07:47:24,478 z okay so you have all these three peg 11891 07:47:22,240 --> 07:47:27,360 arrangements and you are considering 11892 07:47:24,478 --> 07:47:30,160 three disks for example okay let's see 11893 07:47:27,360 --> 07:47:31,200 how this particular program works in 11894 07:47:30,160 --> 07:47:33,200 python 11895 07:47:31,200 --> 07:47:36,398 so first we are creating a function 11896 07:47:33,200 --> 07:47:37,360 called hanoi okay and you are having 11897 07:47:36,398 --> 07:47:41,440 diff 11898 07:47:37,360 --> 07:47:42,160 source auxiliary target that is x y and 11899 07:47:41,440 --> 07:47:44,320 z 11900 07:47:42,160 --> 07:47:46,718 and then you are considering all the 11901 07:47:44,320 --> 07:47:48,798 disk we are equating that to one after 11902 07:47:46,718 --> 07:47:51,440 that we are printing the moments from 11903 07:47:48,798 --> 07:47:54,160 which this one is going to from which 11904 07:47:51,440 --> 07:47:56,558 spec to which pick from source to 11905 07:47:54,160 --> 07:47:57,920 destination or source to auxiliary or 11906 07:47:56,558 --> 07:48:01,600 auxiliary to 11907 07:47:57,920 --> 07:48:03,440 uh destination so xyz the 11908 07:48:01,600 --> 07:48:06,160 program completely deals with the 11909 07:48:03,440 --> 07:48:08,798 combinations of xyz once i show you the 11910 07:48:06,160 --> 07:48:11,120 output you will get to know so 11911 07:48:08,798 --> 07:48:12,558 you are having a common 11912 07:48:11,120 --> 07:48:15,280 formula here 11913 07:48:12,558 --> 07:48:17,920 this minus 1 comma source common target 11914 07:48:15,280 --> 07:48:19,040 comma auxiliary so this is what we use 11915 07:48:17,920 --> 07:48:22,160 generally 11916 07:48:19,040 --> 07:48:24,558 again you have one more formula which is 11917 07:48:22,160 --> 07:48:26,718 considered so again this minus one 11918 07:48:24,558 --> 07:48:29,840 auxiliary first source next and then 11919 07:48:26,718 --> 07:48:33,600 target it is very easy to uh remember 11920 07:48:29,840 --> 07:48:34,958 and also easy to work with so at the end 11921 07:48:33,600 --> 07:48:37,200 what you will do 11922 07:48:34,958 --> 07:48:38,000 you will just uh 11923 07:48:37,200 --> 07:48:40,558 type 11924 07:48:38,000 --> 07:48:43,600 the user input right you have to ask the 11925 07:48:40,558 --> 07:48:46,240 user how many disk he or she wants to 11926 07:48:43,600 --> 07:48:48,478 enter and how you will shuffle that from 11927 07:48:46,240 --> 07:48:51,200 source to the destination how do you 11928 07:48:48,478 --> 07:48:53,200 make that disk which is given by the 11929 07:48:51,200 --> 07:48:54,478 user travel from the source to the 11930 07:48:53,200 --> 07:48:58,398 destination 11931 07:48:54,478 --> 07:49:00,798 then we will be calling the function so 11932 07:48:58,398 --> 07:49:03,280 disks will be having the name and the 11933 07:49:00,798 --> 07:49:04,000 disk itself and then we'll be using x y 11934 07:49:03,280 --> 07:49:05,920 z 11935 07:49:04,000 --> 07:49:08,958 instead of source auxiliary and target 11936 07:49:05,920 --> 07:49:11,040 the objects are created so let's quickly 11937 07:49:08,958 --> 07:49:14,600 check on this particular program what 11938 07:49:11,040 --> 07:49:14,600 will be the output 11939 07:49:32,240 --> 07:49:38,160 right it is asking the 11940 07:49:35,120 --> 07:49:41,280 number of disks you want to enter so i'm 11941 07:49:38,160 --> 07:49:44,240 writing three for now so i'm giving 11942 07:49:41,280 --> 07:49:46,160 enter so these are the different moves 11943 07:49:44,240 --> 07:49:49,040 so you could count here you will be 11944 07:49:46,160 --> 07:49:51,680 having seven moves for this three discs 11945 07:49:49,040 --> 07:49:54,160 i told you the formula 2 to the power n 11946 07:49:51,680 --> 07:49:56,718 minus 1 is used in order to calculate 11947 07:49:54,160 --> 07:49:58,798 how many moves this particular discs 11948 07:49:56,718 --> 07:50:01,520 will make in order to travel from source 11949 07:49:58,798 --> 07:50:05,680 to destination or source to target from 11950 07:50:01,520 --> 07:50:08,558 x to z right so this is about the towers 11951 07:50:05,680 --> 07:50:11,360 of anal implementation so let's quickly 11952 07:50:08,558 --> 07:50:14,000 check manually how does this particular 11953 07:50:11,360 --> 07:50:17,280 movement will happen in the 11954 07:50:14,000 --> 07:50:19,520 ppt again the same outputs which we got 11955 07:50:17,280 --> 07:50:23,520 on the google collab has been listed 11956 07:50:19,520 --> 07:50:26,160 here so let's quickly see how we can 11957 07:50:23,520 --> 07:50:27,600 put that particular output into reality 11958 07:50:26,160 --> 07:50:28,398 how does it look 11959 07:50:27,600 --> 07:50:30,958 right 11960 07:50:28,398 --> 07:50:33,520 so i'll consider 11961 07:50:30,958 --> 07:50:34,478 three different pecs 11962 07:50:33,520 --> 07:50:36,878 x 11963 07:50:34,478 --> 07:50:39,680 y and z okay 11964 07:50:36,878 --> 07:50:43,680 they say we have three different discs 11965 07:50:39,680 --> 07:50:45,680 d1 will be first d2 then d3 so this is 11966 07:50:43,680 --> 07:50:47,840 small this is 11967 07:50:45,680 --> 07:50:51,360 larger than that this is the 11968 07:50:47,840 --> 07:50:54,080 largest of all the three so this is the 11969 07:50:51,360 --> 07:50:57,360 current condition so checking for the 11970 07:50:54,080 --> 07:51:00,638 first move what does the first move say 11971 07:50:57,360 --> 07:51:04,398 disk one should be moved from peg x to 11972 07:51:00,638 --> 07:51:07,280 the peg z so peg x is nothing but source 11973 07:51:04,398 --> 07:51:08,558 this is auxiliary this is target or 11974 07:51:07,280 --> 07:51:11,520 destination 11975 07:51:08,558 --> 07:51:12,958 so i am starting with the first move 11976 07:51:11,520 --> 07:51:14,080 so here 11977 07:51:12,958 --> 07:51:15,200 it says 11978 07:51:14,080 --> 07:51:17,600 though 11979 07:51:15,200 --> 07:51:19,040 this should be moved to 11980 07:51:17,600 --> 07:51:22,478 x to z 11981 07:51:19,040 --> 07:51:26,320 which one the d1 okay 11982 07:51:22,478 --> 07:51:29,920 d3 right this will be moved to z so d1 11983 07:51:26,320 --> 07:51:32,080 will be here it will be removed right 11984 07:51:29,920 --> 07:51:33,120 so the next one 11985 07:51:32,080 --> 07:51:34,320 it says 11986 07:51:33,120 --> 07:51:37,520 move 11987 07:51:34,320 --> 07:51:41,200 disk 2 to x to y 11988 07:51:37,520 --> 07:51:44,638 so it is in x this is in y this is in z 11989 07:51:41,200 --> 07:51:47,280 what it says move though this 2 to y so 11990 07:51:44,638 --> 07:51:48,718 this 2 will come here disc 1 is already 11991 07:51:47,280 --> 07:51:51,520 existing there 11992 07:51:48,718 --> 07:51:53,840 and this 3 will be here so this is the 11993 07:51:51,520 --> 07:51:54,798 current situation when it is at the move 11994 07:51:53,840 --> 07:51:57,040 2 11995 07:51:54,798 --> 07:52:00,638 when it is at the move 3 how it will 11996 07:51:57,040 --> 07:52:04,398 look say for example we are creating the 11997 07:52:00,638 --> 07:52:06,000 same three packs here so x y z 11998 07:52:04,398 --> 07:52:09,920 fine it says 11999 07:52:06,000 --> 07:52:10,718 move disc one from z to y 12000 07:52:09,920 --> 07:52:13,600 so 12001 07:52:10,718 --> 07:52:16,000 while you do that uh disc two can have 12002 07:52:13,600 --> 07:52:20,638 disc one on top but it cannot be vice 12003 07:52:16,000 --> 07:52:21,760 versa we cannot place in this format 12004 07:52:20,638 --> 07:52:25,680 right 12005 07:52:21,760 --> 07:52:28,958 this is breaking rules you can do this 12006 07:52:25,680 --> 07:52:30,558 d2 is larger than d1 so it can take d1 12007 07:52:28,958 --> 07:52:31,600 on the top 12008 07:52:30,558 --> 07:52:33,600 so 12009 07:52:31,600 --> 07:52:37,760 xyz is here 12010 07:52:33,600 --> 07:52:40,718 so here we are i'm just putting this d1 12011 07:52:37,760 --> 07:52:44,558 on the d2 which is already existing and 12012 07:52:40,718 --> 07:52:48,000 d3 is as it is so step 3 let us move 3 12013 07:52:44,558 --> 07:52:49,840 is done if we go to move 4 what is says 12014 07:52:48,000 --> 07:52:53,920 right it will say 12015 07:52:49,840 --> 07:52:58,558 move disk 3 from x to z again so z is 12016 07:52:53,920 --> 07:53:02,638 empty x y z so currently 12017 07:52:58,558 --> 07:53:05,040 we have d1 d2 here it says move d3 which 12018 07:53:02,638 --> 07:53:07,200 is there in x to z 12019 07:53:05,040 --> 07:53:10,478 so d3 will be here 12020 07:53:07,200 --> 07:53:12,718 coming to the fifth move what it says 12021 07:53:10,478 --> 07:53:14,638 disk 1 from 12022 07:53:12,718 --> 07:53:16,558 y to x 12023 07:53:14,638 --> 07:53:18,160 so 12024 07:53:16,558 --> 07:53:19,760 according to the 12025 07:53:18,160 --> 07:53:24,320 situation 12026 07:53:19,760 --> 07:53:25,440 now we have to make this d1 move to x 12027 07:53:24,320 --> 07:53:28,320 d2 12028 07:53:25,440 --> 07:53:32,798 d3 if you could see here it is getting 12029 07:53:28,320 --> 07:53:35,280 settled obviously the next move will be 12030 07:53:32,798 --> 07:53:38,478 without thinking logically we can say 12031 07:53:35,280 --> 07:53:42,878 without going to this part 12032 07:53:38,478 --> 07:53:46,240 d3 is here d2 is here then d2 will be on 12033 07:53:42,878 --> 07:53:48,798 this so d2 will be present here d1 12034 07:53:46,240 --> 07:53:50,080 remains as it is in the seventh move 12035 07:53:48,798 --> 07:53:53,600 what happens 12036 07:53:50,080 --> 07:53:56,958 all the pegs that is y is also empty x 12037 07:53:53,600 --> 07:54:00,160 is having d1 so this d1 will be shifted 12038 07:53:56,958 --> 07:54:02,638 on to this particular stack right 12039 07:54:00,160 --> 07:54:03,440 d1 so it is there 12040 07:54:02,638 --> 07:54:06,798 now 12041 07:54:03,440 --> 07:54:09,360 all the source which was there d1 d2 d3 12042 07:54:06,798 --> 07:54:11,760 at the initial position is shifted to 12043 07:54:09,360 --> 07:54:15,040 the destination again if you could see 12044 07:54:11,760 --> 07:54:18,398 here d1 d2 d3 with the help of seven 12045 07:54:15,040 --> 07:54:19,280 moves so this is how average hanoi will 12046 07:54:18,398 --> 07:54:21,280 work 12047 07:54:19,280 --> 07:54:24,240 we have reached the last part of this 12048 07:54:21,280 --> 07:54:26,240 particular video that is summary let's 12049 07:54:24,240 --> 07:54:28,080 have a quick recap on the concepts which 12050 07:54:26,240 --> 07:54:31,040 we learnt in data structures and 12051 07:54:28,080 --> 07:54:33,600 algorithms in python so we started with 12052 07:54:31,040 --> 07:54:35,840 data structures introduction then we 12053 07:54:33,600 --> 07:54:38,320 learned different data structures that 12054 07:54:35,840 --> 07:54:40,638 is array how to create an array what is 12055 07:54:38,320 --> 07:54:44,398 an array how to implement in python 12056 07:54:40,638 --> 07:54:47,280 programming then followed by queue stack 12057 07:54:44,398 --> 07:54:49,600 linked list and concepts methods and 12058 07:54:47,280 --> 07:54:51,840 implementation as well 12059 07:54:49,600 --> 07:54:54,718 after that we learned regarding tree 12060 07:54:51,840 --> 07:54:57,920 data structure so tree data structure 12061 07:54:54,718 --> 07:55:00,320 had binary tree binary search tree 12062 07:54:57,920 --> 07:55:03,360 and we also learned how to create 12063 07:55:00,320 --> 07:55:04,638 traverse and then implement the same in 12064 07:55:03,360 --> 07:55:08,558 python 12065 07:55:04,638 --> 07:55:10,398 then we had graph data structure so we 12066 07:55:08,558 --> 07:55:13,520 learned introduction towards the graph 12067 07:55:10,398 --> 07:55:17,120 what is graph and we also implemented 12068 07:55:13,520 --> 07:55:19,040 using bfs and dfs algorithm so traversal 12069 07:55:17,120 --> 07:55:20,958 was made between two different 12070 07:55:19,040 --> 07:55:23,040 approaches breadth first search and 12071 07:55:20,958 --> 07:55:24,558 depth first search after that we learned 12072 07:55:23,040 --> 07:55:27,440 regarding hashing 12073 07:55:24,558 --> 07:55:29,200 so post that we started algorithmic 12074 07:55:27,440 --> 07:55:32,240 approaches so 12075 07:55:29,200 --> 07:55:34,160 first we started with algorithm analysis 12076 07:55:32,240 --> 07:55:36,558 how to find the time and space 12077 07:55:34,160 --> 07:55:38,718 complexity for an algorithm 12078 07:55:36,558 --> 07:55:41,920 then we started learning searching and 12079 07:55:38,718 --> 07:55:45,040 sorting algorithms so linear search 12080 07:55:41,920 --> 07:55:47,040 binary search quick sort incision sort 12081 07:55:45,040 --> 07:55:48,798 everything were taught 12082 07:55:47,040 --> 07:55:52,080 with the implementation in python 12083 07:55:48,798 --> 07:55:54,320 programming language then at the last we 12084 07:55:52,080 --> 07:55:56,478 learned regarding different programming 12085 07:55:54,320 --> 07:55:58,638 approaches we could follow in order to 12086 07:55:56,478 --> 07:56:01,840 solve the problem so we started with 12087 07:55:58,638 --> 07:56:05,280 divide and conquer followed by greedy 12088 07:56:01,840 --> 07:56:07,200 then dynamic approach right so all these 12089 07:56:05,280 --> 07:56:10,240 approaches were taught with examples 12090 07:56:07,200 --> 07:56:13,680 like merge sort prim's minimum spanning 12091 07:56:10,240 --> 07:56:16,160 tree and then we were taught with the 12092 07:56:13,680 --> 07:56:18,878 beautiful example of tower of hanoi we 12093 07:56:16,160 --> 07:56:21,200 implemented the same in python as well 12094 07:56:18,878 --> 07:56:22,398 as we analyzed the output for the same 12095 07:56:21,200 --> 07:56:24,080 so 12096 07:56:22,398 --> 07:56:26,638 all these concepts were covered in this 12097 07:56:24,080 --> 07:56:27,600 particular video hope this was helpful 12098 07:56:26,638 --> 07:56:29,840 for you 12099 07:56:27,600 --> 07:56:31,680 will meet up in the next video until 12100 07:56:29,840 --> 07:56:33,280 then thank you 12101 07:56:31,680 --> 07:56:35,360 if you haven't subscribed for our 12102 07:56:33,280 --> 07:56:37,280 channel yet i would request you to hit 12103 07:56:35,360 --> 07:56:40,160 the subscribe button and turn on 12104 07:56:37,280 --> 07:56:42,878 notification bell so that you don't miss 12105 07:56:40,160 --> 07:56:45,200 any new updates or video releases from 12106 07:56:42,878 --> 07:56:48,080 great learning if you enjoy this video 12107 07:56:45,200 --> 07:56:50,320 show us some love and do like this video 12108 07:56:48,080 --> 07:56:52,320 knowledge increases by sharing so make 12109 07:56:50,320 --> 07:56:54,638 sure you share this video with your 12110 07:56:52,320 --> 07:56:57,360 friends and colleagues make sure you 12111 07:56:54,638 --> 07:57:00,080 comment on this video any queries or 12112 07:56:57,360 --> 07:57:04,120 suggestions i'll be more than happy to 12113 07:57:00,080 --> 07:57:04,120 respond to all of them884606

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