Sort Array of 0s, 1s and 2s (Dutch National Flag)

M · mediumPracticearraystwo-pointer

Sort an array containing only 0s, 1s and 2s in a single traversal without extra space.

Input
6
0 1 2 1 0 2
Output
0 0 1 1 2 2
Three pointers: low=0, mid=0, high=n-1. Swap based on value at arr[mid].
← Sieve of Eratosthenes — All Primes up to NSplit Array with Equal Averages →
Report an issue with this question