Thank you for demonstrating all the detail and steps many algorithm books skip or leave exercises to readers.
@sjwang38923 жыл бұрын
The only video that explained this clearly to me. Thank you!
@arunm6195 жыл бұрын
1:14 Not gonna lie, Thought Thanos snapped in the first half
@ashishmishra80825 жыл бұрын
You explained it as a specific example of segregating 0,1 and 2 but it helped me to understand 3 way partition of Quicksort. Thanks sir
@jainmanan91823 жыл бұрын
Thankyou Sir, best explanation on KZbin :)
@RajaRam-fv1mm3 жыл бұрын
Really a detailed explanation that makes more sense. Thank you so Much Sir
@Vishal-nc8ju5 жыл бұрын
best channel on yt to learn and improve skills from basics
@praveenchouhan63884 жыл бұрын
awesome explaination !!!!!!!! love the clarity of ur approach and visual analysis.
@skubydubydu5 жыл бұрын
Counting method also takes O(n) time. It is also taking least time. The reason the swapping method is asked in interviews is because in real scenarios the actual data set is not going to contain just simple numbers. It will be made of records which need to be sorted based on one of the properties of the records.
@GurpreetSingh-yy5fn4 жыл бұрын
counting method takes two traversals while this algorithm takes only one traversal
@udaykadam54554 жыл бұрын
Doesn't matter even if the data is in the string format. You can still count the number of occurrences. It's just that, interviewer will ask for another solution. Combination of double pointer approach along with hoare partion algo can solve this problem within linear time.
@AvinashKumar-ps8wl3 жыл бұрын
Best Explaination on KZbin
@abhisheksarkar_neo7 жыл бұрын
As proposed at the start of the video, counting is also a way to do it and that will also take O(n) time. The reason we cannot do that is because, it will not work in cases where you have any set of numbers, not just 0, 1 and 2 and you want to partition the numbers in 3 buckets
@sravanvedala78117 жыл бұрын
well even with this approach also, you need as many buckets as that of your number groups. For example if you have 0 to 5 numbers repeated in an array, then you need to modify this algorithm to address grouping of 0 to5 numbers in which you case you end up having 6 buckets even with this algorithm. This won't work out of the box if your array has numbers other than 0,1 and 2. However caveat with just counting is, you need another array.
@spetsnaz_25 жыл бұрын
If you do it by counting, then it will take 2 passes to complete the program, but this algo will do the job in a single pass. And this algo is only made for this kind of problems...if we have more no. of distinct elements, why not to use other sorting techniques :P
@krishnataramanandhar5812 жыл бұрын
Thank you for the video that clearly explains how to segregate different value elements,
@ThinkProgramming-Hub Жыл бұрын
A very great and detailed explanation🤝, you make learning fun👏
@niklasschulte16906 жыл бұрын
very nice and clean explaned, thanks!
@JayDonga4 жыл бұрын
You demonstrated algorithm and it works really well. However if someone has never encountered this solution before and they get this problem in interview, in that case how to develop this algorithm from scratch? Appreciate your inputs.
@ankitchawla84874 жыл бұрын
Good Explanation. Very easy to understand thanks a lot for making this video.
@adityagunda21223 жыл бұрын
awesome explanation. easy to understand
@aishwaryasingh2465 жыл бұрын
Amazing content! Thanks for the algorithm videos. It has helped me a lot.
@Akash-Bisariya3 жыл бұрын
How easy you made this problem... Wow!!!
@vivekanandkhyade3 жыл бұрын
Thanks a lot
@Badatbackflips4 жыл бұрын
thank you for the very clear (and concise!) explanation
@angitd37667 жыл бұрын
Best explaination so far
@minirasamedova6483 жыл бұрын
Please keep them coming!!
@CASHTECH12344 жыл бұрын
Good video and explanation. The first 5 mins are sufficient if you already have some grip on algorithms
@hiteshusingh85714 жыл бұрын
Very nice explanation
@chaoschao94326 жыл бұрын
This is related to leetcode problem 75.
@rahulchandrabhan4 жыл бұрын
very easy explanation..Nice
@DurgaShiva75743 жыл бұрын
could the while condition be changed to while(mid
@subrinasirajee5303 жыл бұрын
Well explained. Thanks a lot.
@vivekanandkhyade3 жыл бұрын
Glad it was helpful!
@kewtomrao3 жыл бұрын
So mid travels through the array and when 1 is encountered,we allow it to be in its place but if its 0 we swap it to the left end of 1 ie with the low index and inc low by 1 since the 0 has been placed on the right.But if its greater than 1 then we swap it with the high index ie to the right of 1 and dec high since the 2 has already been put to the end.
@amishapurswani99574 жыл бұрын
greatly explained. thanku sir
@sasmitasahoo11614 жыл бұрын
Nicely explained
@Bakepichai7 жыл бұрын
Useful for lot of people. Keep going
@prnk1394 жыл бұрын
very impressive way of explaining algorithm !!
@colettecampbell15283 жыл бұрын
Thank you! I finally understand.
@rajatmishra35724 жыл бұрын
Thank You sir !!
@deepakdas42245 жыл бұрын
Use Counting sorting for this problem that would be helpful with O(1) space complexity
@ThotaMadhuSudhanRao5 жыл бұрын
But in interview they say not to use that approach and do swapping based
@DurgaShiva75743 жыл бұрын
also Vivekanad Sir...as you said that u can make videos as per ur subscribers request ..so please explain the theory then programming/code of Edges and graphs...as every company is asking it..and i am totally blank for it..hoping to hear from u soon
@anshulsaini74402 жыл бұрын
Thankyou sir 🙏
@sholihatulrichas94146 жыл бұрын
Thank you, your videos are very helpful!!! Keep going
@sukritkapil98163 жыл бұрын
nicely explained! thanks
@auritroghoshal84692 жыл бұрын
Thank You Sir.
@neharikakhera64114 жыл бұрын
Thanks for sharing
@adityapaithon64994 жыл бұрын
Good explanation
@sentinel-y8l5 жыл бұрын
When a[mid] == 2, a[high], besides being 0 or 1 can also be 2, which would need to be moved (like the 0 you mentioned). You simply know nothing about a[high] because you never processed that value before, as opposed to a[low] (which was processed before, so it can only be 0 - first iteration, or 1 all other iterations). This is due to you starting mid iterations from left to right.
@Anubis101104 жыл бұрын
Thanks man..clear explanation..
@davidotewa64114 жыл бұрын
Anyone knows how to find the algorithm if we decide to have Four numbers instead of 3. Would we use the above approach
@letstalk86703 жыл бұрын
Sir please make separate playlist for all the topics..
@MrMarkgyuro5 жыл бұрын
King of Algorithm's!
@shailshah90216 жыл бұрын
Counting the number of 0s, 1s, and 2s and then replacing the array is also O(n). However, it is not in-place.
@abhimanyusharma37416 жыл бұрын
It will also take 2 pass to sort the array. Dutch national flag will sort the array in 1 pass.
@gamma_v14 жыл бұрын
It's actually in-place (see the definition of in-place: en.wikipedia.org/wiki/In-place_algorithm). The reason this method is not acceptable for this problem is that by definition of the problem you are only allowed to swap elements (see the definition of Dutch National Flag Problem in Wikipedia).
@shofiyabootwala20943 жыл бұрын
Thank You Sir
@shreyanaik60624 жыл бұрын
Thank you so much sir
@TechieAlpha3 жыл бұрын
Thank you sir 🖤
@farazahmed60436 жыл бұрын
Amazing sir. Keeping making such vids
@deepakdubey5195 жыл бұрын
I wish I had taken your lectures before
@anjurawat92744 жыл бұрын
nice explanation but counting 1 2 3 can also be done in 0(n) so how this one more efficient?
@sumansaurabh81054 жыл бұрын
efficiency here is that it does all that in the single pass.
@anjurawat92744 жыл бұрын
@@sumansaurabh8105 oh u meant we can't calculate 1,2,0 count in one pass?
@sumansaurabh81054 жыл бұрын
You can but then in one pass you will count all the 0,1 and 2s and then you will probably make another array to populate those or go one more pass to populate the original array with 0,1 and 2. The challenge here is you have to do in memory without using extra space and in one pass. Hence this solution. I would recommend you to go too leetcode and read the problem description.
@anjurawat92744 жыл бұрын
@@sumansaurabh8105 satisfied thanks
@nikhilm42904 жыл бұрын
@suman once we count the number of 0's,1's and 2's why do we place in an extra array. We can just override the elements in given array since we know the count of each. I understand it takes O(2n)=>O(n) but as long as the interviewer is happy we can go with count solution. It is always not necessary to give text book solutions and it is a very natural way to think about the count initially even before you propose this textbook solution. I hope you agree with this.
@alexanderpeles817 жыл бұрын
Thank you. Very nice explanation
@coderajput18167 жыл бұрын
very good explanation
@ah62mousavi6 жыл бұрын
nice job, do you have the verification video of this? pre and post condition and loop invariants?
@dewdropk-dl5tv7 жыл бұрын
very nice explanation
@infotainment71234 жыл бұрын
you got new suscriber
@lokeshagarwal62194 жыл бұрын
swap part i m not able to understand like how we can shift from case 0 to case 1,2
@babu1234567817 жыл бұрын
Why didnt we use counting sort for this problem? Counting sort takes O(n) Time complexity. It uses extra space though.
@simranjeetsingh60026 жыл бұрын
In counting sort you need to traverse the array twice (first time for counting and second time for putting) ,so its time complexity is O(2n) but in this one only once you have to traverse so its purely O(n)
@gurmeetchawla83626 жыл бұрын
simran O(2n) is also O(n) only, I am sure you know that, but I am not trying to justify using of count sort for this problem.
@adarshpanigrahi77774 жыл бұрын
Sir can we use sorting alogrithm I would be very easy than any other method
@GurpreetSingh-yy5fn4 жыл бұрын
in the interviews they want us to solve it without sorting, so this algorithm is used.
@mayankaggarwal29375 жыл бұрын
i have an question .. why you don't just sorted the elements using merge sort.. because for worst case also..its complexity is o(nlogn)..and in this video you are getting o(n) complexity.i think merge sort would be a very nycc option .can you please explain..
@ankurshah31125 жыл бұрын
You can do it in O(n)via countSort in 2 pass,but this is trying to do it in one pass. Question has this constraint. If somebody asks you to do it in 1 pass, this is how you would solve it.
@bigray7127 жыл бұрын
amazing
@AdityaKumar-ws5pv7 жыл бұрын
awesome sir ji u r gr8.
@sourovroy79515 жыл бұрын
The Solution is buggy. It will not pass [1,2,0]. Try this one: void sort(vector& nums) { int left=0,mid=0,right=nums.size()-1; while(mid
@SoumikNandi4 жыл бұрын
Godly
@rushi9017 жыл бұрын
Hi, Can you make video for median of two sorted array or median of stream?
@lazy_engineer6 жыл бұрын
very useful thanks
@Ram-kv1xp4 жыл бұрын
@ 5:30 look at the Flicka Da Wrist
@dasabhishek07905 жыл бұрын
Can you show this for 0,1,2,0,1,2 ??? I think this will fail.
@monalisadas90625 жыл бұрын
and even for [1,2,0]
@prajitbanerjee82265 жыл бұрын
nice
@KnowledgeActionWords5 жыл бұрын
thanks
@rahulkhandelwal465787 жыл бұрын
Thank You
@vineetktripathi6 жыл бұрын
Very good explanation, but naming of mid is confusing, Why don't you rename mid to tracker / pointer/currentIndex.
@vaibhavrbs5 жыл бұрын
I was thinking the same, however if you think about it, calling it mid makes more sense as mid_value_index as it stays with 1, similarly with high_value_index as it stays with 2 and same case with low_value_index as it stays with 0 ,
@payalsagar18084 жыл бұрын
thankgod he is there :) saviour
@good114 Жыл бұрын
💞💞❤❤
@sachinbhalla145 жыл бұрын
sir your code is not working can any one provide source code ??
@chaoschao94326 жыл бұрын
Whether this algorithim is in place?
@yatheeshareddy36615 жыл бұрын
no, it is a kind of quicksort algo
@rishabhbisht70145 жыл бұрын
This didn't work for me when passed an array = [1,0,1,0,1,0,1,0,2,0,2,0,2,0,2,0,2,1,0,1,0,1,0]
@arnavrastogi84734 жыл бұрын
Problem : code logic is not correct solution: Add mid
@sanjaysingh50076 жыл бұрын
sir vector pairs , DSU,
@sumanthv20466 жыл бұрын
This wont work for {0,1,2,1,2} because we arent finding the crct values of low,mid and high initially.
@pratikparikh80276 жыл бұрын
It works
@chandangarg13284 жыл бұрын
but counting will also take O(n)
@kansiram53407 жыл бұрын
sir i need your github link
@abhishektiwari59323 жыл бұрын
print(arr.sort())
@SIG4426 жыл бұрын
Yet your own flag, India I take it, is based up on the Dutch flag like so many other nations. That would mean that your own flag has a problem as well ;) Aside from that, I tried to follow what you were saying. But I have no clue still what you are trying to tell with this. Please do explain. I can't say I had any useful education at school, along side that I was the target for pretty much everyone. So learning wasn't really my primary thing to do in my early years. Yet I seem to be smart enough to follow very advanced things in many cases, with the right explanation. I like to learn and understand more.
@tusharbhart41054 жыл бұрын
Just sort the array in ascending order......that's it
@Matkartik-yz4vs5 жыл бұрын
very slow...
@stholy325 жыл бұрын
The only question i have is....... Where on earth will you ever use this.....???!!
@spetsnaz_25 жыл бұрын
The place where you got to know this algo!
@sachinbhalla145 жыл бұрын
sir your code is not working can any one provide source code ??