Author |
Topic: Arrange n balls in an order (Read 2659 times) |
|
digvesh
Newbie
Gender:
Posts: 1
|
|
Arrange n balls in an order
« on: Mar 16th, 2009, 7:44am » |
Quote Modify
|
If you have n balls in an array, some of which are red, some are green and some are blue. Write an algorithm to arrange those n balls in such a way that all the balls with same color are together. i.e. first all red balls, then all green balls, then all blue balls. Assume you have two predefined methods available: 1. GetColor( i ) : returns the color of the ball at index i in the array. 2. Swap( i, j ) : swaps the balls at indexs i and j in the array.
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Arrange n balls in an order
« Reply #1 on: Mar 16th, 2009, 7:59am » |
Quote Modify
|
Here's one of the previous threads on the subject. (And there should be at least two others, somewhere) [edit] one, two, three [/edit]
|
« Last Edit: Mar 16th, 2009, 8:09am by towr » |
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
|