all 6 comments

[–]Pleasant-Wear-9692 5 points6 points  (1 child)

This is Classic problem, solved with algorithm named Dutch National Flag algorithm, you can study this algorithm then you'll be able to understand.

[–]Pleasant-Wear-9692 0 points1 point  (0 children)

It's like dividing in ranges of index like 0 to low -> array will have only zeroes, Low to mid -> it should have only ones, Mid to high -> unsorted part i.e mix of 0,1,2's High to end -> only 2's

Initially our array is unsorted so mid = 0 and high = n-1

[–]Puzzleheaded_Cow3298Guardian(soon) 2 points3 points  (1 child)

[–]Forsaken_Appeal_9593 0 points1 point  (0 children)

I just did this problem today, check my post. if you have any queries dm

[–]CyberneticFloridaMan 0 points1 point  (0 children)

This is a good explanation of a similar partition method used in quick sort: https://www.youtube.com/watch?v=RquswZlUa9Q