How many comparisons are needed?

  • MHB
  • Thread starter evinda
  • Start date
In summary, the process of finding the smallest and second smallest element in an array by comparing pairs of elements requires $3 \lfloor \frac{n}{2}\rfloor$ comparisons, where $n$ is the number of elements in the array. This is true for both even and odd values of $n$.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)Suppose that we want to find the smallest and second smallest element of an array and we consider the elements as pairs.

For example if we have this array:

View attachment 3839

We compare [m]7[/m] with [m]8[/m] and conclude that the smallest is [m]7[/m] and the second smallest is [m]8[/m]. Then we look at the numbers [m] 3 [/m], [m] 4 [/m], we compare them, we see that [m] 3 [/m] is the smaller one , then we compare [m] 3 [/m] with the by now smallest, i.e. with [m]7[/m], we see that [m] 3 [/m] is smaller, so it is our new smallest element. Then we assign to the variable for the second smallest element the number [m] 7 [/m] and compare it with [m] 4 [/m] and since the latter is smaller the second smallest element will be [m] 4 [/m]. And we continue in the same way.
Right?

If so, then if we consider that we have $n=2k$ elements then we will need $1$ comparison for the first pair and $3$ for the remaining $\frac{2k-2}{2}=k-1$.
So in total we would need $1+3(k-1)=\frac{3n}{2}-2=3 \lfloor \frac{n}{2} \rfloor-2$.
Is it right?
Because in my lecture notes, it says that we need $3 \lfloor \frac{n}{2} \rfloor$ comparisons. :confused:
Also if $n=2k+1$, I think that we assign the first element to both of the variables for the smallest and the second smallest element, then considering the first pair we compare the two elements, compare the smallest of them with the by now smallest and if it is smaller change the variable for the smallest element and then compare the other element of the pair with the by now second smallest. So now we just need $3$ comparisons for each of the $k$ pairs, so $ \text{comparisons} =3k=3\frac{n-1}{2}=3 \lfloor \frac{n}{2}\rfloor$, right? (Thinking)
 

Attachments

  • array23.png
    array23.png
    1.2 KB · Views: 51
Last edited:
Technology news on Phys.org
  • #2
Yes, that is correct. In the case of $n=2k+1$, you would assign the first element to both variables for the smallest and second smallest elements. Then you would compare the two elements in the first pair and compare the smallest of them with the by now smallest element. If it is smaller, you would change the variable for the smallest element and then compare the other element of the pair with the by now second smallest. You would then proceed in the same way with the remaining $k$ pairs, resulting in a total of $3k = 3\frac{n-1}{2} = 3 \lfloor \frac{n}{2}\rfloor$ comparisons.
 

Related to How many comparisons are needed?

1. How do you determine the number of comparisons needed for a scientific study?

The number of comparisons needed for a scientific study is typically determined by the specific research question being investigated. This number can vary greatly depending on the complexity of the question, the size of the study population, and the statistical methods being used.

2. Is there a standard formula for calculating the number of comparisons needed?

No, there is no standard formula for calculating the number of comparisons needed. It is important for scientists to carefully consider the specific goals and design of their study in order to determine an appropriate number of comparisons.

3. Can too many comparisons lead to false conclusions?

Yes, conducting too many comparisons can increase the likelihood of false conclusions, also known as Type I errors. This is why it is important for scientists to carefully plan and justify the number of comparisons being made in their study.

4. Are there any techniques to reduce the number of comparisons needed?

Yes, there are several techniques that can be used to reduce the number of comparisons needed. These include using statistical methods such as Bonferroni correction or false discovery rate control, as well as carefully selecting a subset of comparisons that are most relevant to the research question.

5. How can scientists ensure that their study has enough statistical power with the number of comparisons being made?

To ensure that a study has enough statistical power, scientists can conduct power analyses to determine the appropriate sample size needed for a given number of comparisons. It is also important to carefully consider the effect size and variability of the data in order to have adequate statistical power.

Similar threads

  • Programming and Computer Science
Replies
4
Views
2K
Replies
9
Views
2K
  • Linear and Abstract Algebra
Replies
2
Views
1K
  • Programming and Computer Science
Replies
27
Views
2K
  • Programming and Computer Science
Replies
29
Views
2K
Replies
9
Views
2K
  • Programming and Computer Science
2
Replies
58
Views
8K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top