Author |
Topic: Set of Three Sort (Read 1587 times) |
|
Sir Col
Uberpuzzler
impudens simia et macrologus profundus fabulae
Gender:
Posts: 1825
|
|
Set of Three Sort
« on: Feb 2nd, 2005, 1:31pm » |
Quote Modify
|
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
|
|
IP Logged |
mathschallenge.net / projecteuler.net
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Set of Three Sort
« Reply #1 on: Feb 2nd, 2005, 2:32pm » |
Quote Modify
|
:: 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; } ::
|
« Last Edit: Feb 2nd, 2005, 2:40pm by towr » |
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
igni_ferroque
Newbie
Posts: 11
|
|
Re: Set of Three Sort
« Reply #2 on: Feb 4th, 2005, 10:13pm » |
Quote Modify
|
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)
|
|
IP Logged |
|
|
|
|