Sunday, May 4, 2014

Maximum and minimum of an array using minimum number of comparisons

Problem:
Find maximum and minimum of an array using minimum number of comparisons.

Solution:
http://www.geeksforgeeks.org/maximum-and-minimum-in-an-array/

1. Linear: 1 + 2*(n - 2)
2. Divide and conqure: 1.5*n - 2 (if n is a power of 2, otherwise more comparisions are required)
3. Compare in pairs: also ~1.5*n.


Complexity:
time - O(n)
space - O(1) or O(log n) (for method #2)
Links and credits:
http://www.geeksforgeeks.org/maximum-and-minimum-in-an-array/

No comments:

Post a Comment