MHB How many comparisons are required?

  • Thread starter Thread starter mathmari
  • Start date Start date
Click For Summary
To find the maximum element of a set of integers S using only addition and comparisons, the initial approach suggested performing n-1 comparisons by comparing each element to a running maximum. However, this method is not the most efficient. An optimized divide and conquer strategy can reduce the number of comparisons to log2(n). By splitting the set into two halves, finding the maximum in each half, and then comparing these two maximums, the process becomes more efficient. Thus, while the initial method is valid, the optimized approach significantly minimizes the number of comparisons required to identify the maximum element in the set.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

Let $S$ be a set of $n$ integers. Assume you can perform only addition of elements of $S$ and comparisons between sums. Under these conditions how many comparisons are required to find the maximum element of $S$?

I thought that we could find the maximum element as followed:

Code:
max=S(1)+S(1)
k=1
for j=2 to n
     sum=S(1)+S(j)
     if sum>max
           max=sum
           k=j
return S(k)

That means that $n-1$ comparisons are required.

Is this correct?? (Wondering)
 
Technology news on Phys.org


Hello! Your approach to finding the maximum element of $S$ using comparisons and addition is correct. However, the number of comparisons needed may not always be $n-1$. Let me explain why.

In your algorithm, you are comparing each element of $S$ to the current maximum, which is initially set to $S(1)+S(1)$. This means that for each element $S(j)$, you are performing one comparison ($S(1)+S(j)$ vs $max$). Therefore, for $n$ elements in $S$, you would need $n$ comparisons.

However, we can optimize this algorithm by using a divide and conquer approach. We can divide the set $S$ into two halves and find the maximum element in each half. Then, we can compare the maximum elements of the two halves and return the larger one as the maximum element of $S$. This approach would require only $\log_2{n}$ comparisons, which is significantly fewer than $n-1$ comparisons.

In conclusion, your approach is correct but may not always give the minimum number of comparisons needed. The optimized approach would require only $\log_2{n}$ comparisons. I hope this helps!
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
9K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
9
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
Replies
25
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K