Click here to Skip to main content
15,888,195 members
Please Sign up or sign in to vote.
1.67/5 (5 votes)
See more:
If you are given an array of balls,the array contains ball in three colors:Red,green and blue,the balls are randomly distributed in the array and are not sorted.
The number of balls of each color is unknown and is not equal to the number of balls of any of the different colors.

you are asked to sort the array so as all the balls of one color are lumped together .
the order sorting is indifferent.
For Example,after sorting you could have all the blue balls at the beginning of the array and then followed by all the red balls and finally followed by all the green balls.

Write a programe in pseudo code(or any) .you are to provide an optimized solution that minimizes
the excution time and the resources to be used.
Posted
Comments
stib_markc 26-May-12 7:47am    
If you are asked to write pseudo code, why so many tags? And also here assistance is provided if your facing a problem, better show what you have done or tried to do for this assignment.
Read this before posting a Question: [^]
Sandeep Mewara 26-May-12 13:22pm    
It does not work like this here.

Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it's not that hard.
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
 
Share this answer
 
Comments
eng.Fadialia 26-May-12 7:57am    
It's not a homework ,it is HR's question,i couldn't solve it.
stib_markc 26-May-12 8:15am    
Then please provide, what you have tried for solving it. We may be able to point where you were wrong, and you can rectify that and proceed in the right direction.
Keith Barrow 27-May-12 9:38am    
So it's a question for a job interview?
eng.Fadialia 26-May-12 8:32am    
I tried to solve it at entrance exam for a company,so I want to know what is the algorithm or pseudo code of this problem?
Emilio Garavaglia 26-May-12 9:18am    
You must tell us WHAT you have tried, otherwise we cannot help you.
There is no reason we can hep someone entering a company without the required knowledge: your future colleague cannot solve all the problems you are in charge of.
I suppose that:
Blue=1 , Green=2, Red=3
pseudo code by Bubble sort:

procedure bubbleSort( A : list of sortable items )
repeat
swapped = false
for i = 1 to length(A) - 1 inclusive do:
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
 
Share this answer
 
Comments
nv3 27-May-12 6:54am    
And why do you think that a bubble sort is an "optimized solution" to the problem?
Keith Barrow 28-May-12 10:39am    
A bubble sort is never the most optimised solution to sort, even allowing for the fact that optimisation depends on the type of data you start off with.
eng.Fadialia 29-May-12 7:29am    
Thank you all, but what is the best algorithm to this case?
Keith Barrow 29-May-12 9:26am    
Well, the original question in the interview was bad. The most optimal sort depends on a few factors otherwise we'd all know and use the one "optimised" algorithm. Factors include how well ordered the list is, the size of the list in relation to the memory you have to play with. You may have even more esoteric requirements, like needing to know how long a sort is going to take for a real time system for example.
The standard answer when there are no given requirements is Quicksort, it is generally considered fastest *on average*.
My comment was prompted by the fact that bubblesort is never the most optimised, it can always be replaced by a cocktail sort which is faster on average than bubble sort, especially if the algorithm keeps track of where the last swap happened reducing the number of comparisons needed. It does this without requiring more memory, but is still slow compared to quicksort.
You should look up "Sorting Alogrithm" in wikipedia, it's very instructive

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900