wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> microsoft >> Arrange n balls in an order
(Message started by: digvesh on Mar 16th, 2009, 7:44am)

Title: Arrange n balls in an order
Post by digvesh on Mar 16th, 2009, 7:44am
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.

Title: Re: Arrange n balls in an order
Post by towr on Mar 16th, 2009, 7:59am
Here (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1100111371)'s one of the previous threads on the subject. (And there should be at least two others, somewhere)


[edit]
one (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1182484564;), two (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1197342769), three (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1231090929;)
[/edit]



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board