Sort on second key when, first key in a set is sorted

Assume that we are given as input n pairs of items, where
  1. the first item is a number
  2. the second item is one of three colors (red, blue, or yellow).
Further, assume that the items are sorted by number. Give an O(n) algorithm to sort the items 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).

0 comments:

Post a Comment

 
Moviehole-madhurtanwani © 2010 | Designed by Chica Blogger | Back to top