Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Assume that we are given n balls as input, where the ball has a number and is one of three colors (red, blue, or yellow).
Assume that we are given n balls as input, where the ball has a number and is one of three colors (red, blue, or yellow). Further assume that the balls are sorted by their number. Give an O(n) algorithm to sort the balls by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started