Given an array containing only the values 0, 1 and 2, sort it in non-decreasing order (the "Dutch national flag" problem).
Input
Line 1: an integer n.
Line 2: n integers, each 0, 1 or 2.
Output
The sorted array, space-separated.
Given an array containing only the values 0, 1 and 2, sort it in non-decreasing order (the "Dutch national flag" problem).
Line 1: an integer n.
Line 2: n integers, each 0, 1 or 2.
The sorted array, space-separated.
Example 1
6 2 0 2 1 1 0
0 0 1 1 2 2
Two 0s, two 1s, two 2s.
Example 2
3 2 1 0
0 1 2
0 1 2.
Example 3
1 1
1
A single value.
You could sort, but with only three values you can just count them.
Output that many 0s, then 1s, then 2s.
Count the 0s, 1s and 2s and emit them in order (a three-way partition also works in one pass).
import sys
d = list(map(int, sys.stdin.read().split()))
n = d[0]
a = d[1:1 + n]
print(' '.join(map(str, sorted(a))))
Tell us what needs to change, who it affects and any important deadline. We will review the context and reply with useful next questions.