Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maxima & minima of an array [closed]

Tags:

arrays

c

An array of n numbers is given, where n is an even number. The maximum as well as the minimum of these n numbers needs to be determined.I need to know the comparisions needed?

like image 546
user2170497 Avatar asked Mar 14 '13 15:03

user2170497


1 Answers

It can be done using 3*n/2-2 comparisons.

For n == 2, simply compare the two numbers. Now suppose we have minimum and maximum for the first n-2 numbers. Compare the remaining two numbers, then compare the bigger one to the previous maximum and the smaller to the previous minimum.

like image 119
Henrik Avatar answered Oct 14 '22 13:10

Henrik