How many comparisons are required?

  • Context: MHB 
  • Thread starter Thread starter mathmari
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on determining the number of comparisons required to find the maximum element in a set of integers, denoted as $S$. The initial approach suggested that $n-1$ comparisons are needed, but it was clarified that the correct number of comparisons is actually $n$ when using the method of comparing each element to the current maximum. An optimized divide and conquer algorithm can reduce the number of comparisons to $\log_2{n}$, significantly improving efficiency.

PREREQUISITES
  • Understanding of basic algorithms and data structures
  • Familiarity with the concept of maximum element in a set
  • Knowledge of divide and conquer strategies
  • Basic mathematical understanding of logarithmic functions
NEXT STEPS
  • Research the divide and conquer algorithm for finding maximum values
  • Learn about the time complexity of algorithms, focusing on $\log_2{n}$ comparisons
  • Explore optimization techniques in algorithm design
  • Study other algorithms that utilize comparisons, such as quicksort and mergesort
USEFUL FOR

Computer scientists, algorithm designers, and students studying data structures who are interested in optimizing search algorithms and understanding comparison-based methods.

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!
 

Similar threads

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