Dsa
Sorting
Warm-up
Sort Colors
Write a program that reads a list of colors represented as integers from standard input and outputs them in sorted order. The colors are represented by the integers 0 (Red), 1 (White), and 2 (Blue). The program should not use any sorting library but should implement a counting sort approach.
Input format:
- The first line contains an integer n (1 ≤ n ≤ 100). The second line contains n integers separated by spaces, each integer being 0, 1, or 2.
Output format:
- A single line containing the sorted colors as integers separated by spaces.
Example:
Input:
5
0 1 2 0 1
Output:
0 0 1 1 2
Key concepts
sortingarraycounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now