Table of Contents: Finding A Room...AGAIN 0:00 - 0:26 Introduction To The Problem 0:26 - 1:22 An Example Of The Partitioning 1:22 - 3:59 Some Approaches To The Problem 3:59 - 7:16 Optimal Solution Walkthrough (Forward) 7:16 - 8:36 Optimal Solution Walkthrough (Backward) 8:36 - 9:44 The Final Solution 9:44 - 10:26 The Wrap Up 10:26 - 10:35 This problem has implications in the improvement of quicksort's worst case O(n^2) runtime cases caused by repeated elements (quicksort has poor performance for inputs that have many repeated elements).
@ShubhamSinghYoutube4 жыл бұрын
learning quick sort from A-train , made my day! lol
@BackToBackSWE4 жыл бұрын
ye
@clipzone83024 жыл бұрын
Lol A-train's teaching algorithms after retirement from vought
@felixongati18403 жыл бұрын
One trivial but important optimization, if the position of the current item is on the left of the pivot position, do not swap. i.e. if current_item < pivot and index_current_item < index_pivot, do not swap.
@PiIsRational4 жыл бұрын
I thought I could figure this out because I am comfortable with the partitioning algorithm in the context of quick sort, but I could not. Thanks for the great explanation. Keep up the great work. Learning a ton on this chan.
@BackToBackSWE4 жыл бұрын
sure
@zarlorin3728 Жыл бұрын
this guy is more motivated than my prof in any ways
@abhinavgupta14924 жыл бұрын
This cleared my concept, i was stuck at a quick sort problem. THANKS MAN!!
@BackToBackSWE4 жыл бұрын
sure
@FrancisGabon3 жыл бұрын
Really like the tone and style of that channel and his presenter! Thanks a lot, great content.
@ashwin-g-17015 жыл бұрын
There is a better way... You do not need to have a separate forward and backwards pass.... This is how its gonna work: You store the indexes to which we have to take the lower than pivot values to, i.e the green pointer in the end of the video, and also another value that stores the index after which, are the greater than pivot values, i.e the red pointer. Let me just call them green and red indexes for now.... -- Now, when you encounter a value < pivot, exchange with green index, increment green index and go on to check the next value. -- When you encounter a value = pivot, ignore it and move to the next value. -- When you encounter a value > pivot, swap it with red index, and move red index to left, but check the value at that position again with these 3 points(the one which you had swapped for the > pivot value, ie the one that was previously at the red index) This might not make sense at first, but the way it works is that all values between green pointer and the one we are going to check, have been checked before, to be equal to pivot. Just try this for the example in the video... Its a pretty small improvement to reduce array accesses to exactly the size of the array, but its an improvement nonetheless. Sorry if my explanation was too confusing :)
@BackToBackSWE5 жыл бұрын
nah u good
@sachitmohannayak49645 жыл бұрын
Wow, this helped me learn the algorithm so quick; the colours are very helpful too.
@BackToBackSWE5 жыл бұрын
hahaha nice, glad it did
@kumarshubham92174 жыл бұрын
really appreciate your enthusiasm for teaching..you are a great and honest teacher...you put a lot of energy man! keep it up!
@BackToBackSWE4 жыл бұрын
thanks ok
@69shrutivichare603 жыл бұрын
Thank you so much This is the only HELPFUL video on 3 way quick sort Please make such more videos 🙏
@jeezradz4 жыл бұрын
The best video on dutch national flag on youtube so far! Just subscribed!
@BackToBackSWE4 жыл бұрын
thanks and welcome
@ritikkhatri3 жыл бұрын
thank you, i couldn't grasp 3 partition before watching.
@BackToBackSWE3 жыл бұрын
Glad I could help!
@colemanhoyt54374 жыл бұрын
The example at the end works so well! Thank you for the explanation!
@aribakodes54534 жыл бұрын
you are a truly amazing teacher
@BackToBackSWE4 жыл бұрын
thanks
@khadijakhaldi64685 жыл бұрын
Thank you so much for your help! why don't we use the partition step in the quicksort ?
@BackToBackSWE5 жыл бұрын
I made this a while back, don't remember the problem 😳
@khadijakhaldi64685 жыл бұрын
haha it’s ok 😁
@LalitYadav-vi1ul4 жыл бұрын
I think the forward and backward scan and replace is Bentley-McIlory 3-way partitioning. But, nice explanation :)
@BackToBackSWE4 жыл бұрын
ok
@OmniscientPotato5 жыл бұрын
Thank you for this excellent explanation!
@BackToBackSWE5 жыл бұрын
thansk
@deepaktiwari28474 жыл бұрын
You were just FANTASTIC !!
@BackToBackSWE4 жыл бұрын
thanks
@vaibhavsingh755 Жыл бұрын
Great Explanation Sir😊
@prathameshpatil60404 жыл бұрын
Can we not do this in one pass? just swap with back or front pointers as per index value ..
@BackToBackSWE4 жыл бұрын
Maybe, I dont remember
@AnantChowdhary4 жыл бұрын
Yup, we can: public void sortColors(int[] nums) { int filledUpTillFromStart = -1; int filledUpTillFromEnd = nums.length; int i = 0; while(i < nums.length && i < filledUpTillFromEnd) { if(nums[i] == 0) { filledUpTillFromStart++; swap(i, filledUpTillFromStart, nums); if(i == filledUpTillFromStart) i++; } else if(nums[i] == 2) { filledUpTillFromEnd--; swap(i, filledUpTillFromEnd, nums); } else { i++; } } } public void swap(int i, int j, int[] arr) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }
@ananyapamde45143 жыл бұрын
Really like your videos
@PankajKumar-hz3oi3 жыл бұрын
thanks, brother, great explanation
@gamingblast23253 жыл бұрын
In second way i dont get why its O(n^2) , is the approach is to select every single element and look to element less than it ?
@snlagr4 жыл бұрын
every superhero has a basement
@BackToBackSWE4 жыл бұрын
tru
@yipg20764 жыл бұрын
I think the way you explained from 7:27 is not appropriate in the case like pivot number is 2 (Blue)
@BackToBackSWE4 жыл бұрын
I dont remember this video too much
@yipg20764 жыл бұрын
@@BackToBackSWE k, don't mind. All your videos are very helpful for me to understand the problem well. Best Regards ;)
@Ghungroo-Beats3 жыл бұрын
Great Explanation!
@vitna915 жыл бұрын
How do yoi choose the pivot? The value at pivot index should always be 1?
@BackToBackSWE5 жыл бұрын
can u timestamp? I made this a while back
@vitna915 жыл бұрын
7:16
@kumarc48533 жыл бұрын
thank you
@anveshreddy12453 жыл бұрын
Can we apply this algorithm if the arrays number are not only 0 1 2,I mean more unique numbers like array of 1,3,5,6
@okeuwechue92386 ай бұрын
you can replace single-digit values with a RANGE of values i.e. each single digit transforms to a SET of values
@chengjiang70515 жыл бұрын
Amazing solution
@BackToBackSWE5 жыл бұрын
thx
@jacobzhang8404 жыл бұрын
Great explanation!
@BackToBackSWE4 жыл бұрын
thanks
@YavinArba5 жыл бұрын
informative and engaging, thanks man
@BackToBackSWE5 жыл бұрын
sure
@nackyding5 жыл бұрын
Is the second example the bubble sort algorithm?
@BackToBackSWE5 жыл бұрын
can you timestamp it?
@pawehoowicki79404 жыл бұрын
very nice!!
@BackToBackSWE4 жыл бұрын
thx
@xVermyy5 жыл бұрын
Bro, great explanation!
@BackToBackSWE5 жыл бұрын
Thank you!
@akashsrivastava83243 жыл бұрын
Awesome !!!
@sidhant46745 жыл бұрын
Great work man!
@BackToBackSWE4 жыл бұрын
thanks
@doggo6605 жыл бұрын
wow good job! Thank you.
@BackToBackSWE5 жыл бұрын
thanks
@doggo6605 жыл бұрын
@@BackToBackSWE I just have one question. Why is the first approach O(n) space complexity if we're just using 3 arrays? Doesn't memory have to grow by n based on input? In that case it's O(1) I think. Space complexity is pretty confusing and not easy to find on the internet.
@BackToBackSWE5 жыл бұрын
Each array cannot be larger than n. We bound to O(n) asymptotically because the space we use cannot exceed that linear nature on the tail end of a graph of space usage. For time...we are just doing linear time passes. No nested operations that will tie one variable to another. Is this clear? Continue asking questions.
@doggo6605 жыл бұрын
@@BackToBackSWE Ohh ok I think I understand now. So it's O(n) space complexity since n is the size of the array. Therefore, the space is dependent on the size of what array that has been passed in to the function. So really its O(n) auxiliary space since an array by default has an O(n) space complexity.
@BackToBackSWE5 жыл бұрын
@@doggo660 yeah.
@noa.leshem Жыл бұрын
i just failed this question in a job interview 🤡 edit: nvm, bombed the question passed the interview
@BackToBackSWE Жыл бұрын
Congratulations on passing the interview 🎉
@hawksawed3 жыл бұрын
4:45 if anyone is trying to see the 3 solutions
@spk94345 жыл бұрын
Awesome !
@BackToBackSWE5 жыл бұрын
love.
@AffairWithGeo Жыл бұрын
Am i late ?? in learning from you?
@ayusharora20193 жыл бұрын
Hey !! Can you please post the code for the same? in Java If possible
@StanOliver10005 жыл бұрын
What is so unusual about being in a basement??????
@BackToBackSWE5 жыл бұрын
nothing
@StanOliver10005 жыл бұрын
@@BackToBackSWE Exactly
@arowa.5 жыл бұрын
i literally thought that he will start by saying , hey yo my man's here rofl