YouTube2Text

Quick Sort For Beginners | Strivers A2Z DSA Course — Transcript

by take U forward · 6,431 words · 867 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 video from the
  4. 0:08Strivers A to Z DSA course just in case
  5. 0:10you don't know about this and for the
  6. 0:12first time here this is world's most
  7. 0:14in-depth DS algo course why do I say
  8. 0:17that because you can go over the entire
  9. 0:19internet you can buy any of the paid
  10. 0:21courses in the market none of them will
  11. 0:23be covering any DS algo stuff in such
  12. 0:27depth this course has 456 modules and
  13. 0:31you'll be covering 400 plus problems in
  14. 0:34this particular course itself so once
  15. 0:36you complete the course you will be
  16. 0:38ready for any of the DS algorithms in
  17. 0:41any of the companies in any part of the
  18. 0:42world that is my guarantee to you so in
  19. 0:45this video we will be learning about
  20. 0:47quick sort what about these couple of
  21. 0:49topics now these couple of topics are
  22. 0:51very self-explanatory so what I will be
  23. 0:53doing is I'll be adding notes to them so
  24. 0:55that once you go through the entire
  25. 0:56notes you will be able to understand
  26. 0:58that by yourself a video is not required
  27. 1:01for these couple of topics so that's why
  28. 1:02I will not be adding videos so in this
  29. 1:05video we will be talking about quick
  30. 1:07sort so without waiting let's get
  31. 1:09started quick sort algorithm what does
  32. 1:12the algorithm do very obvious to guess
  33. 1:13yes it does sort any of the given data
  34. 1:17structure in an ascending order but if
  35. 1:20you do some minor tweaks to it yes if
  36. 1:23you do some minor tweaks to it it'll be
  37. 1:25also sorting it in an descending order
  38. 1:28so in this video I'll be teaching you
  39. 1:30the quick sort algorithm that sorts the
  40. 1:33given data structure in an ascending
  41. 1:35order in depth so that you can do the
  42. 1:37minor tweaks by yourself and you can
  43. 1:39sort the data structure in a descending
  44. 1:42order as well so what will be the flow
  45. 1:44of the lecture very simple first will be
  46. 1:46explaining the algorithm then I'll be
  47. 1:48telling you the intuition behind every
  48. 1:50step of the algorithm once we are done
  49. 1:52with that we'll be going and writing the
  50. 1:54pseudo code now remember one thing I'll
  51. 1:56be adding the pseudo code in such a way
  52. 1:58that you can code it in C plus plus Java
  53. 2:00python or any of the programming
  54. 2:01languages you code in and so this
  55. 2:03tutorial is going to be language
  56. 2:06independent and then we will be doing a
  57. 2:08dry run on the pseudocode so that the
  58. 2:10recursion is clear on your head once
  59. 2:13these three steps are done we'll be
  60. 2:15moving to the online compiler and
  61. 2:17compiling it so that you get an essence
  62. 2:19of the online compiler post that I'll be
  63. 2:21discussing the time complexity and the
  64. 2:23space complexity as well
  65. 2:26so you must be thinking let's drive a
  66. 2:28bit we already know about the merge sort
  67. 2:32algorithm which has a Time complexity of
  68. 2:34n log n and it has a space complexity of
  69. 2:37bigo of n where we use an extra
  70. 2:40temporary array y pixel so quick sort
  71. 2:44has a Time complexity similar to
  72. 2:46moisture and login but doesn't use us
  73. 2:48any extra temporary array yes it uses
  74. 2:51recursion stack space but no temporary
  75. 2:54array so quick sort is slightly better
  76. 2:57than merge sort but
  77. 2:59does that mean you don't need to know
  78. 3:00Marshall no you need to because there
  79. 3:02are a lot of problems which can be
  80. 3:04solved just by using merge and we will
  81. 3:08be seeing that in the arrays playlist
  82. 3:09when we will be solving problems related
  83. 3:11to more short term it's very important
  84. 3:12that you understand all the Sorting
  85. 3:14algorithms because you never know in an
  86. 3:16interview the interviewer might ask you
  87. 3:18any of the Sorting algorithms might ask
  88. 3:20you any question that might be similar
  89. 3:22to a particular sorting algorithm so do
  90. 3:24not skip any of the Sorting algorithms
  91. 3:26that is what my recommendation to you
  92. 3:28will be so let's get into the
  93. 3:31explanation of the algorithm and we will
  94. 3:32be using this particular array as an
  95. 3:34example to understand the algorithm so
  96. 3:37the first step of the algorithm is very
  97. 3:39simple if you are given an array the
  98. 3:42first step is very simple pick a paper
  99. 3:45remember this pick a paper what is a
  100. 3:49paper pivot can be any element in the
  101. 3:51array any element
  102. 3:53can be the first element
  103. 3:54can be the last can be the median can be
  104. 3:58the random or can be any element by your
  105. 4:01choice any element by your chart there
  106. 4:03is no particular rule to pick up effort
  107. 4:06no particular Rule now
  108. 4:08once you have picked the pivot so for an
  109. 4:11example we say that we will be following
  110. 4:13the first two and we'll be picking up
  111. 4:14the pivot as the first element of Any
  112. 4:18Given array to us so I'll be like okay
  113. 4:20we're given this array so we will be
  114. 4:22picking up the pivot as poor that is my
  115. 4:26job I'll say Okay given this array so
  116. 4:28I'll be picking pivot as 4 first element
  117. 4:31at the ending one after that
  118. 4:33pick that element and place it in its
  119. 4:36correct place in the sorted array what
  120. 4:38does it mean now this array is unsorted
  121. 4:41agreed
  122. 4:43but if I have to ask you what is the
  123. 4:45sorted array can I say the sorted array
  124. 4:47will be one two three four five six
  125. 4:49seven nine
  126. 4:50can I and if I have to write it will it
  127. 4:52be something like 1 2 3 4 5 6 7 9. and
  128. 4:57if I ask you what is the correct place
  129. 4:58of food
  130. 5:00so I have to ask you what is the correct
  131. 5:02place of 4 you'll be saying correct
  132. 5:04place of 4 is this which is the fourth
  133. 5:07place it is currently lying at the first
  134. 5:09place but actually the correct place of
  135. 5:114 is at the fourth piece that is what
  136. 5:13you'll do you'll pick up four and you'll
  137. 5:15say
  138. 5:16picked up an element and place it in its
  139. 5:19correct place and just take it and place
  140. 5:21it in its correct place that is what you
  141. 5:22have to do pick up a paper and place it
  142. 5:25in its correct place
  143. 5:27now how will that help let's understand
  144. 5:30I said pick a paper so you pick the four
  145. 5:33and you pleased it in its correct Place
  146. 5:36done then you picked up six for an
  147. 5:38example just for an exam and you placed
  148. 5:41it at its correct place which is this
  149. 5:43then you picked up two and placed it at
  150. 5:46its correct place then you picked the
  151. 5:47five and placed it as at its correct
  152. 5:49Pace then you picked up seven and place
  153. 5:52it at its correct please and you picked
  154. 5:54up nine and placed it added Skype please
  155. 5:57then you picked up one and placed it at
  156. 5:59his connect place and he picked up three
  157. 6:00and placed it at its correct please will
  158. 6:04the array be sorted at the end of the
  159. 6:06day that is the reason the intuition was
  160. 6:09very simple pick up a pivot and pivot
  161. 6:11can be anything doesn't matter what you
  162. 6:13pick because at the end of the day if
  163. 6:16you're putting them into its correct
  164. 6:17place and every element is put the array
  165. 6:20will be sorted it doesn't matter what
  166. 6:21you pick as long as you're putting them
  167. 6:24into the correct place at the end of the
  168. 6:26day if I've picked up every element the
  169. 6:28area will be sorted that's the reason
  170. 6:30any of them will work but we will go
  171. 6:33with first element you can go with the
  172. 6:34last median whatever you wish to Okay so
  173. 6:37we've understood the algorithm pick up a
  174. 6:39paper and place
  175. 6:41it in its correct place in the sorted
  176. 6:43array but what happens to other elements
  177. 6:45so the rulers the uh step number two is
  178. 6:47very simple
  179. 6:48smaller on the left remember this
  180. 6:51smaller on the left
  181. 6:55and large on the right how I'll explain
  182. 6:58you that
  183. 6:59afterwards but as of now just understand
  184. 7:01the explanation and the intuition of the
  185. 7:03algorithm small on the left and the
  186. 7:06larger on the right so what are the
  187. 7:07smallest is six smaller no so six will
  188. 7:10go on the right it's two smaller yes on
  189. 7:13the left is five smaller no greater so
  190. 7:16goes on the right is 7 smaller no it
  191. 7:18goes on the right is nine smaller no
  192. 7:20goes on the right is one smaller yes
  193. 7:22it's three smaller years so if you see
  194. 7:26can I see I have two one three
  195. 7:29and I have six five seven nine so these
  196. 7:32are greater
  197. 7:34and these are smaller
  198. 7:36and can I say after one step after one
  199. 7:42step can I say this that one element is
  200. 7:47at its correct place in the array I can
  201. 7:49so what is left can I say the left
  202. 7:52portion
  203. 7:53is still an unsorted and the right
  204. 7:56portion is still unsorted now why is the
  205. 7:59step what is the intuition behind
  206. 8:01smaller on the left and larger on the
  207. 8:03right if you think if you think in a
  208. 8:06very logical manner a four is at its
  209. 8:09correct place the left guys to the four
  210. 8:12in the sorted array will be smaller and
  211. 8:15the right guys will be greater that's
  212. 8:17why it's logical to say smaller go on
  213. 8:21the left and larger go on the right so
  214. 8:23that's the intuition behind the step two
  215. 8:25you have understood the intuition behind
  216. 8:27both the steps now you what you need to
  217. 8:30do is repeat these steps repeat these
  218. 8:32steps again and again and how do you do
  219. 8:34that very simple can I say four is done
  220. 8:36what is left the left portion and the
  221. 8:39right portion let's take the left
  222. 8:41portion two one three can I say it's an
  223. 8:43individual array it is so let's take
  224. 8:46that individual array two one three and
  225. 8:48let's again apply the same books what is
  226. 8:51of step one pick a paper what is our
  227. 8:53rule
  228. 8:54first element
  229. 8:56let's go and pick up the first element
  230. 8:57as the pivot
  231. 8:58two if I ask you the correct order
  232. 9:01if I have to write the sorted out array
  233. 9:03can I say this will be the sorted array
  234. 9:05can I say the correct order of two will
  235. 9:07be
  236. 9:08here
  237. 9:09thereby
  238. 9:11step two correct order I put it I've
  239. 9:13picked up every word and put it into the
  240. 9:15correct order step two
  241. 9:17small on the left so one is smaller on
  242. 9:20the left T is larger on the right so can
  243. 9:24I say 2 is done and we are left with one
  244. 9:27and three so again two smaller arrays
  245. 9:30but whenever an array has one element
  246. 9:33no need to do anything why one element
  247. 9:36is sorted in itself so can I say at the
  248. 9:40end you made this array to one two three
  249. 9:42so at the original places this will
  250. 9:45change this will change and this will
  251. 9:47change so the left guy got sorted that's
  252. 9:51the time for the right guy six five
  253. 9:52seven nine so let's do it for the right
  254. 9:55guys well six five seven nine
  255. 9:57so I'll write it six five seven nine
  256. 10:00rule one on the sari yes so I did it
  257. 10:04very simple four was done did the same
  258. 10:07algorithm on the left did the same
  259. 10:09algorithm on the right so kind of
  260. 10:10recursion kind of get you get an idea of
  261. 10:12recursion take the array get the paper
  262. 10:15do everything then call it for the left
  263. 10:17call it for the right kind of you're
  264. 10:20getting this yes next what's the next
  265. 10:23stop for the right direction six five
  266. 10:25seven eight what's your favorite
  267. 10:27six you have to hypothetically again
  268. 10:29same rule hypothetically you have to
  269. 10:31write it what will be five six seven
  270. 10:33eight so what's the correct order of six
  271. 10:35yes this one smaller on the
  272. 10:39larger on the right
  273. 10:41so can I say two arrays again five seven
  274. 10:44nine so can I hypothetically say that
  275. 10:47six was placed at its correct place so
  276. 10:50I'll go ahead and say
  277. 10:52six was placed at its correct place
  278. 10:54right and can I say five can I say five
  279. 10:58is a single array 5 is also placed at
  280. 11:01its correct place I can what is left
  281. 11:03this seven nine so again another array
  282. 11:06so let's take the seven nine as another
  283. 11:08array again pick up and pivot seven is
  284. 11:11the pivot what's the correct place of
  285. 11:12paper
  286. 11:137 itself and the other one is nine which
  287. 11:16is the greater so on the right so 7 is
  288. 11:20placed again go ahead and say 7 is done
  289. 11:24and now 9 is the single element so
  290. 11:26that's also done so at the end can I see
  291. 11:29this is also sorted so at the end can I
  292. 11:33say the array automatically did it for
  293. 11:36all the elements and each of them is at
  294. 11:39its correct place can I say that I can
  295. 11:41thereby the quick sort algorithm works
  296. 11:45this is the explanation and the
  297. 11:47intuition done
  298. 11:50explanation and the intuition it's very
  299. 11:53simple if I have to explain it in a
  300. 11:55shorter way pick up a pivot place it in
  301. 11:58its correct Place smaller on the left
  302. 12:00larger on the right and now the question
  303. 12:02comes how do you do this in a code how
  304. 12:05do you do this in the code so this is
  305. 12:07what we will be understanding in the
  306. 12:09pseudo code because picking a pivot is
  307. 12:12very easy because first element is kind
  308. 12:14of easy but now you might be thinking
  309. 12:15but how do you please the four in its
  310. 12:18correct place because by naked eye it's
  311. 12:20very simple this is the sorted way so
  312. 12:22you are seeing four here so you're
  313. 12:23saying 4 will be here how do you code it
  314. 12:25that's where you have to understand the
  315. 12:28algorithm in depth this other thing that
  316. 12:30you have to understand in order to do
  317. 12:31the pseudo code so if you carefully see
  318. 12:34this is the entire array right and once
  319. 12:37you have placed four you go for the left
  320. 12:39and you go for the right now the next
  321. 12:42doubt that might be coming up to your
  322. 12:43mind is but striver we have an entire
  323. 12:45array and then you're placing the pivot
  324. 12:47at its correct please then you're going
  325. 12:49to the left and going to the right so do
  326. 12:51you create new arrays no we'll be using
  327. 12:54the concept of low and high that we used
  328. 12:57in the merge sort instead of building
  329. 13:00new arrays we'll be using pointers to
  330. 13:03explain that okay
  331. 13:05the left array is from here to here the
  332. 13:08right array is from here to here we'll
  333. 13:09be using kind of pointers so let's
  334. 13:12understand the explanation in terms of
  335. 13:14pseudo code so that we can code so
  336. 13:16before going to the pseudo code let's
  337. 13:18understand how do we perform these
  338. 13:20couple of steps using a bit of variables
  339. 13:24and code logic so that we can actually
  340. 13:26write the pseudo code so imagine you're
  341. 13:28given this particular array and you have
  342. 13:30to apply quick sort to it and this is an
  343. 13:32unsorted array so will you be carrying
  344. 13:34this entire array logically doesn't make
  345. 13:37sense why because in the future you know
  346. 13:39these arrays will be broken down into
  347. 13:42smaller steps so we have to recreate new
  348. 13:44arrays so we will not be doing that
  349. 13:46instead of that we will be creating low
  350. 13:48pointer which will be pointing to the
  351. 13:49zeroth index which is the first time and
  352. 13:52we'll be keeping an high pointer which
  353. 13:53will be pointing to the last cat so what
  354. 13:56does low and high mean it means the
  355. 13:58array is from here to here very simple
  356. 14:00now what did I see what's the payment so
  357. 14:04if pivot is very simple a pivot will be
  358. 14:05the array of loom
  359. 14:08why we said paper will be the first
  360. 14:10element so the first element in the
  361. 14:12array is array of flow so first step is
  362. 14:15done
  363. 14:16that was picking up a pivot what's the
  364. 14:18next step placing the pivot in its
  365. 14:21correct place right so let's do it how
  366. 14:24do you do that in order to do that you
  367. 14:26take a i Pointer Place it at the lower
  368. 14:30or the leftmost part of the array
  369. 14:33and then take a j and place it to the
  370. 14:36right most part of the area done now you
  371. 14:39say okay let's find
  372. 14:42the first element which is greater than
  373. 14:47my Pivot
  374. 14:49because
  375. 14:50if it's smaller than it is at the
  376. 14:53correct place but if it is greater then
  377. 14:55then we will find it so let's find
  378. 14:57is 4 greater than the pivot no because
  379. 15:00what's your favorite now if I have to
  380. 15:01write the pivot the pivot is 4 so is 4
  381. 15:04greater than the pivot no is 6 greater
  382. 15:07than the pivot yes so what you will do
  383. 15:09is you will say Okay 6 is greater than
  384. 15:11the paper so you'll stop at six so your
  385. 15:14I pointer
  386. 15:15stops at six and then you will go and
  387. 15:18say okay let's find the first element
  388. 15:20that is smaller than the pivot from the
  389. 15:23right
  390. 15:24I'll say three are you smaller than the
  391. 15:26paper he says yes I am so I'll stop
  392. 15:29because 3 is smaller
  393. 15:31so you have figured out the first guy
  394. 15:33who is greater than the paper on the
  395. 15:35left and the first guy was smaller than
  396. 15:37the pivot on the right
  397. 15:40so you just have a swap so it's a 3 and
  398. 15:43you say six so you just swap them done
  399. 15:47with the swap what is the next job that
  400. 15:49you will do next job again come back and
  401. 15:52do the same thing figured out the first
  402. 15:55element from the left which is greater
  403. 15:58than the pivot is 3 greater than the
  404. 15:59pivot no so just go ahead is
  405. 16:032 greater than the paper no go ahead is
  406. 16:065 greater than the pivot yes 5 is
  407. 16:08greater than the pivot so I has stopped
  408. 16:11at a element which is greater than the
  409. 16:14pivot now go to the right and find the
  410. 16:16first element that is smaller than pivot
  411. 16:18is 6 smaller than pivot no so move
  412. 16:22is one smaller than the pivot yes stop
  413. 16:26so five
  414. 16:28and one will swap one and five swapped
  415. 16:33perfect
  416. 16:34let's again continue the same step
  417. 16:37we say okay on the left find the first
  418. 16:40who is greater is one greater
  419. 16:43is one greater than pivot
  420. 16:45no is 7 greater than pivot yes we stop
  421. 16:49there
  422. 16:51so it stops at seven on the right find
  423. 16:54the first who is lesser than pivot
  424. 17:00so we move is 9 less than paper no we
  425. 17:05move is 7 lesser than paper no we move
  426. 17:08is
  427. 17:093 is 1 lesser than pivot yes and we stop
  428. 17:14and we stop
  429. 17:15so we stop here the J stops here like J
  430. 17:19goes here J goes here and J then stops
  431. 17:22here so apparently we stopped at a
  432. 17:25position where J has crossed I it has
  433. 17:30crossed I you stop you do not swap
  434. 17:32anymore do not swap it anymore which
  435. 17:34means you are in the territory you are
  436. 17:37in the territory where if you see
  437. 17:39everything is smaller than the pivot and
  438. 17:42on the right you are at a territory
  439. 17:44where everything seven nine five six is
  440. 17:47greater than pivot so you stop what's
  441. 17:50the last job you'll do since you stopped
  442. 17:52at one you just take this four and you
  443. 17:54place it here so you basically say
  444. 17:56okay one and four goes here so if you
  445. 18:00see what happens is one three two four
  446. 18:03is that it's gonna Place seven nine five
  447. 18:06six
  448. 18:08as super sum
  449. 18:10that's very simple and we are done with
  450. 18:12this yes that's
  451. 18:14that is how easy it was so once you have
  452. 18:17placed your pivot at the correct index
  453. 18:20in the sorted array can I call this as
  454. 18:23the partition index why can I call this
  455. 18:26as a partition index very simple the
  456. 18:29reason being
  457. 18:30this is where
  458. 18:32it's at the correct order in the sorted
  459. 18:34array this is not in the correct order
  460. 18:38and this is not in the correct order
  461. 18:39it's a kind of partition left is still
  462. 18:43to be done and the right is still to be
  463. 18:45done so that is why you call it as a
  464. 18:46partition so can I say as of now the low
  465. 18:50was pointed here and the high was
  466. 18:51pointed here can I see the new array
  467. 18:54that you have to sort according to the
  468. 18:56same algorithm is from here to here and
  469. 19:00it's from here to here so can I see the
  470. 19:02new array one array will be low to
  471. 19:04partition minus one
  472. 19:07and the other array will be partition
  473. 19:09plus one comma high this is where the
  474. 19:13this array is this and this array is
  475. 19:15this so what you do is perform a quick
  476. 19:17sort again and perform a quick sort
  477. 19:19again on this that's it and on this
  478. 19:22array again repeat the same angle and
  479. 19:24once you have done this you will be able
  480. 19:27to completely sort the array right so I
  481. 19:30think I've understood the explanation
  482. 19:31the intuition and kind of the algorithm
  483. 19:33as well as of now it's time to write the
  484. 19:36pseudo code the pseudo code is going to
  485. 19:37be very simple we say quick sort and we
  486. 19:40know it will be given an array and we
  487. 19:43know the array in the array the area
  488. 19:46that we have to sort will be from low to
  489. 19:48high
  490. 19:48that is something which we definitely
  491. 19:50know and we know we will only sort the
  492. 19:52array if it has more than one elements
  493. 19:55that means a flow is lesser than high
  494. 19:58because if low is equal to high in that
  495. 20:01case assume high is here which means
  496. 20:03this is the array one element is already
  497. 20:05sorted so if low is lesser than high
  498. 20:08that means it has more than one elements
  499. 20:10thereby we will be sorting it
  500. 20:13and this is what kind of the algorithms
  501. 20:16gesture will look like
  502. 20:18if low is lesser than I what do we do
  503. 20:21imagine you're given this array what did
  504. 20:23we do at first
  505. 20:25I figured out the pivot
  506. 20:27put it at its correct place and then
  507. 20:30sorted the left half and then the right
  508. 20:32half so what did we do at the first step
  509. 20:35place the pivot at its correct place and
  510. 20:37found out where it was placed and then
  511. 20:40called for the left and then called for
  512. 20:41the right so can I say okay let's call a
  513. 20:44function let's call a function which
  514. 20:47will do that so I'll tell the function
  515. 20:49that for this array low to high please
  516. 20:53find out a pivot place it at its correct
  517. 20:56place and tell me where you placed it
  518. 20:58like for this case you will be placing
  519. 21:00it here tell me where you placed it and
  520. 21:02I'll call that as partition index
  521. 21:05partition index
  522. 21:08done
  523. 21:09once I've placed it at its correct place
  524. 21:12I'll have other guys because after the
  525. 21:15algorithm after the function has taken
  526. 21:17place
  527. 21:18how will it look like it looked like
  528. 21:20something like two
  529. 21:21one three and this side will look like
  530. 21:24six
  531. 21:25five seven nine
  532. 21:27after the function has taken place
  533. 21:30so now you have to sort this portion of
  534. 21:32the array and this portion of the array
  535. 21:34and the partition index is here so can I
  536. 21:36again call it
  537. 21:38quick sort go for the left now which is
  538. 21:41from which is the array but in this
  539. 21:44array take from low to partition index
  540. 21:47minus one
  541. 21:48and in the right array take it from
  542. 21:52partition index plus 1 comma high again
  543. 21:56the lines can swap this can be at the
  544. 21:58first and then this can also be there it
  545. 22:00doesn't matter even if you swap it like
  546. 22:03this it's still okay because you have to
  547. 22:04sort the left and the right or the right
  548. 22:07and then the left any any way works as
  549. 22:10long as you are sorting it this is
  550. 22:12simple because
  551. 22:13the recursion will do its job because
  552. 22:15precaution has been called for this so
  553. 22:17again it will do a stop quite simple but
  554. 22:21the question comes up okay he understood
  555. 22:23till here but how do we write the
  556. 22:26function let's understand how do you
  557. 22:28write the function the function remember
  558. 22:30alighted with maybe a yellow
  559. 22:32the function has taken an array and has
  560. 22:36taken a low and has taken a high
  561. 22:39and you know it has to find a pivot and
  562. 22:41in our case the pivot is very simple we
  563. 22:44were saying the paper to be array of
  564. 22:46flow which is the first element
  565. 22:49and what did we do at every step if you
  566. 22:51remember we first kept I here and we
  567. 22:54kept a g here so let's do that so we
  568. 22:56said I is equal to array of low
  569. 22:59perfect
  570. 23:00or or maybe just just write it for the
  571. 23:03index I is at low and G is at high this
  572. 23:06is something which we said and what did
  573. 23:08we do
  574. 23:09we kind of said okay if I is here which
  575. 23:12is the first element greater than pivot
  576. 23:15and we've stopped here
  577. 23:17and then we swapped and we kept on doing
  578. 23:19this kept on doing this till
  579. 23:21I cross J and we had a left and a right
  580. 23:24so we kind of write okay
  581. 23:27keep on doing this
  582. 23:30so we keep repeating the steps now what
  583. 23:33are the steps
  584. 23:35find out because I was here
  585. 23:38find out the first guy that's greater
  586. 23:42than the pivot so okay while
  587. 23:46array of I is lesser than array of pivot
  588. 23:50that means it's okay this is not the guy
  589. 23:53that I'm looking for a 4 is less than
  590. 23:56equal to 4 I'm not looking for you I'm
  591. 23:58looking for someone greater than you so
  592. 24:00this is a correct condition and
  593. 24:02obviously I cannot exceed the arrays
  594. 24:05boundary which is definitely High
  595. 24:09it cannot exceed the RS boundary if
  596. 24:11that's the case we say okay I plus plus
  597. 24:15that means it keeps on going keeps on
  598. 24:17going till it doesn't finds an element
  599. 24:20greater so the moment it is at six the
  600. 24:22while loop will stop because that's
  601. 24:24where it has to and similarly from the
  602. 24:26right
  603. 24:28similarly from the right we say array of
  604. 24:31J from the right what do you need to
  605. 24:33find someone who is smaller so if this
  606. 24:36guy is greater than pivot
  607. 24:38if this guy is greater than pivot then
  608. 24:42what you will do is you will say Okay J
  609. 24:44again since you're going left
  610. 24:47just say this
  611. 24:49and you will say Okay J minus
  612. 24:53so what these lines will do is this will
  613. 24:56find the first element which is greater
  614. 25:00than the pivot and this will find the
  615. 25:01first element that is lesser than pivot
  616. 25:03and at the end of the day remember if
  617. 25:06they haven't crossed if they haven't
  618. 25:09crossed if you remember then you swap
  619. 25:11them you say array of I and array of J
  620. 25:15to be swapped quite simple
  621. 25:18quite simple and then you can close this
  622. 25:20this while loop gets closed here
  623. 25:23very very simple so if I have to show
  624. 25:26you the algorithm again
  625. 25:27let's do a dragon
  626. 25:29what happens first time I stops here and
  627. 25:32J stops here so 6 and 3 gets swept six
  628. 25:35and three next I will stop here next J
  629. 25:39will be stopping here so next one and
  630. 25:42five get swept next I goes and stops at
  631. 25:46seven because seven is greater than 4
  632. 25:50but J kind of stops here J goes here
  633. 25:53doesn't stops because 9 is greater J
  634. 25:55goes here doesn't stop because 7 is
  635. 25:57greater J goes your stops so if you see
  636. 26:00J has crossed i j has crossed I because
  637. 26:04J is at one so if J crosses I do not
  638. 26:07swap anymore because you are already at
  639. 26:10the left half three two one and you're
  640. 26:13already at the right half seven nine
  641. 26:16five and six you're already at the
  642. 26:18correct hubs so you do not do a swapping
  643. 26:21if they have cross they are already in
  644. 26:23the correct zones so at the end of the
  645. 26:25day where is J J is at here and Louis at
  646. 26:28here so that end of the day when this
  647. 26:31has this has crossed the Stop and we say
  648. 26:34okay it's time for the pivot to come
  649. 26:36it's correct please so swap the pivot
  650. 26:39which is the first element
  651. 26:41with the array of J which is the last
  652. 26:43element of the left index which is the
  653. 26:46last element of the left index so just
  654. 26:48swap it and this will come here and this
  655. 26:51will go here
  656. 26:52quite simple So eventually what you will
  657. 26:54get is
  658. 26:58three two one perfect that so once
  659. 27:02you've done this what do you need to do
  660. 27:04you need to tell them the partition
  661. 27:06index and you know the index is nothing
  662. 27:08but J because that's where the pivot
  663. 27:10came in so returns a this is how the
  664. 27:12function will look like quite simple
  665. 27:14this is how the function will look like
  666. 27:16again repeating find the first element
  667. 27:18greater than pivot find the first
  668. 27:21element smaller than paper swap again
  669. 27:23keep doing the same thing till you have
  670. 27:26an left and right once you have that
  671. 27:28just swap with the last level so if you
  672. 27:31are if you're able to understand this
  673. 27:32it's pretty much sharp also before going
  674. 27:35into the code I'll just like to mention
  675. 27:36one thing this portion is something
  676. 27:38which we have to be careful about and
  677. 27:41this portion is something again which we
  678. 27:43have to be careful about why because
  679. 27:45when you write this and you're doing I
  680. 27:46plus plus this might exceed the low to
  681. 27:50high array boundary because you are
  682. 27:53standing at the last and you're checking
  683. 27:54for the last and then then you're moving
  684. 27:56it might exceed the boundary so this
  685. 27:58instead of high will be high minus one
  686. 28:00this instead of low will be low plus one
  687. 28:02and there is other doubt that might come
  688. 28:05into your head why are we doing lesser
  689. 28:08than equal to and greater here now
  690. 28:11imagine you add 4 and you place three
  691. 28:15one two on the left and let's say five
  692. 28:17six seven nine on the right Boris please
  693. 28:20what if there was another four then he
  694. 28:22would have either place four on the left
  695. 28:24or either on the right so I will say on
  696. 28:28the left we will place everything lesser
  697. 28:30than equal to on the right we'll Place
  698. 28:32everything greater than equal to that's
  699. 28:34why we are finding greater and we are
  700. 28:37finding lesser over here got it why
  701. 28:39because you have to decide where do you
  702. 28:42put the equal to guys like if there is
  703. 28:44someone equal to weight you place them
  704. 28:46you have to decide on that as well so
  705. 28:49you place it on the left in that case
  706. 28:50you do it like this if you place it on
  707. 28:52the right then you have to do the vice
  708. 28:53versa I hope that does make sense so I
  709. 28:56can say that we are done with the pseudo
  710. 28:58code and the dry run as well because
  711. 28:59while I was writing the code I was doing
  712. 29:01the dragon as well and now going back to
  713. 29:03the recursive Dragon for these couple of
  714. 29:05things I'll recommend you to watch my
  715. 29:08merge sort video because in that I've
  716. 29:11explained how these couple of things uh
  717. 29:14split the arrays and how individual
  718. 29:16stuffs work so just in case you are not
  719. 29:17aware how the recursion Works merge sort
  720. 29:21tutorial is a classic example where I've
  721. 29:23done the entire Dragon so that you
  722. 29:24understand recursion in depth how sub
  723. 29:28problems are solved and everything
  724. 29:29by the way before moving uh to coding it
  725. 29:33in an online compiler I just wanted to
  726. 29:35quickly tell you something this is a
  727. 29:37divide and conquer algorithm because
  728. 29:39we're kind of dividing it and when you
  729. 29:42divide it every part is getting solved
  730. 29:44by itself so that's why divide and then
  731. 29:47you conquer basically solve so it's a
  732. 29:50divide and conquer algorithm
  733. 29:52now coming into the online compiler will
  734. 29:54be quickly coding the quick sort
  735. 29:56algorithm by the way the problem link is
  736. 29:58in the description make sure you uh code
  737. 30:01it as well and submit so coming to over
  738. 30:03here uh we have to return a vector so
  739. 30:06assume we have a quick sort we say array
  740. 30:09is 0 and array dot size minus one at the
  741. 30:12end of the day we are returning the
  742. 30:13array itself because we have to return
  743. 30:14it and now let's write the void quick
  744. 30:17sort and Vector end and then array and
  745. 30:20then IND low Indy high and we know if
  746. 30:24low is lesser than high then only we do
  747. 30:26it like for one elements or that we will
  748. 30:29not do it and we have to get the
  749. 30:30partition index and for that we can call
  750. 30:33the partition function
  751. 30:35and the partition function can take
  752. 30:37array low and high
  753. 30:39and then we can call the quick sort for
  754. 30:41array and low P index minus 1 and we can
  755. 30:45also call the quick sort for array P
  756. 30:48index plus 1 comma High that's it that
  757. 30:51is what I will do and over here we can
  758. 30:53say partition and we can say vector and
  759. 30:56array and we can take the low we can
  760. 30:59take the high and can I say that the
  761. 31:02pivot will be array of low we can say
  762. 31:05the array I will be this we can say the
  763. 31:07J pointer will be this if you remember
  764. 31:09well enough and we can just go on till
  765. 31:10ILS then J and we know that we have to
  766. 31:13go on
  767. 31:14till finding the first element that is
  768. 31:17greater than pivot on the left so we go
  769. 31:20on till it is less than equal to and we
  770. 31:23stop at lesser than high minus 1 so that
  771. 31:26it doesn't crosses the boundary when we
  772. 31:27do I plus plus and we also on the right
  773. 31:31have to find the element that is smaller
  774. 31:33so we go on till it is greater than
  775. 31:36pivot not equal to because the equal to
  776. 31:39guys will be on the left and then we go
  777. 31:41on the same reason low plus 1 that we do
  778. 31:44not cross it and at the end of the day
  779. 31:47if they haven't crossed let's take the
  780. 31:50right guys put them onto the left and in
  781. 31:52the left guys put them on to the right
  782. 31:53the swap of array of I array of J
  783. 31:56perfect at the end once everything is
  784. 31:58done we know J will be at the last of
  785. 32:01the left so swap the pivot which is at
  786. 32:04array of low with the last of the left
  787. 32:06and since the pivot goes to J we say
  788. 32:08that's the partition index that's how
  789. 32:10you can write the partition once you've
  790. 32:12done this you can quickly go and run
  791. 32:15this all right it's giving me time limit
  792. 32:17exceeded why is that happening
  793. 32:21because I've written less than equal to
  794. 32:23it will be greater than equal to
  795. 32:24my bad it All Passes Now quickly let's
  796. 32:27submit it and see if it's running
  797. 32:29absolutely fine
  798. 32:30it is so now moving back uh to the iPad
  799. 32:33to discuss the time complexity so the
  800. 32:37time complexity of this particular
  801. 32:39algorithm is n log n y is it n log n
  802. 32:44again the same reason as merge sort
  803. 32:47because every time you're kind of
  804. 32:50splitting them into two halves now over
  805. 32:52here the halves are for sure getting
  806. 32:55into like something and something then
  807. 32:57again this is getting into something and
  808. 32:58something so it's kind of not getting
  809. 33:01equally split as in most shot but
  810. 33:04they're getting into a couple of halves
  811. 33:06so this half will be going on like the
  812. 33:08height of the tree if you remember the
  813. 33:10more short video will be log base to n
  814. 33:12that's how uh because 2 to the power
  815. 33:14something log base to n and y n because
  816. 33:18every partition every this function is
  817. 33:22going through the entire array itself so
  818. 33:24it's a big O of n for everything so n
  819. 33:27cross it log n and the space complexity
  820. 33:29we are not using any extra space yes
  821. 33:31there is a recursion stack space but
  822. 33:33let's not compute it so the this is the
  823. 33:36time complexity in the space complexity
  824. 33:37I did not explain any login in depth
  825. 33:40because this is a continuous course I'm
  826. 33:43assuming you have seen the merge sort
  827. 33:45video where I've discussed in depth
  828. 33:47about n login so go back and watch it
  829. 33:49because over here we perform the similar
  830. 33:52kind of recursion calls so it's very UNL
  831. 33:54like this being big of n is self
  832. 33:57explaining why big often because you're
  833. 33:59moving I from low till high and you're
  834. 34:03moving J from I to low that's a b go off
  835. 34:06and it's a very obvious case and for
  836. 34:08these couple of things it's log n so
  837. 34:11comes from my shot I've explained that
  838. 34:13over there so the time complexity is l
  839. 34:15login and the space complexity is Big of
  840. 34:18one not taking recursive stack space
  841. 34:20into account so that's the time
  842. 34:21complexity and space complexity so with
  843. 34:24this I can say I have wrapped up more
  844. 34:26shot as well and the couple of notes
  845. 34:27will be added very very soon so guys
  846. 34:30with this we complete step two of
  847. 34:32Strivers A to Z DSA course the next
  848. 34:35thing will be step three where we will
  849. 34:36be starting with the arrays playlist so
  850. 34:38guys I hope you have understood the
  851. 34:39quick sort algorithm in depth so just in
  852. 34:42case you did please make sure you hit
  853. 34:43that like button and if you're new to
  854. 34:45our Channel what are you doing kill that
  855. 34:47subscribe button right away and to
  856. 34:49continue our tradition do comment
  857. 34:51understood so that I understand that you
  858. 34:52have understood everything and also as
  859. 34:54an assignment yes as an assignment you
  860. 34:57have to write this code for descending
  861. 34:59and you have to paste that code in the
  862. 35:01comments I'll be checking out the
  863. 35:02comments for sure and here with this
  864. 35:04I'll be wrapping up this video let's
  865. 35:06read in some other videos till then bye
  866. 35:07bye take care
  867. 35:10don't ever forget your golden I will

About this transcript

This page contains the full transcript of Quick Sort For Beginners | Strivers A2Z DSA Course by take U forward, generated from the public captions YouTube serves with the video. The transcript has 6,431 words across 867 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.