// efg, April 1998 FUNCTION MedianInteger (x: ARRAY OF INTEGER): INTEGER; VAR i : INTEGER; j : INTEGER; Middle : INTEGER; Temporary: INTEGER; BEGIN // Use truncated selection sort to find median Middle := (High(x)+1) DIV 2; FOR i := 0 TO Middle DO BEGIN FOR j := 1 TO High(x)-i DO BEGIN IF x[j] > x[j-1] THEN BEGIN Temporary := x[j]; x[j] := x[j-1]; x[j-1] := Temporary END END END; IF Odd(High(x)) // range is 0 to High(x) ==> High(x) + 1 elements THEN BEGIN // When High(x) is Odd, there are an even number of elements in array. // Define median as average of two middle values. RESULT := (x[middle] + x[middle-1]) DIV 2 END ELSE BEGIN // When High(x) is Even, there are an odd number of elements in array. // Median is the middle value. RESULT := x[middle] END END {MedianInteger};