Monday, 9 September 2013

How to "sort" elements of 2 possible values in place in linear time?

How to "sort" elements of 2 possible values in place in linear time?

Suppose I have a function f and array of elements.
The function returns A or B for any element; you could visualize the
elements this way ABBAABABAA.
I need to sort the elements according to the function, so the result is:
AAAAAABBBB
The number of A values doesn't have to equal the number of B values. The
total number of elements can be arbitrary (not fixed). Note that you don't
sort chars, you sort objects that have a single char representation.
Two more things:
the sort should take linear time - O(n),
it should be performed in place,
it should be a stable sort.
Any ideas?

No comments:

Post a Comment