How many comparisons are required?

  • MHB
  • Thread starter mathmari
  • Start date
In summary, the number of comparisons needed to find the maximum element of $S$ using comparisons and addition can be optimized to $\log_2{n}$ by using a divide and conquer approach.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

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
  • #2


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!
 

Related to How many comparisons are required?

1. How many comparisons are required in a scientific experiment?

In a scientific experiment, the number of comparisons required can vary depending on the specific research question, design of the experiment, and the statistical analysis being used. Generally, a minimum of 30 comparisons is recommended for reliable results.

2. Why is it important to know how many comparisons are required?

Knowing the required number of comparisons is important because it helps researchers to determine the appropriate sample size and statistical power of their experiment. It also ensures that the results are statistically significant and reliable.

3. How can I determine the required number of comparisons for my experiment?

The required number of comparisons can be determined through power analysis, which takes into account the research question, experimental design, and statistical analysis. There are also online calculators and software programs available to assist with this calculation.

4. Can the required number of comparisons change during the course of an experiment?

Yes, the required number of comparisons can change if there are unexpected results or if the research question or design of the experiment is altered during the course of the study. It is important to reassess and adjust the number of comparisons as needed.

5. Are there any consequences if the required number of comparisons is not met?

If the required number of comparisons is not met, the results of the experiment may not be statistically significant or reliable. This can lead to incorrect conclusions and potentially invalidate the entire study. It is crucial to carefully determine and meet the required number of comparisons for accurate and valid results.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
34
Views
3K
Replies
9
Views
2K
  • Programming and Computer Science
3
Replies
97
Views
7K
  • Programming and Computer Science
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top