Reason behind mid++?

Why mid++,low++ if(a[mid] == 0)
but no mid++, only high-- if(a[mid] == 2) ?

I see we don’t do mid++ if a[mid] == 2, because what if even after swap(a[mid], a[high]) a[mid] is 2.
Then, why do mid++ along with low++ if(a[mid == 0) ?
I guess it’s because it never happens that even after swap(a[mid], a[low]) a[mid] is 0. But, how can we be sure that this actually never happens?

Yup correct

Again correct but a[mid] will be 1 or 0 depending on case
How we make sure? We are traversing array from left to right so we know its already sorted as 000111… Something
And low will be at last 0 or first 1(depending on implementation )
So that how we make sure

1 Like

@Kartikkhariwal1 Alright! Got it. Thanks for the precise explanation.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.