Algorithm with ceil(n/2) comparisons

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Algorithm
In summary, the conversation discusses designing an algorithm to find the minimum element in a strictly alternating sequence of numbers with at most $\lceil \frac{n}{2} \rceil$ comparisons. The suggested algorithm involves storing the index of the minimum element and only updating it when a new minimum is found. It is shown that this algorithm requires $\lceil \frac{n}{2} \rceil$ comparisons, regardless of the length of the sequence. It is also discussed that simplifying the code by not checking the last element every time does not affect the correctness and actually makes the analysis simpler.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)

I am looking at the following exercise:

We say that a sequence of numbers $A=(a_1,a_2, \dots, a_n) , n \geq 3$, is strictly alternating if for each $i$, with $2 \leq i \leq n-1$, one of the following conditions stands:

  • $a_{i-1}< a_i$ and $a_i>a_{i+1}$
    $$$$
  • $a_{i-1}>a_i$ and $a_i<a_{i+1}$

For example, the sequences $(2,10,5,6,3,11,-1,7,4)$ and $(2,-7,4,3,6,0,33,2)$ are alternating, but the sequnce $(5,6,7,3,4,-2,7,10)$ isn't.

Design an algorithm, that, given an alternating sequence of numbers $A$, executes at most $\lceil \frac{n}{2} \rceil$ comparisons, in order to calculate the minimum element of $A$. Justify your answer.

I wrote the following code:

Code:
#include <stdio.h>

int main()
{
int n,i,min,position_min;
printf("Give n: \n");
scanf("%d",&n);
while (n<3){
   printf("	Give an other value for n: \n");
   scanf("%d", &n);
}
int A[n];
for (i=0; i<n; i++){
     printf("Give the value of the %dth position of the array: \n",i+1);
     scanf("%d",&A[i]);
}
min=A[0];
position_min=0;
if (A[1]<min) {
    min=A[1];
    position_min=1;
}
for (i=position_min+2; i<n; i+=2){
    if (A[i]<min){
       min=A[i];
       position_min=i;
    }
}
printf("min=%d \n ",min);
return 0;
}

So, we need $2 \left ( \frac{n-\text{position_min}-2+1}{2} \right )=n-\text{position_min}-1$ comparisons..But.. we don't need $\lceil \frac{n}{2} \rceil$ comparisons, at the algorithm I wrote.. (Sweating)

How could I do it otherwise? :confused:
 
Technology news on Phys.org
  • #2
If the first number is smaller than the second then you have the indecies are even. Otherwise they are odd.

You don't need to store the value of the element but rather you store the index.

For example :

Code:
A = {4,6,5,7,3,8,2}  
Then min is initialized to 0 since 4 <6.  counter =2;
Since A[min]<A[counter] , min stays 0;
Counter = counter+2 = 4;
Since A[min]>A[counter] min=4;
Counter = counter+2=6;
Since A[min]>A[counter] min=6;
Counter = counter+2=8 > length(A)
Hence min = 6 and the minimum is A[6]=2;

Since each time we travel almost to the end of the array and we skip one element at a time we expect the complexity to be \(\displaystyle \frac{n}{2}\).Fore the case were the first element is greater than the second initialize min=1 and counter = 3.
 
  • #3
ZaidAlyafey said:
If the first number is smaller than the second then you have the indecies are even. Otherwise they are odd.

You don't need to store the value of the element but rather you store the index.

For example :

Code:
A = {4,6,5,7,3,8,2}  
Then min is initialized to 0 since 4 <6.  counter =2;
Since A[min]<A[counter] , min stays 0;
Counter = counter+2 = 4;
Since A[min]>A[counter] min=4;
Counter = counter+2=6;
Since A[min]>A[counter] min=6;
Counter = counter+2=8 > length(A)
Hence min = 6 and the minimum is A[6]=2;

Since each time we travel almost to the end of the array and we skip one element at a time we expect the complexity to be \(\displaystyle \frac{n}{2}\).Fore the case were the first element is greater than the second initialize min=1 and counter = 3.

So, at this for loop:

Code:
for (i=position_min+2; i<n; i+=2){
    if (A[i]<min){
       min=A[i];
       position_min=i;
    }
}

we don't need to write the command:
Code:
position_min=i;
, right? (Thinking)

So,should it be like that? :confused:

Code:
#include <stdio.h>

int main()
{
int n,i,min,j;
printf("Give n: \n");
scanf("%d",&n);
while (n<3){
   printf("	Give an other value for n: \n");
   scanf("%d", &n);
}
int A[n];
for (i=0; i<n; i++){
     printf("Give the value of the %dth position of the array: \n",i+1);
     scanf("%d",&A[i]);
}
min=A[0];
j=0;
if (A[1]<min) {
    min=A[1];
    j=1;
}
for (i=j+2; i<n; i+=2){
    if (A[i]<min){
       min=A[i];
    }
}
printf("min=%d \n ",min);
return 0;
}
 
  • #4
Generally we are not interested in the element but rather the index so we have

Code:
if(A[0] < A[1])
    min=0;
else
    min=1;

for(counter = min+2; counter < n ; counter+=2)
    if(A[counter] < A[min])
          min = counter;

printf("The minimum is %d ", A[min]);
 
  • #5
ZaidAlyafey said:
Generally we are not interested in the element but rather the index so we have

Code:
if(A[0] < A[1])
    min=0;
else
    min=1;

for(counter = min+2; counter < n ; counter+=2)
    if(A[counter] < A[min])
          min = counter;

printf("The minimum is %d ", A[min]);

So, we need $n-min-2+1=n-min-1=\left\{\begin{matrix}
\frac{n-1}{2} &, min=0 \\
\\
\frac{n-2}{2} & ,min=1
\end{matrix}\right.$ comparisons, right? (Thinking)

But.. how can we show that we need $ \lceil \frac{n}{2} \rceil$ comparisons? :confused:
 
  • #6
Just don't bother only checking the last element if min = 0, do it every time. It doesn't affect correctness, and makes your code simpler to analyze, because in that case you simply have this number of comparisons:

$$1 + \left ( \left \lfloor \frac{n}{2} \right \rfloor - 1 \right ) + (n \bmod 2) = \left \lfloor \frac{n}{2} \right \rfloor + (n \bmod 2)$$

Where the first $1$ is the initial comparison, the second term is the for loop (note that you must subtract $1$ because you've already processed the first two elements) and the final $n \bmod 2$ is because we need one more comparison if $n$ is odd for the last element.

If $n$ is even, then this gives:

$$\frac{n}{2} = \left \lceil \frac{n}{2} \right \rceil$$

And if $n$ is odd, then this gives:

$$\frac{n - 1}{2} + 1 = \frac{n + 1}{2} = \left \lceil \frac{n}{2} \right \rceil$$

And so you are done. Sometimes it pays off to not be too clever :)
 

FAQ: Algorithm with ceil(n/2) comparisons

What is an algorithm with ceil(n/2) comparisons?

An algorithm with ceil(n/2) comparisons is a type of algorithm that uses the ceiling function to determine the number of comparisons needed to solve a problem. The ceiling function rounds up the result of n divided by 2 to the nearest whole number, ensuring that the algorithm will use at least this many comparisons.

How does an algorithm with ceil(n/2) comparisons work?

An algorithm with ceil(n/2) comparisons works by dividing the problem into two parts and then using the ceiling function to determine the number of comparisons needed for each part. This ensures that the algorithm will use at least this many comparisons to solve the problem.

What are the advantages of using an algorithm with ceil(n/2) comparisons?

One advantage of using an algorithm with ceil(n/2) comparisons is that it guarantees a minimum number of comparisons, which can help with performance optimization. Additionally, it can be useful for problems where the input size is unknown or varies greatly.

Are there any drawbacks to using an algorithm with ceil(n/2) comparisons?

One potential drawback is that the algorithm may use more comparisons than necessary in some cases. This can result in slower performance for smaller input sizes. Additionally, the use of the ceiling function may add some complexity to the algorithm.

In what types of problems is an algorithm with ceil(n/2) comparisons commonly used?

An algorithm with ceil(n/2) comparisons is commonly used in problems that require a divide and conquer approach, such as sorting or searching algorithms. It is also useful for problems where the input size is large and/or variable.

Similar threads

Replies
1
Views
2K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
17
Views
1K
Replies
1
Views
1K
Replies
6
Views
6K
Back
Top