Algorithm: findMedian(a, b, c, d, e)
step 1: sort(a, b, c, d) {sort the first four numbers using the sort module in 5 comparisons}
step 2:
if e < b then median <- b
else if e > c then median <- c
else median <- e
return median
Total comparisons to sort the first four numbers = 5
Total comparisons to locate median = 2
Therefore total comparisons to find median of four numbers = 5 + 2 = 7
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment