Compute median of up to 5 in Scala
问题 So, while answering some other question I stumbled upon the necessity of computing the median of 5. Now, there's a similar question in another language, but I want a Scala algorithm for it, and I'm not sure I'm happy with mine. 回答1: Here's an immutable Scala version that has the minimum number of compares (6) and doesn't look too ugly: def med5(five: (Int,Int,Int,Int,Int)) = { // Return a sorted tuple (one compare) def order(a: Int, b: Int) = if (a<b) (a,b) else (b,a) // Given two self-sorted