YouTube2Text

Merge Sort | Algorithm | Pseudocode | Dry Run | Code | Strivers A2Z DSA Course — Transcript

by take U forward · 8,560 words · 1,087 segments · language en · Watch on YouTube

Full transcript

  1. 0:03hey everyone welcome back to the channel
  2. 0:04I hope you guys are doing extremely well
  3. 0:06so this is another lecture from the
  4. 0:07Strivers a toz course in case you're for
  5. 0:10the first time here this is India's most
  6. 0:12in-depth DS algo course why do I say
  7. 0:14that because this course has 456 modules
  8. 0:17and we will be solving more than 400
  9. 0:19plus problems in this particular DS algo
  10. 0:22course you can buy any of the paid
  11. 0:23courses on the market you can check out
  12. 0:26all the free courses on YouTube you will
  13. 0:28not find such a comprehensive course on
  14. 0:30DS algo throughout the market so in case
  15. 0:34you haven't checked it out there's a
  16. 0:35link in the description you can check
  17. 0:36out the entire course so in the previous
  18. 0:39video we have covered uh the step 2.1
  19. 0:41module in this uh video we will be
  20. 0:43covering the algorithm merge sort but
  21. 0:46there is a slight prerequisite to this
  22. 0:48algorithm you should have covered the
  23. 0:50step 1.5 module just in case you haven't
  24. 0:53please go and check it out and guys we
  25. 0:56are attaching the notes to all the
  26. 0:59problems and the video solution will be
  27. 1:01coming over here like if you see this
  28. 1:02one the video Solutions are attached and
  29. 1:04we'll soon be attaching all the notes so
  30. 1:07please make sure you follow this
  31. 1:08particular sheet the link will be in the
  32. 1:11description so without waiting let's get
  33. 1:12started with the problem merge short so
  34. 1:15what is merge sort it is nothing but one
  35. 1:18of the other sorting algorithms you must
  36. 1:21be thinking but striver till now you
  37. 1:23have taught us bubble short insertion
  38. 1:26sort selection sort so why this another
  39. 1:29algor Alm if you remember all of these
  40. 1:31three algorithms we're taking B go of n
  41. 1:33squ in order to sort an array this is
  42. 1:37where M short is better mot takes a much
  43. 1:40much better time complexity we will be
  44. 1:42discussing the time complexity at the
  45. 1:44end of this video but as of now you need
  46. 1:46to understand that mot is a much more
  47. 1:49optimized sorting algorithm now you know
  48. 1:52sorting algorithm is nothing but it will
  49. 1:54sort a given data structure if it has
  50. 1:56numbers it will sort it according to
  51. 1:57Numbers if it has characters it will
  52. 1:59sort it according to characters if it
  53. 2:01has strings it will sort it according to
  54. 2:03the strings so as of now we know that
  55. 2:05mot is a sorting algorithm so what will
  56. 2:08be the flow of the lecture the flow of
  57. 2:09the lecture will be first I'll explain
  58. 2:12the algorithm completely then I'll go to
  59. 2:15the pseudo code and since this is a
  60. 2:19recursive algorithm I will go to dry run
  61. 2:22the recursion so that it gets into your
  62. 2:24head so after this three steps you will
  63. 2:27be able to code this particular
  64. 2:29algorithm in any of the languages
  65. 2:31because I will be writing the pseudo
  66. 2:32code post this I'll be taking one of the
  67. 2:35languages and I'll be writing the main
  68. 2:37code again this is something which is
  69. 2:39not that important I will be focusing
  70. 2:42more on the first three steps so without
  71. 2:44waiting let's get started with the first
  72. 2:46step that is the explanation of the
  73. 2:47algorithm so in order to explain the
  74. 2:49algorithm I will take this particular
  75. 2:50example of an array which has a size of
  76. 2:54nine I need to remember two words in M
  77. 2:57short please remember that the algorithm
  78. 2:59means means divide and merge please
  79. 3:04remember this divide and merge so as as
  80. 3:09you can see that this array consists of
  81. 3:11nine elements if I have to ask you a
  82. 3:14question how do you divide nine elements
  83. 3:16into two parts you'll be like okay there
  84. 3:19are two ways in which I can do it one is
  85. 3:21I'll say four numbers and five numbers
  86. 3:25that's one of the ways the other way you
  87. 3:28can say is five numbers numers and four
  88. 3:31numbers that's two ways in which you can
  89. 3:34divide nine why because nine is a odd
  90. 3:38number so that cannot be splitted into
  91. 3:41two equal parts that's why either you
  92. 3:43have to go 4X 5 or 5 by4 now you can
  93. 3:47follow any of the path I'll follow the
  94. 3:495x4 path so if I follow the 5x4 path
  95. 3:52what happens is this algorithm divides
  96. 3:54the array hypothetically very important
  97. 3:57word this algorithm divides the array
  98. 3:59hypothetically into two parts and says 3
  99. 4:021 2 4
  100. 4:041 and 5 2 6 4 what do you mean by
  101. 4:08hypothetically when I will write the
  102. 4:10pseudo code I'll explain you okay so as
  103. 4:12of now this algorithm has divided the
  104. 4:14array into two parts one which has five
  105. 4:17elements and the other which has four
  106. 4:19elements I can say left part and the
  107. 4:21right part now what I will do is I'll
  108. 4:23always take the left part and I'll try
  109. 4:25to divide it again so let's try to
  110. 4:27divide it again if I try to divide div
  111. 4:29it again what happens is it has five
  112. 4:32numbers remember this it has five
  113. 4:34numbers so you can divide it like 2x 3
  114. 4:37or 3x2 now since since in the first step
  115. 4:41I divided N9 into five and four bigger
  116. 4:45smaller I'll follow the same pattern
  117. 4:47I'll follow the same pattern so that I
  118. 4:49can easily write a repetitive code now
  119. 4:52what I'll do is I'll try to divide it
  120. 4:53something like 312 and
  121. 4:5641 remember one thing you can also div
  122. 4:59divide it as smaller greater that is
  123. 5:02completely okay how you divide it
  124. 5:04doesn't matter as long as you divide it
  125. 5:06okay but make sure you follow the same
  126. 5:09pattern like over here it was like over
  127. 5:12here if you see it was 54 it is greater
  128. 5:14smaller so follow the same pattern of
  129. 5:16division in the future as well for odd
  130. 5:19numbers okay so next 312 go ahead and
  131. 5:22divide it I follow the same pattern and
  132. 5:24let's say 31 and two so it's like 2/ one
  133. 5:29again I'll try to divide it and this
  134. 5:32time it has two numbers it's even so it
  135. 5:36gets divided into two equal parts so
  136. 5:39I'll divide it something like three and
  137. 5:42one now tell me one thing can this be
  138. 5:45divide it further the answer to that is
  139. 5:48no we cannot because it's a single
  140. 5:49number and you cannot divide a single
  141. 5:52number so this is where you stop you say
  142. 5:55the moment I get a single number I stop
  143. 5:59and you perform the second half of the
  144. 6:01algorithm that states please merge them
  145. 6:04so what do you mean by merge it is three
  146. 6:07I'll write
  147. 6:08three it's one so they are basically two
  148. 6:11hypothetical array consisting of one
  149. 6:14numbers so if there are two hypothetical
  150. 6:17arrays of one numbers and I want to make
  151. 6:19it a single array what will I do I'll
  152. 6:22say okay let's merge it and you try to
  153. 6:25merge it into a single array consisting
  154. 6:28of the two numbers but in a sorted way
  155. 6:32so if I have to say okay I have three I
  156. 6:34have one and if I have to fill the first
  157. 6:36position I have to make it sorted whom
  158. 6:39will I take will I take one will I take
  159. 6:41three the answer to that is I will take
  160. 6:44one so I take one next who is remaining
  161. 6:47three I've merged it I'll explain merge
  162. 6:50in detail as the array gets bigger but
  163. 6:52as of now what you can say is okay we
  164. 6:55come back we come back whenever it's a
  165. 6:57single element in both sides we come
  166. 6:59back we take them and we merge and on
  167. 7:02merging this will become 1 three this
  168. 7:05will become 1 three perfect now this 312
  169. 7:09was divided into this portion and was
  170. 7:12divided into this portion can I say the
  171. 7:16left portion got itself sorted and now
  172. 7:19the right portion has a single element
  173. 7:22can a single element be further divided
  174. 7:25the answer to that is no so your next
  175. 7:27step will be very simple you will take
  176. 7:301A 3 and two so can I say these are two
  177. 7:34hypothetical arrays one is 1A 3 on the
  178. 7:37left one is two on the right and you'll
  179. 7:39try to merge them into a single because
  180. 7:42there are two hypothetical arrays and if
  181. 7:44you carefully observe the left array in
  182. 7:47itself is sorted and the right array in
  183. 7:51itself is sorted so now I will go across
  184. 7:54and hypothetically try to merge them if
  185. 7:56I try to merge them they will give me
  186. 7:59three elements in the sorted order now
  187. 8:01tell me one thing do you take one or do
  188. 8:04you take two in order to fill this first
  189. 8:07position you'll be like hey dri I will
  190. 8:09take one so you took one and you did not
  191. 8:13take two so the moment you've taken one
  192. 8:16the next you will say do I take three do
  193. 8:18I take two like I'll take two so you
  194. 8:21took two next you just left with one so
  195. 8:24you will take three again I'll explain
  196. 8:26this algorithm in depth do not worry so
  197. 8:29we
  198. 8:30basically merge them again merge them so
  199. 8:33we get 1 2 3 the merging has
  200. 8:38completed so can I say initially when I
  201. 8:41went to the left we had 1 2 3 and when
  202. 8:44we went to the right we had 41 so the
  203. 8:47left portion has divided and merged
  204. 8:51itself into a single sorted array it has
  205. 8:55it's time to go to the right let's go to
  206. 8:57the right so the moment you look at the
  207. 8:59right it has 41 so we again try to uh
  208. 9:04divide it so if you try to divide it it
  209. 9:06will be four and one can I say we can
  210. 9:10stop over here because they are single
  211. 9:12elements and they cannot be further
  212. 9:14divided yes we cannot further divide
  213. 9:17them so what we do is we
  214. 9:19stop now if it is four if it is one and
  215. 9:23if you have to merge them into a single
  216. 9:27sorted array first one will come then
  217. 9:29four will come so what will happen is
  218. 9:31they will try to merge them and you will
  219. 9:33get one comma four so can I say can I
  220. 9:37say that this one went got it sorted
  221. 9:41this one went and got it sorted so
  222. 9:43basically now they both will come back
  223. 9:46because the left half is sorted and the
  224. 9:48right half is sorted if both of them are
  225. 9:51completely sorted what will happen so
  226. 9:54can I say it went left and it got itself
  227. 9:57sorted it went right it got itself
  228. 10:00sorted it's time to come back so they
  229. 10:04will get merged very simple let's write
  230. 10:07it again 1 2 3 and 1 4 and they will try
  231. 10:12to merge themsel into how many elements
  232. 10:15definitely yes five elements so let's
  233. 10:19kind of now try to get into the
  234. 10:21algorithm now you know this is the
  235. 10:22smallest element on the left half if I
  236. 10:25say there are two hypothetical arrays
  237. 10:27can I say on the left the small smallest
  238. 10:29element is definitely on the beginning
  239. 10:31because both the arrays are sorted so we
  240. 10:34will take one we will take one we have
  241. 10:37one over here we have one over here but
  242. 10:40I have to ask you which one will occupy
  243. 10:43this it can be any of the ones I take
  244. 10:45any of the ones and maybe I'll just move
  245. 10:48it now if I have to ask you out of two
  246. 10:51out of one which will occupy this you
  247. 10:55know the answer it's one so you now move
  248. 10:57the pointer next next if I ask you out
  249. 11:00of two out of four which one will occupy
  250. 11:03this you will be saying two so I'll move
  251. 11:06the pointer next if I ask you out of
  252. 11:08three out of four which one does occupy
  253. 11:12this particular guy it like three and
  254. 11:14this will move out next if I ask which
  255. 11:16one will occupy this you'll be like four
  256. 11:18and this moves out so apparently you got
  257. 11:21this entire thing sorted so can I say
  258. 11:24this will become 1 1 2 3 4 done
  259. 11:31so this half is sorted so we started
  260. 11:35with nine numbers we splitted them into
  261. 11:39five on this side four on this side five
  262. 11:43guys are have been sorted by the
  263. 11:45algorithm divide divide divide and merge
  264. 11:48merge merge they got themselves sorted
  265. 11:50now it's about these four elements can I
  266. 11:53perform the same algorithm of divide and
  267. 11:55merge on these four I can so what I'll
  268. 11:59do is I'll again start because now it's
  269. 12:01the right half turn to get sorted so
  270. 12:04I'll not again divide the four elements
  271. 12:06and if there are even elements how do
  272. 12:09they get divided into equal number of
  273. 12:10elements so it will become 5 comma 2 and
  274. 12:146 comma 4 5 comma 2 will become 5 2 so
  275. 12:20again can I say they will be sorted back
  276. 12:23into
  277. 12:2525 and now this one will take place so
  278. 12:29be like 6 4 again it will come back I'll
  279. 12:34say 4 6 perfect now can I say this
  280. 12:37portion is sorted this portion is sorted
  281. 12:40so now they will come back and how will
  282. 12:42they come back let's write it out 25 4 6
  283. 12:46so both of them will get merged into how
  284. 12:49many elements four elements two or four
  285. 12:52which one goes at the first two four or
  286. 12:56five four or five four
  287. 13:01sorry uh now six or five yes 5 next six
  288. 13:09it's like 2 456 very simple so this
  289. 13:12becomes 2 456 we can go ahead and erase
  290. 13:15this as well got a bit clumsier but I'm
  291. 13:18I'm assuming you're getting the
  292. 13:19explanation now where are we can I say
  293. 13:23we went to the left we got everything
  294. 13:25sorted we went to the right we got
  295. 13:27everything sorted the left left the left
  296. 13:30five elements are sorted the right four
  297. 13:32elements are sorted what is that we need
  298. 13:35to do need to merge them you need to
  299. 13:37perform the last step that is merge so
  300. 13:39these 1 1 2 3 4 1 1 2 3 4 and on the
  301. 13:45right we have 2 4 5 6 we have 2 4 5 six
  302. 13:49they will get merged into nine elements
  303. 13:52and that will be my ultimate sorted
  304. 13:55array how do we merge let's Now
  305. 13:58understand the algorithm in depth so
  306. 14:00what we do is we keep couple of pointers
  307. 14:03one here one here okay and then we
  308. 14:06create an array of size let's say nine 1
  309. 14:102 3 4 5 6 we created an array of size 9
  310. 14:15or you can create an Mt and keep on
  311. 14:17piling it up that's your choice we will
  312. 14:20take care of that during the
  313. 14:21implementation so we know this entire
  314. 14:25left array is sorted we know the entire
  315. 14:27right array is sorted so so we have to
  316. 14:30combine them to get a sorted array so
  317. 14:33out of one and out of two because both
  318. 14:35of them are smallest I know I've taken
  319. 14:38the two smallest guys from left from
  320. 14:40right which one will take this place you
  321. 14:43say one so you take one and you place it
  322. 14:46and the moment you've taken someone
  323. 14:48you'll just move the pointer let's start
  324. 14:50start moving the pointer got it and you
  325. 14:52take one or you take two again you take
  326. 14:54one and you move the pointer you take
  327. 14:56two or you take two you can take two and
  328. 14:58you you can move the pointer you'll take
  329. 15:00three or you'll take two you'll take two
  330. 15:02and you'll move the pointer now will you
  331. 15:04take four will you take three you'll
  332. 15:06take three and you'll move the pointer
  333. 15:07now will you take four or will you take
  334. 15:09four you can take four and you can move
  335. 15:11the pointer so we can say the left array
  336. 15:15has been exhausted please hear me
  337. 15:18properly I can say that the left array
  338. 15:21is completed it doesn't have any more
  339. 15:24elements that can go over here so what I
  340. 15:26will do is without doing any further
  341. 15:29comparisons I'll pick up everyone from
  342. 15:31the right and I'll just put it over here
  343. 15:34I'll pick up everyone from the right and
  344. 15:37I'll put it over here cuz there is no
  345. 15:39one left on the left got it if something
  346. 15:43similar happened that the right element
  347. 15:45got completed then you would have taken
  348. 15:47this and you have put it it doesn't
  349. 15:49matter so it's very simple if I have to
  350. 15:53define the merge algorithm it's very
  351. 15:54simple you take two pointers compare put
  352. 15:57put put one gets comp completed the
  353. 15:59other one gets put we will implement
  354. 16:02this do not worry
  355. 16:04so we have done the Sorting so the last
  356. 16:07step what will happen they come back
  357. 16:09they come back and this will be 1 1 2 2
  358. 16:133 4 1 1 2 2
  359. 16:173 4 4 5 6 4 4 5 6 so can I see after I
  360. 16:24have completed all the steps of divide
  361. 16:27and merge if you carefully notice the
  362. 16:29last step was merge in the entire
  363. 16:32algorithm so once I performed both of
  364. 16:34them completely on all the parts of the
  365. 16:37array the array that I get will always
  366. 16:41be sorted this was the algorithm I hope
  367. 16:44you have understood the expl okay so I
  368. 16:45hope you have understood the explanation
  369. 16:48it's time to understand the pseudo code
  370. 16:50first thing that we have to observe is
  371. 16:53what are we doing we are dividing we
  372. 16:56dividing the array into two halves but
  373. 16:59logically speaking if you're going to
  374. 17:01break the uh break the array down into
  375. 17:03two new arrays and then again this array
  376. 17:06into two new arrays again this array
  377. 17:08into two new arrays will it makes sense
  378. 17:11no this is where in the pseudo code we
  379. 17:15will try to play around with index
  380. 17:19instead of breaking them down into two
  381. 17:22new arrays we will try to play around
  382. 17:24with indexes okay so in order to write
  383. 17:27the pseudo code what I've done is is
  384. 17:29I've taken a very small array of size
  385. 17:31five now you know the array indexing
  386. 17:33starts from zero and it will go till
  387. 17:35four if the size is five so what I will
  388. 17:39say is see this index will be referred
  389. 17:43as my low this index will be referred as
  390. 17:48high so what does low and high means is
  391. 17:50a starting point of an hypothetical
  392. 17:52array and high is the ending point of an
  393. 17:55hypothetical AR so initially we have an
  394. 17:58entire aray that has to be sorted so low
  395. 18:01is the starting point of that entire
  396. 18:03array and high is the ending point of
  397. 18:06that hypothetical array got it okay so
  398. 18:08let's try writing the pseudo code for
  399. 18:09this merge short algorithm so I'll go
  400. 18:12ahead and write the function name as
  401. 18:14merge sort and
  402. 18:17initially I'll pass the array and I will
  403. 18:20say in this array initially please
  404. 18:24consider the entire array that you have
  405. 18:26to sort so the hypothetical starting
  406. 18:29point of this array is low and this is
  407. 18:31high which means take the entire array
  408. 18:34so I'll pass him low and I'll pass him
  409. 18:37high and initially when this is called
  410. 18:40Low will be passed as zero high will be
  411. 18:43passed as four very simple let's go on
  412. 18:47and as of now I'll not write any
  413. 18:49condition I'll keep conditions apart so
  414. 18:53how do I write this algorithm let's try
  415. 18:56to observe something what are we doing
  416. 18:59repeatedly are we doing something
  417. 19:00repeatedly yeah divide divide divide
  418. 19:04divide kind of doing divide divide
  419. 19:06divide divide and at the last step we're
  420. 19:09trying to come back come back does it
  421. 19:11give you an idea that you have to apply
  422. 19:13something like a recursion like you have
  423. 19:15to go divide divide divide and then you
  424. 19:18have to come back that's kind of like
  425. 19:19recursion backtracking if you have seen
  426. 19:21the lectures you can try to relate it
  427. 19:23with recursion and backtracking that's
  428. 19:25what we will be using so what I will say
  429. 19:27is okay now now what is the thing that
  430. 19:29I'll do I'll try to take this array and
  431. 19:32try to break it down into two parts that
  432. 19:34is what I'm doing I'll try to take this
  433. 19:36aray and I'll try to break it down into
  434. 19:38two parts where I will take the first
  435. 19:42one to be 3 to 4 that's how I was doing
  436. 19:45the greater like the three numbers at
  437. 19:47the first and then the two numbers at
  438. 19:48the back so I'll say I'll divide it into
  439. 19:50two parts the left and the right that's
  440. 19:52what I'm doing and then I'll ask can you
  441. 19:55please sort this can you please sort
  442. 19:57this once both of them are sorted I can
  443. 20:00actually again merge them that's what
  444. 20:03the algorithm was so I'll be like okay
  445. 20:06do I need to make a new array no this is
  446. 20:09where low and high comes in I say Hey
  447. 20:12listen this is array
  448. 20:14indexing so can I say this array is
  449. 20:17nothing but an array where the low is at
  450. 20:20zero an array where the high is at two
  451. 20:22can I see this I can so if I have to
  452. 20:26look properly the low is at zero
  453. 20:29the high is at two that's what I'm doing
  454. 20:31so I'll go and say that Hey listen maybe
  455. 20:34find a middle index so middle is going
  456. 20:37to be low plus High by two very
  457. 20:40simple so if you see over here what will
  458. 20:43be this 0 + 4 by 2 will give you 2 so
  459. 20:48where is the first half of the array
  460. 20:51it's from 0 till 2 and where is the
  461. 20:54second half it's from 3 to 4 so can I
  462. 20:57say hey please go ahead
  463. 21:01and try to sort this array sort this
  464. 21:06array which starts from zero which is
  465. 21:09apparently low and which goes on till
  466. 21:11two which is apparently middle because
  467. 21:14in the first time the middle is two and
  468. 21:16can I say okay this means I'm I'm asking
  469. 21:20him to do for this one right but I have
  470. 21:24to also ask him to do for this one
  471. 21:25because if he sorts this
  472. 21:28if it doesn't sorts this how will I
  473. 21:30merge them because both of them have to
  474. 21:32be sorted in order to be merged so what
  475. 21:34I will do is I will again say mer
  476. 21:38short I'll say array can I get the right
  477. 21:42half of the array sof I'll be like okay
  478. 21:44in this array which portion are you
  479. 21:47telling me to sort I'm saying from 3 to
  480. 21:494 what is three that's nothing but mid +
  481. 21:521 because mid was two till high so what
  482. 21:55I've done is instead of making two new
  483. 22:00arrays I told the indexes I told in this
  484. 22:05array take from low to mid which is take
  485. 22:07from 0 to two which is this in this
  486. 22:09array take from 3 to four which is this
  487. 22:12instead of dividing them into two arrays
  488. 22:15I passed on the indexes and set them to
  489. 22:17set them to sort themselves now assuming
  490. 22:20that these functions are getting sorted
  491. 22:23by themselves once the portion from low
  492. 22:27to mid once once the portion from low to
  493. 22:30mid is sorted once the portion from mid
  494. 22:32one to high is sorted what is the next
  495. 22:35step if you remember the next step is
  496. 22:37merge so probably I can say that hey
  497. 22:41please go ahead and merge the array the
  498. 22:46first half of the array is very simple
  499. 22:48it goes from low to mid and the second
  500. 22:51half will be from mid plus one to high
  501. 22:53so take these things and can you please
  502. 22:56merge this particular array that's what
  503. 22:59I'm saying that's what I'm saying very
  504. 23:00simple now when do we stop when do we
  505. 23:03stop is my question we have to write
  506. 23:05that over here because if you know if
  507. 23:08this is nothing but a recursion mot
  508. 23:11calling itself mot calling itself it's a
  509. 23:13recursion in order to stop recursion you
  510. 23:17write something as base case I hope you
  511. 23:20know that in order to stop recursion you
  512. 23:22write something as base case so you have
  513. 23:24to write that base case let's analyze
  514. 23:26what is the base case let's go back to
  515. 23:28this example what is the Beast case can
  516. 23:31I say when your array has one element
  517. 23:35when your array has one element so if I
  518. 23:37have to index it can I say this is index
  519. 23:40zero this is index one this is index 3 2
  520. 23:433 4 5 6 7 8 can I say these are the last
  521. 23:49indexes and if I have to ask what array
  522. 23:52is this you'll be like this array
  523. 23:55is something like from index one to
  524. 23:59index one where index one is the low
  525. 24:02index one is the high can I say this is
  526. 24:04the array that represents this yes
  527. 24:07because we are not creating a new array
  528. 24:09I am saying this array is from index one
  529. 24:12to index one which is this right so can
  530. 24:15I say the moment we have the low the
  531. 24:18left point we have the high the right
  532. 24:20point to be same that's when we do not
  533. 24:23go and divide we do not divide we just
  534. 24:26go back so that's what the base case
  535. 24:28will be we write he if at any moment the
  536. 24:34low tends to be equal to equal to high
  537. 24:38or exceeds high for that reason or maybe
  538. 24:41it's some how like it will never but
  539. 24:44still this for safety reasons if it
  540. 24:46exceeds High please do not because
  541. 24:48there's no need to do any of the further
  542. 24:50algorithms it's a single element array
  543. 24:53and it will always be sorted please go
  544. 24:55back please go back so this is how the
  545. 24:57pseudo code will look like might seem a
  546. 25:00bit confusing but now when I'll dry run
  547. 25:02by taking an example it straight away
  548. 25:04get to your head okay so we can see that
  549. 25:06the pseudo code has been completed but
  550. 25:09in order to understand this algorithm
  551. 25:11with Clarity we have to do a dryon of
  552. 25:13this recursive algorithm then only it
  553. 25:15will get clarified so let's try to do a
  554. 25:19dryon by taking this particular example
  555. 25:23so remember one thing whenever this
  556. 25:25algorithm is called it is called with
  557. 25:27the complete AR AR so the entire array
  558. 25:29is always been passed with this
  559. 25:31algorithm and the initial low and high
  560. 25:33will be 0 and four because that's what
  561. 25:36for this particular example it will be
  562. 25:37because initially I will pass on this
  563. 25:40entire array and ask mot to sort it so
  564. 25:43what will happen will this first base
  565. 25:45case operate because no low is still
  566. 25:48smaller than high so it doesn't mid
  567. 25:51comes out what will be the value of mid
  568. 25:54can I say the value of mid will be two
  569. 25:56so what happens is it goes goes ahead
  570. 25:58and calls the M shot it goes ahead and
  571. 26:01calls the mer shot so let's copy paste
  572. 26:04the same algorithm let's do
  573. 26:07it so I've copy
  574. 26:09pasted so what we can say is this
  575. 26:12particular recursion call will call this
  576. 26:14particular function again but this time
  577. 26:17it will pass the low as zero and pass
  578. 26:19the high as two so apparently what
  579. 26:21happens is it calls for this guy
  580. 26:25remember how recursion works is first
  581. 26:27this will get oper ated comes back then
  582. 26:30this line gets operated that's how
  583. 26:32recursion works so that's why first the
  584. 26:34left portion will get operated so the
  585. 26:37recursion call for zero to goes now what
  586. 26:39happens does this line operate no this
  587. 26:41line doesn't operates what happens tries
  588. 26:44to find the mid and this time the mid
  589. 26:46comes out to be 1 because 0 + 2 is 2 and
  590. 26:512 by2 is one so one comes out so this
  591. 26:54time again this SM shot gets called but
  592. 26:57if the M shot gets called called what
  593. 26:59will happen it gets called For What from
  594. 27:020 to 1 so it's like 3 to 2 is what it is
  595. 27:05getting called and then it will be
  596. 27:06called for the only number four which is
  597. 27:09just two so we will go ahead and maybe
  598. 27:12again call this recursion let's go ahead
  599. 27:15and call the recursion again so the
  600. 27:17recursion is again called what happens
  601. 27:19this time the mot is called with 01
  602. 27:22which means this particular example
  603. 27:25right so this time the lower is 0 the
  604. 27:28high is one so what happens the mid is 0
  605. 27:31+ 1 by 2 which is nothing but Zer so
  606. 27:35again the M shot is called so let's
  607. 27:38again copy paste this same
  608. 27:41algorithm the M shot this time is called
  609. 27:44for 0 0 if I go into the diagram it's
  610. 27:47something like for three and something
  611. 27:50like for just one so this time it's
  612. 27:52called for three so what happens is it's
  613. 27:55called for 0 and zero so apparently this
  614. 27:58particular base case gets executed and
  615. 28:01it returns so I can say the it's done
  616. 28:04it's done so if I have to go back into
  617. 28:07the chart this goes back similarly
  618. 28:11another more shot will be called let's
  619. 28:15try to write this as
  620. 28:16well and this time it will be called for
  621. 28:19from mid + one to high so mid was zero
  622. 28:23so it'll be like 1 comma 1 because high
  623. 28:26was one mid was was zero so bit + 1 is 1
  624. 28:30high is 1 so high is 1 it's called for
  625. 28:331A 1 again the low the base case
  626. 28:36operates and it calls back apparently
  627. 28:40both of these recursive functions are
  628. 28:43completed once both of these recursion
  629. 28:45functions are completed can I say this
  630. 28:49that I'm going back from here and now we
  631. 28:52have this as sorted we have this as
  632. 28:53sorted it's two sorted arrays it's two
  633. 28:56sorted arrays what will happen happen
  634. 28:58let's go back here this merge happens so
  635. 29:01it takes a three it takes a two and it
  636. 29:05tries to merge it now we will write the
  637. 29:07merge algorithm afterwards but let's
  638. 29:09understand just imagine merges merging
  639. 29:12two arrays that's it that's a very
  640. 29:13simple algorithm just understand this so
  641. 29:152A 3 happens so what would have happened
  642. 29:182A 3 right so this would have become 2
  643. 29:22comma 3 perfect let's go back to the
  644. 29:25recursion so can I say now we can go
  645. 29:29back from here to here and we can say
  646. 29:31the 01 is sorted so this time this
  647. 29:36recursion will again be called let's try
  648. 29:38to write it so what will be this
  649. 29:40recursion if 01 is done sorry my bad if
  650. 29:4501 is done what's the next mid + 1
  651. 29:49that's two I is two so you'll call it
  652. 29:51for 2 comma 2 and if you call it for 2
  653. 29:54comma 2 what will happen yes they are
  654. 29:56the same numbers so it will will return
  655. 29:59and if it returns this time what will
  656. 30:01happen it called for 2 comma 2 this guy
  657. 30:06has done the recursion and this guy has
  658. 30:08done the recursion so apparently again
  659. 30:11the merge happens this time the merge
  660. 30:13happens from 0 to 1 till the two the
  661. 30:17merge this time happens for 2 comma 3
  662. 30:20and maybe four or whatever it
  663. 30:23is yes and this will become 2 comma 3A 4
  664. 30:27I hope you getting the gist so can I say
  665. 30:30after this I'll again go back and when I
  666. 30:34go back now I'll try to just come back
  667. 30:37yes when I go back can I see this that 0
  668. 30:40comma 2 would have been sorted and if 0a
  669. 30:432 is sorted I'm done with this I'll go
  670. 30:46to the right part which is having this
  671. 30:48and
  672. 30:50similarly this is done so this will call
  673. 30:53it right and how will it call it let's
  674. 30:57copy paste and show it
  675. 30:58if 0a 2 has been done so can I say next
  676. 31:02is mid + 1 so this is nothing but three
  677. 31:05highest four this time it goes and calls
  678. 31:07for 3 comma 4 again the same thing will
  679. 31:10happen the mid will be like 3 + 4 7 by
  680. 31:1423 so it will be called for 3 comma 3
  681. 31:17and then it'll come back this will be
  682. 31:19called for 4 comma 4 it'll be come back
  683. 31:22and then this will merge both of these
  684. 31:25two elements it's like it'll be called
  685. 31:27for one it'll be called for three then
  686. 31:29they will be merged as 1 three and once
  687. 31:33this is done what do we have we have 0a
  688. 31:362 sorted we have 3A 4 sorted so the last
  689. 31:40merge takes place between this 0a 2 that
  690. 31:44is 2A 3A 4 and 1A 3 so apparently they
  691. 31:48are merged together when merged you'll
  692. 31:52get 1 2 3 3 4 that is what happens at
  693. 31:55the last step I hope that
  694. 31:58does make sense so so I hope the dry run
  695. 32:01is clear in terms of the pseudo code it
  696. 32:03was very simple first the entire array
  697. 32:06goes to the left goes to the left goes
  698. 32:08to the left goes to the left and then
  699. 32:10merges it always performs this recursion
  700. 32:13first this recursion first this
  701. 32:15recursion first once this is completed
  702. 32:18then this once both are completed then
  703. 32:20the merge happens need to understand
  704. 32:23that this is how the flow Works in order
  705. 32:24to understand this in much more depth
  706. 32:27what I'll recommend you is take a pen
  707. 32:29and paper write this code try doing it
  708. 32:32all over again got it so what have we
  709. 32:35completed we have completed the dryon
  710. 32:38the pseudo code as well but before going
  711. 32:40into the code we still are left with the
  712. 32:43merge pseudo code if we are able to
  713. 32:45write the merge pseudo code I think we
  714. 32:47will be done so let's take a bigger
  715. 32:50example in order to understand the merch
  716. 32:52pseudo code can I say the indexing is
  717. 32:54like 0 1 2 3 4 5 6 7 7 8 and this is
  718. 32:58like 0 1 2 3 4 and this is like 5 6 7
  719. 33:02and 8 and if the merch will happen I
  720. 33:06will call it like merge these two arrays
  721. 33:09because if you know this array when goes
  722. 33:12into depth goes into depth it will be
  723. 33:14sorted it'll be sorted like 1 1 2 3 4
  724. 33:18and this also will go into the depth and
  725. 33:20it will be sorted like 24 56 this is
  726. 33:23something which all of us know for sure
  727. 33:25that both of them will be sorted and and
  728. 33:28both of the array will be something like
  729. 33:301 1 2 3 4 and this will be like 2 4 5 6
  730. 33:34this is something we know and this will
  731. 33:37be index from 0 to 4 this will be from 5
  732. 33:40to 8 that's something we know we just
  733. 33:42have to write an algorithm to do it
  734. 33:45so we're writing the algorithm okay so
  735. 33:47we're saying
  736. 33:51merge I'll give you the array and I'm
  737. 33:53stating you that the first half is from
  738. 33:56low to mid and then you can go from mid
  739. 33:59plus one to high so I'm giving you all
  740. 34:00the three parameters
  741. 34:02required what I'll do is maybe I'll just
  742. 34:05keep an empty data structure it can be a
  743. 34:08vector it can be a list as of now just
  744. 34:11imagine this is an empty data structure
  745. 34:14where we can add elements okay now can I
  746. 34:18say that the left array will be from low
  747. 34:22to mid and can I say the right array
  748. 34:25will be from mid plus one to high I I
  749. 34:27can so I will keep couple of pointers so
  750. 34:31I'll say okay left pointer is at the low
  751. 34:36so that means left comes over here and
  752. 34:38I'll say right pointer is nothing but at
  753. 34:41Mid + one so what happened I'll say WR
  754. 34:44is here now what will I do I will try to
  755. 34:48compare these two and try to fill in
  756. 34:51this bigger array the bigger array I'll
  757. 34:54try to compare these two and try to fill
  758. 34:56in the bigger array so what I will do is
  759. 34:59I will now try to iterate till what till
  760. 35:02we have elements on the left or till we
  761. 35:04have elements on the right can I say
  762. 35:07that means I will say while left is
  763. 35:10smaller than equal to Mid which means I
  764. 35:14still have element and I'll say and and
  765. 35:18right is lesser than equal to high which
  766. 35:21means I still have elements on the right
  767. 35:25very simple now what I'll do is
  768. 35:28okay this is what I'll do till I have
  769. 35:29elements on the left till I have
  770. 35:31elements on the left on the right so
  771. 35:34I'll not try to compare I will say Hey
  772. 35:36listen if array of
  773. 35:38left is smaller than equal to array of
  774. 35:42right so what
  775. 35:44happens this and this gets compared and
  776. 35:48if one is smaller than two I'll take one
  777. 35:51and I'll add it to our resultant guy
  778. 35:54I'll add it to our resultant guy so I'll
  779. 35:56say okay maybe uh we can name this as
  780. 35:59something
  781. 36:01else let's go and name this as maybe
  782. 36:03let's say temporary that's a better name
  783. 36:06as a temporary can you please add
  784. 36:08yourself the smaller guy array of left
  785. 36:11and once you have done this can you move
  786. 36:13ahead the pointer which means the left
  787. 36:16pointer goes to the next again so
  788. 36:19basically what will happen is now
  789. 36:21temporary will have one with itself
  790. 36:24again the comparison between one and two
  791. 36:26will happen and this one will come here
  792. 36:28the temporary again takes it and now the
  793. 36:30left moves here again the comparison
  794. 36:33between two and two will happen and it
  795. 36:35comes here and the left moves here this
  796. 36:37time when you compare three and two when
  797. 36:40you compare three and two the two will
  798. 36:42come but the if only Compares for lesser
  799. 36:45so there will be a else so else will
  800. 36:47state can you please add the right guy
  801. 36:51can you please add the right guy that is
  802. 36:53take from the array and add the right
  803. 36:56guy and once you have added can you move
  804. 36:59the right Plus+ so what I'll do is I'll
  805. 37:01move the right to ahead now that's how
  806. 37:06this will work and you'll keep on
  807. 37:07comparing and you'll keep on adding till
  808. 37:10this is not completed so what will
  809. 37:12happen this two will come and the right
  810. 37:14will move here now the comparison
  811. 37:16between four and three happens so three
  812. 37:19comes and the left moves here now four
  813. 37:22comes and the left is exhausted left
  814. 37:25exceeds mid left exceeds mid so we see
  815. 37:28that the left has exceeded mid there is
  816. 37:30no one on the left so what I will say is
  817. 37:33after the end if there
  818. 37:35is anyone on the left which means if
  819. 37:38there is anyone on the left because if
  820. 37:42this while if this while loop is false
  821. 37:44either it will be false because this is
  822. 37:46true or this is true so if there are on
  823. 37:50the left if there are on the left I will
  824. 37:53still add it I just take everyone from
  825. 37:56the left and add it to my
  826. 38:01stuff and if there are people on the
  827. 38:04right because anyone will be true either
  828. 38:08there will be on the left either there
  829. 38:10will be on the right if there are people
  830. 38:12on the
  831. 38:13right which will be right high and then
  832. 38:18I'll say temporary do add array of
  833. 38:22right and I'll do right Plus+ quite
  834. 38:25simple
  835. 38:27if that means if on left copy them if on
  836. 38:32right copy them as simple as that so can
  837. 38:36I say at the end of the day I would have
  838. 38:38four 4 5 six all of them in the
  839. 38:41temporary area now we need to place them
  840. 38:44into the original array so what have we
  841. 38:46completed till this step we can say the
  842. 38:50left sorted array and the right sorted
  843. 38:52array have been put into a third array
  844. 38:55temporary but we actually need to put it
  845. 38:59from low to high because that's the
  846. 39:01original array because the original
  847. 39:03array is from 0 to 8 and you take these
  848. 39:05elements and put it there because it is
  849. 39:08in temporary so what I'll do is I will
  850. 39:11probably Loop yes let's Loop I'll say
  851. 39:13for I from low to high is where I Loop
  852. 39:17please loop from there and I'll say
  853. 39:19array of I that means who is the first
  854. 39:22element
  855. 39:24temporary temporary must be keeping
  856. 39:27because it's a temporary array it will
  857. 39:29always keep in the zero index so I'll
  858. 39:31always say
  859. 39:33temporary I minus
  860. 39:36low that's it first time I will be low
  861. 39:39so what will this happen low minus low
  862. 39:42that will be zero next time I will be
  863. 39:44low + one so low + 1 minus low that'll
  864. 39:47be one next time I will be low plus 2
  865. 39:49low plus 2 minus low that'll be two so
  866. 39:51it will get all these indexes because it
  867. 39:55will be stored in a temporary array
  868. 39:56it'll pick up all these indexes and
  869. 39:59store in the correct order so it'll pick
  870. 40:02up all these indexes at 0 1 2 and store
  871. 40:06it in the correct order from low to high
  872. 40:08because
  873. 40:10because this was low to mid this was mid
  874. 40:13plus one to high and they CAU together
  875. 40:15from low to high that was the original
  876. 40:17array got it this is how the merge will
  877. 40:21look like I hope that does make sense so
  878. 40:23now I can confidently say that You' have
  879. 40:25understood the explanation the pseudo
  880. 40:27code and the dryon so you can code it in
  881. 40:29C++ Java and python so I'll just go
  882. 40:32across and code it in C++ but the Java
  883. 40:35and python codes you can find them in
  884. 40:37the links in the description it will be
  885. 40:39updated very very soon just in case it
  886. 40:41is not there as of now let's come to the
  887. 40:42compiler and try to code it so we have
  888. 40:44given a function we given the array and
  889. 40:46we given this so assume we are going to
  890. 40:48write this function and we say void uh M
  891. 40:52short and I'll say let's take the array
  892. 40:55which is this please make sure you pass
  893. 40:57it reference low and high that's what we
  894. 41:00require and we know one thing if low is
  895. 41:02equal to equal to high we're going to
  896. 41:04return that's it otherwise we are going
  897. 41:06to find a middle which is low plus High
  898. 41:11by 2 and then we will be calling the MS
  899. 41:14with array comma low comma mid and
  900. 41:20Ms aray comma mid + one comma high and
  901. 41:24then we will be calling something like
  902. 41:26merge Where will passing array low mid
  903. 41:30and high that's what the algorithm will
  904. 41:32be and we can call Ms with the array and
  905. 41:360 comma n minus one because that's the
  906. 41:38initial starting and the end position of
  907. 41:41the entire array just have to write the
  908. 41:43merge function and I think we will be
  909. 41:45done so let's write the void merge and
  910. 41:48again make sure you take everything by
  911. 41:50reference array and then low and then
  912. 41:53you can take mid and then you can take
  913. 41:55High
  914. 41:58so can I just take a temporary array we
  915. 42:01can and we can take the left pointer to
  916. 42:03be low plus one uh sorry low rather and
  917. 42:06we can take the right pointer to be mid
  918. 42:08+ one because we know the first array
  919. 42:10will be like if I have to commented it
  920. 42:13will be from low to mid and the second
  921. 42:15array if you remember it will be mid +
  922. 42:17one till high so let's see uh let's
  923. 42:22write this so we say left is lesser than
  924. 42:24equal to Mid and and M and and
  925. 42:29write is lesser than equal to high very
  926. 42:33simple but we need a temporary array
  927. 42:36that is defined over here we say hey
  928. 42:38array of left are
  929. 42:41you smaller than array of right if you
  930. 42:45are can you please go ahead and get
  931. 42:48attached push
  932. 42:51back to array of left very simple at the
  933. 42:55same time we can do a left Plus plus and
  934. 42:58you can write an else and you can
  935. 42:59temporary do push back array of right
  936. 43:04and you can say right Plus+ this is what
  937. 43:07you will do for comparing and putting it
  938. 43:09after that if there are still elements
  939. 43:12left on the left please take it because
  940. 43:14if this is false then this must be
  941. 43:17having some elements on the left so go
  942. 43:19ahead and just copy paste this
  943. 43:23perfect and if there are still elements
  944. 43:26on the right then go ahead and copy
  945. 43:29paste everything from the right quite
  946. 43:32simple once you have done this make sure
  947. 43:34you put them into the array which is for
  948. 43:36in I equal to low I less than equal
  949. 43:40to high and I ++ and you can say array
  950. 43:44of I will be temporary of I minus low so
  951. 43:49once you've done this since everything
  952. 43:50is by reference the the numbers will
  953. 43:53rearrange themselves in the array itself
  954. 43:55now I can go ahead I can go ahead and
  955. 43:57run this and see if it is running fine I
  956. 43:59hope it does it does and now I'll
  957. 44:01quickly submit this I write codes in one
  958. 44:05go W nice no syntax errors so we have
  959. 44:09understood everything but there's only
  960. 44:12one thing that is left which is the time
  961. 44:15complexity of this merge short algorithm
  962. 44:18remember one thing what's happening
  963. 44:21there's an N size array which is getting
  964. 44:23divided to n by2 n by2 then this n by2
  965. 44:26is getting divided to n by4 n by4 n by4
  966. 44:30and n by4 so at every step they're
  967. 44:34getting divided by two by two by two by
  968. 44:37two and if you remember the time
  969. 44:39complexity lecture I have already told
  970. 44:42whenever it gets divided by by two the
  971. 44:45time complexity is log base to n
  972. 44:47remember this now you might ask me but D
  973. 44:50why you're dividing it by by 2 by 2 but
  974. 44:53why it is log base 10 it's very simple
  975. 44:55if I have to write 16
  976. 44:57and I have to divide it by two it'll be
  977. 44:59like 8 and 8 it'll be like four and four
  978. 45:04right like if there are four like if
  979. 45:06there are 16 array elements gets divided
  980. 45:08into 8 and8 eight gets divided into four
  981. 45:10and four four gets into two and two and
  982. 45:12then two gets into one and one so
  983. 45:15apparently this is nothing but one step
  984. 45:19two step three step four step and 16 is
  985. 45:23nothing but 2 to the^ 4 and that's
  986. 45:27nothing but four steps right four steps
  987. 45:30log base 26 is nothing but four steps so
  988. 45:33it's approximately logarithmic number of
  989. 45:37times the division happens it's
  990. 45:39approximately logarithmic number of
  991. 45:41times that the division happens that
  992. 45:43means it gets broken down into
  993. 45:46logarithmic of end time and you're also
  994. 45:49performing something like a merge every
  995. 45:53time so every time division by like this
  996. 45:56is div this is half and this is half
  997. 46:00this half Will Go On Till log base 2 N
  998. 46:04near about again everything is in terms
  999. 46:06of nearabout okay time complexities goes
  1000. 46:09till near about log base 2 and then
  1001. 46:11there is a merge happening how much is
  1002. 46:13merge taking if I have to think of the
  1003. 46:16extremest case if I have to think of the
  1004. 46:19extremest case can I say this is of size
  1005. 46:21three this is of size two so in order to
  1006. 46:25merge three and two elements
  1007. 46:27I'm going to require five steps at
  1008. 46:30minimum one comparison that's like
  1009. 46:32everyone is comparing in itself it's
  1010. 46:34like a near about B go of n that's the
  1011. 46:38worst case because for this I'll end up
  1012. 46:40taking at minimum of five steps because
  1013. 46:44every element is compared one by one and
  1014. 46:46then it is put over here it takes
  1015. 46:48minimum of five steps which is nothing
  1016. 46:50but the length of the array at the worst
  1017. 46:52case so can I say the merge I have to go
  1018. 46:57back to this uh there's so many things
  1019. 46:59written man I have to go back this can I
  1020. 47:01say this at the worst case takes B of n
  1021. 47:04this time over here it might take B of n
  1022. 47:07by two over here it might take n by4
  1023. 47:09something like this but at the worst
  1024. 47:11case it is taking n thereby I can
  1025. 47:14actually say that the time complexity is
  1026. 47:17nothing but n into log base 2 N now you
  1027. 47:22might ask me qu a question but St wait
  1028. 47:26once step two step three step four step
  1029. 47:28who will be counting these steps wait
  1030. 47:32that is actually compensated
  1031. 47:34how because the merge over here takes
  1032. 47:38big of n the merge over here takes n by2
  1033. 47:41the merge over here will take n by4 the
  1034. 47:42merge is taking lesser so in overall if
  1035. 47:46you try to add it up in mathematical you
  1036. 47:48will actually get somewhere near about n
  1037. 47:52into log base 2 N as the mer shorts time
  1038. 47:56complex so n log is the best average and
  1039. 47:59the worst case but if I have to ask hey
  1040. 48:02what is the space complexity of the
  1041. 48:03algorithm how much extra space are we
  1042. 48:05using so if you remember the algorithm
  1043. 48:08the algorithm is actually using indexes
  1044. 48:11no separate Ares is this the given array
  1045. 48:13that they are using are they using any
  1046. 48:15extra additional space they are during
  1047. 48:17merge in every merge step they're
  1048. 48:20creating a temporary array which at the
  1049. 48:22worst case might take B of in so the
  1050. 48:25space complexity at the worst case is
  1051. 48:28beo of n near about beo of n for this
  1052. 48:32particular algorithm so with this I will
  1053. 48:34be completing M short and the time is uh
  1054. 48:373:28 a.m. in the morning as you can see
  1055. 48:40I cannot record in the day because
  1056. 48:41there's a lot of background noise so
  1057. 48:44just in case you understood everything
  1058. 48:46please please consider hitting that like
  1059. 48:48button and if you haven't checked out
  1060. 48:50sters a to DSA course the link will be
  1061. 48:52in the description please make sure to
  1062. 48:54check it out because we have notes for
  1063. 48:56every everything we will be attaching
  1064. 48:58video solutions to everything yes most
  1065. 49:01of the advanced topics have been covered
  1066. 49:03previously I'm starting to cover the
  1067. 49:05basic topics now so you can definitely
  1068. 49:07proceed ahead with the advanced topics
  1069. 49:09in case you need for Advance as well so
  1070. 49:11guys if you're new to our Channel please
  1071. 49:12do consider hitting that subscribe
  1072. 49:14button because that is the only thing
  1073. 49:15that keeps you motivated to make these
  1074. 49:17kind of videos and and yeah to follow
  1075. 49:19our tradition please make sure you
  1076. 49:20comment understood in the comments so
  1077. 49:22that I get a VI that you guys are
  1078. 49:24understanding everything so with this
  1079. 49:25I'll be wrapping up this video if you
  1080. 49:27haven't followed me on Instagram Twitter
  1081. 49:28LinkedIn the links will be in the
  1082. 49:30description with this let's wrap up this
  1083. 49:31video and meet in some other video till
  1084. 49:33then
  1085. 49:33byebye your heart is
  1086. 49:37broken your
  1087. 49:39golden I will

About this transcript

This page contains the full transcript of Merge Sort | Algorithm | Pseudocode | Dry Run | Code | Strivers A2Z DSA Course by take U forward, generated from the public captions YouTube serves with the video. The transcript has 8,560 words across 1,087 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.