Monday, February 9, 2009

Computing The Median of An Array

Pseudocode for computing the median of an array A[0, 1, 2, ..., n-1]

01 SelectMedian(A[0, 1, 2, ..., n-1])
02 BubbleSort(A) or SelectionSort(A)
03 if n is odd then
04 r <- A[(n-1)/2]
05 else
06 r <- (A[n/2] + A[(n/2)-1)])/2
07 end if
08 return r

No comments:

Post a Comment