wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> Set of Three Sort
(Message started by: Sir Col on Feb 2nd, 2005, 1:31pm)

Title: Set of Three Sort
Post by Sir Col on Feb 2nd, 2005, 1:31pm
S is a set of unsorted values, {a,b,c}, not necessarily distinct.
Let x=minimum value, y=median value, and z=maximum value.

Develop "efficient" algorithms to find,
(i) x
(ii) y
(iii) x and z
(iv) x and y
(v) x, y, and z

Title: Re: Set of Three Sort
Post by towr on Feb 2nd, 2005, 2:32pm
::[hide]
In the case less is asked for, simply trim the branches to accommodate.

if (a <= b)
{
 if (a <= c)
   x=a;
 else /* a > c */
   x=c,y=a;

 if (b <= c)
   z=c,y=b;
 else /* b > c */
   z=b;
}
else /* a > b */
{
 if (b <= c)
   x=b;
 else /* b > c */
   x=c,y=b;

 if (a <= c)
   z=c,y=a;
 else /* a > c */
   z=a;
}
[/hide]::

Title: Re: Set of Three Sort
Post by igni_ferroque on Feb 4th, 2005, 10:13pm
[hide]def f(S):
     a, b, c = S
     x, y, z = S
     if a < b:
           x = a
           y = b
     else:
           y = a
           x = b
     if y > c:
           z = y
           y = c
     if c < x:
           y = x
           x = c
     return (x,y,z)      
[/hide]



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