How to find min and max of 100 numbers ?

In summary, the minimum number of comparisons required to find the minimum and maximum of 100 numbers is approximately 1.5 times n - 2, which equals 148. This can be achieved by dividing the list of numbers into two lists of equal size, recursively breaking these lists down into smaller sublists, and then combining the results with two comparisons for each pair of lists. This results in a binary tree structure and the number of comparisons can be calculated as ⌈(3n/2)⌉ - 2. By using an iterative approach, the number of comparisons can also be determined as 148.
  • #1
22990atinesh
143
1

Homework Statement


The minimum number of comparisons required to find the minimum and the maximum of 100
numbers is ...

Homework Equations


##T(n)=T(\lceil \frac{n}{2} \rceil) + T(\lfloor \frac{n}{2} \rfloor) + 2##

The Attempt at a Solution


Recurrence for the above problem is
##T(n)=T(\lceil \frac{n}{2} \rceil) + T(\lfloor \frac{n}{2} \rfloor) + 2 \approx 1.5 \times n - 2##

hence ##T(100)=1.5 \times 100 - 2 = 148##
But by solving like this I'm coming up with the ans 162

We can divide list of 100 numbers in two list (50,50). Now upon combining the result of these two list 2 more comparisons will be required. Now recursively we break the lists like below, which will make a binary tree

##100 \implies (50,50)##
##50 \implies (25,25)##
##25 \implies (12,13)##
##12 \implies (6,6), 13 \implies (6,7)##
##6 \implies (3,3), 7 \implies(3,4)##
##3 \implies (2,1), 4 \implies (2,2)##

By combining the results upwards in the tree with 2 comparisons on merging each two lists I'm coming up with ans 162 what am I overcounting.
 
Physics news on Phys.org
  • #2
Splitting up the lists as you've shown, I also get 162. You can either split up the lists into powers of two, or use an iterative approach for 148 compares. In this case, it should be easy to see why the number of compares is ⌈(3n/2)⌉ - 2: the first pair takes 1 compare, all of the remaining pairs take 3 compares, and if there are a odd number of elements, the last one takes 2 compares.

Code:
static size_t cmp;              // number of compares

MINMAX findminmax(int a[], size_t sizea)
{
MINMAX minmax;
size_t i;
int min;
int max;

    if(sizea == 0){             // if empty array
        minmax.min = 0;         //   set min = max = 0
        minmax.max = 0;
        return minmax;
    }
    if(sizea == 1){             // if size == 1
        minmax.min = a[0];      //   set min = max = a[0]
        minmax.max = a[0];
        return minmax;
    }
    if(a[0] <= a[1]){           // set initial min and max
        minmax.min = a[0];
        minmax.max = a[1];
    } else {
        minmax.min = a[1];
        minmax.max = a[0];
    }
    cmp += 1;
    for(i = 2; i < (sizea-1); i += 2){ // find min and max
        if(a[i] <= a[i+1]){
            if(a[i  ] < minmax.min)
                minmax.min = a[i  ];
            if(a[i+1] > minmax.max)
                minmax.max = a[i+1];
        } else {
            if(a[i  ] > minmax.max)
                minmax.max = a[i  ];
            if(a[i+1] < minmax.min)
                minmax.min = a[i+1];
        }
        cmp += 3;
    }
    if(i < sizea){
        if(a[i] < minmax.min)
            minmax.min = a[i];
        if(a[i] > minmax.max)
            minmax.max = a[i];
        cmp += 2;
    }
    return minmax;
}
 
Last edited:

Related to How to find min and max of 100 numbers ?

1. How do I find the minimum and maximum values of a set of 100 numbers?

To find the minimum and maximum values of a set of numbers, you can use a simple algorithm called "linear search." Start by setting the first number in the set as both the minimum and maximum values. Then, compare this number to the next number in the set. If the next number is smaller than the current minimum, replace the minimum with the next number. If the next number is larger than the current maximum, replace the maximum with the next number. Continue this process until you have compared all 100 numbers, and you will have your minimum and maximum values.

2. Can I use a computer program to find the minimum and maximum values for me?

Yes, there are many programming languages that have built-in functions or methods specifically for finding the minimum and maximum values of a set of numbers. For example, in Python, you can use the min() and max() functions to find the minimum and maximum values, respectively. In Java, you can use the Collections.min() and Collections.max() methods to find the minimum and maximum values, respectively.

3. What if my set of numbers is not in a specific order? Can I still find the minimum and maximum values?

Yes, you can still find the minimum and maximum values even if your set of numbers is not in a specific order. The linear search algorithm described in the first question will work regardless of the order of the numbers. However, if you are using a programming language, it may be helpful to sort the numbers in ascending or descending order first, as this can make it easier to find the minimum and maximum values.

4. What is the best way to find the minimum and maximum values if I have a large set of numbers?

If you have a large set of numbers, it may be more efficient to use a different algorithm called "divide and conquer." This involves breaking the set of numbers into smaller subsets and finding the minimum and maximum values for each subset, then comparing the minimum and maximum values of each subset to find the overall minimum and maximum values. This approach can be more efficient for larger sets of numbers, as it reduces the number of comparisons needed.

5. Can I use statistical methods to find the minimum and maximum values of a set of numbers?

Yes, there are statistical methods such as descriptive statistics that can be used to find the minimum and maximum values of a set of numbers. These methods involve calculating measures such as the mean, median, and mode, which can give you an idea of the spread and distribution of the numbers in your set. However, if you simply need to find the exact minimum and maximum values, the linear search algorithm or other similar approaches may be more appropriate.

Similar threads

  • Electrical Engineering
Replies
4
Views
861
  • Programming and Computer Science
Replies
7
Views
3K
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Mechanical Engineering
Replies
2
Views
907
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Biology and Chemistry Homework Help
Replies
4
Views
1K
Back
Top