The Dutch National Flag Problem (The Quicksort "Band-Aid")

  Рет қаралды 30,973

Back To Back SWE

Back To Back SWE

Күн бұрын

Пікірлер: 89
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
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).
@ShubhamSinghYoutube
@ShubhamSinghYoutube 4 жыл бұрын
learning quick sort from A-train , made my day! lol
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
ye
@clipzone8302
@clipzone8302 4 жыл бұрын
Lol A-train's teaching algorithms after retirement from vought
@felixongati1840
@felixongati1840 3 жыл бұрын
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.
@PiIsRational
@PiIsRational 4 жыл бұрын
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.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@zarlorin3728
@zarlorin3728 Жыл бұрын
this guy is more motivated than my prof in any ways
@abhinavgupta1492
@abhinavgupta1492 4 жыл бұрын
This cleared my concept, i was stuck at a quick sort problem. THANKS MAN!!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@FrancisGabon
@FrancisGabon 3 жыл бұрын
Really like the tone and style of that channel and his presenter! Thanks a lot, great content.
@ashwin-g-1701
@ashwin-g-1701 5 жыл бұрын
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 :)
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
nah u good
@sachitmohannayak4964
@sachitmohannayak4964 5 жыл бұрын
Wow, this helped me learn the algorithm so quick; the colours are very helpful too.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
hahaha nice, glad it did
@kumarshubham9217
@kumarshubham9217 4 жыл бұрын
really appreciate your enthusiasm for teaching..you are a great and honest teacher...you put a lot of energy man! keep it up!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thanks ok
@69shrutivichare60
@69shrutivichare60 3 жыл бұрын
Thank you so much This is the only HELPFUL video on 3 way quick sort Please make such more videos 🙏
@jeezradz
@jeezradz 4 жыл бұрын
The best video on dutch national flag on youtube so far! Just subscribed!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thanks and welcome
@ritikkhatri
@ritikkhatri 3 жыл бұрын
thank you, i couldn't grasp 3 partition before watching.
@BackToBackSWE
@BackToBackSWE 3 жыл бұрын
Glad I could help!
@colemanhoyt5437
@colemanhoyt5437 4 жыл бұрын
The example at the end works so well! Thank you for the explanation!
@aribakodes5453
@aribakodes5453 4 жыл бұрын
you are a truly amazing teacher
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thanks
@khadijakhaldi6468
@khadijakhaldi6468 5 жыл бұрын
Thank you so much for your help! why don't we use the partition step in the quicksort ?
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
I made this a while back, don't remember the problem 😳
@khadijakhaldi6468
@khadijakhaldi6468 5 жыл бұрын
haha it’s ok 😁
@LalitYadav-vi1ul
@LalitYadav-vi1ul 4 жыл бұрын
I think the forward and backward scan and replace is Bentley-McIlory 3-way partitioning. But, nice explanation :)
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
ok
@OmniscientPotato
@OmniscientPotato 5 жыл бұрын
Thank you for this excellent explanation!
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
thansk
@deepaktiwari2847
@deepaktiwari2847 4 жыл бұрын
You were just FANTASTIC !!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thanks
@vaibhavsingh755
@vaibhavsingh755 Жыл бұрын
Great Explanation Sir😊
@prathameshpatil6040
@prathameshpatil6040 4 жыл бұрын
Can we not do this in one pass? just swap with back or front pointers as per index value ..
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
Maybe, I dont remember
@AnantChowdhary
@AnantChowdhary 4 жыл бұрын
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; }
@ananyapamde4514
@ananyapamde4514 3 жыл бұрын
Really like your videos
@PankajKumar-hz3oi
@PankajKumar-hz3oi 3 жыл бұрын
thanks, brother, great explanation
@gamingblast2325
@gamingblast2325 3 жыл бұрын
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 ?
@snlagr
@snlagr 4 жыл бұрын
every superhero has a basement
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
tru
@yipg2076
@yipg2076 4 жыл бұрын
I think the way you explained from 7:27 is not appropriate in the case like pivot number is 2 (Blue)
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
I dont remember this video too much
@yipg2076
@yipg2076 4 жыл бұрын
@@BackToBackSWE k, don't mind. All your videos are very helpful for me to understand the problem well. Best Regards ;)
@Ghungroo-Beats
@Ghungroo-Beats 3 жыл бұрын
Great Explanation!
@vitna91
@vitna91 5 жыл бұрын
How do yoi choose the pivot? The value at pivot index should always be 1?
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
can u timestamp? I made this a while back
@vitna91
@vitna91 5 жыл бұрын
7:16
@kumarc4853
@kumarc4853 3 жыл бұрын
thank you
@anveshreddy1245
@anveshreddy1245 3 жыл бұрын
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
@okeuwechue9238
@okeuwechue9238 6 ай бұрын
you can replace single-digit values with a RANGE of values i.e. each single digit transforms to a SET of values
@chengjiang7051
@chengjiang7051 5 жыл бұрын
Amazing solution
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
thx
@jacobzhang840
@jacobzhang840 4 жыл бұрын
Great explanation!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thanks
@YavinArba
@YavinArba 5 жыл бұрын
informative and engaging, thanks man
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
sure
@nackyding
@nackyding 5 жыл бұрын
Is the second example the bubble sort algorithm?
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
can you timestamp it?
@pawehoowicki7940
@pawehoowicki7940 4 жыл бұрын
very nice!!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thx
@xVermyy
@xVermyy 5 жыл бұрын
Bro, great explanation!
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
Thank you!
@akashsrivastava8324
@akashsrivastava8324 3 жыл бұрын
Awesome !!!
@sidhant4674
@sidhant4674 5 жыл бұрын
Great work man!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thanks
@doggo660
@doggo660 5 жыл бұрын
wow good job! Thank you.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
thanks
@doggo660
@doggo660 5 жыл бұрын
@@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.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
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.
@doggo660
@doggo660 5 жыл бұрын
@@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.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
@@doggo660 yeah.
@noa.leshem
@noa.leshem Жыл бұрын
i just failed this question in a job interview 🤡 edit: nvm, bombed the question passed the interview
@BackToBackSWE
@BackToBackSWE Жыл бұрын
Congratulations on passing the interview 🎉
@hawksawed
@hawksawed 3 жыл бұрын
4:45 if anyone is trying to see the 3 solutions
@spk9434
@spk9434 5 жыл бұрын
Awesome !
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
love.
@AffairWithGeo
@AffairWithGeo Жыл бұрын
Am i late ?? in learning from you?
@ayusharora2019
@ayusharora2019 3 жыл бұрын
Hey !! Can you please post the code for the same? in Java If possible
@StanOliver1000
@StanOliver1000 5 жыл бұрын
What is so unusual about being in a basement??????
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
nothing
@StanOliver1000
@StanOliver1000 5 жыл бұрын
@@BackToBackSWE Exactly
@arowa.
@arowa. 5 жыл бұрын
i literally thought that he will start by saying , hey yo my man's here rofl
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
what
@anuragv400
@anuragv400 3 жыл бұрын
Should be Ez for Dutch peoples..
@Dyslexic_Neuron
@Dyslexic_Neuron 5 жыл бұрын
finally !!!
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
hi
The N Queens Problem using Backtracking/Recursion - Explained
14:29
Back To Back SWE
Рет қаралды 137 М.
SISTER EXPOSED MY MAGIC @Whoispelagheya
00:45
MasomkaMagic
Рет қаралды 13 МЛН
Kluster Duo #настольныеигры #boardgames #игры #games #настолки #настольные_игры
00:47
버블티로 부자 구별하는법4
00:11
진영민yeongmin
Рет қаралды 23 МЛН
The Quicksort Sorting Algorithm: Pick A Pivot, Partition, & Recurse
26:31
Back To Back SWE
Рет қаралды 165 М.
Anxiety Wars: Existentialism vs Absurdism
14:10
mystiverse
Рет қаралды 386
The 0/1 Knapsack Problem (Demystifying Dynamic Programming)
20:30
Back To Back SWE
Рет қаралды 206 М.
SISTER EXPOSED MY MAGIC @Whoispelagheya
00:45
MasomkaMagic
Рет қаралды 13 МЛН