Question

In: Computer Science

Write an algorithm that finds both the smallest and largest numbers in a list of n...

Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array items.(but please code in java).

Solutions

Expert Solution

public class MinMax {
        public static void main(String[] args) {
                int arr[]= {10,5,4,12,15,24,29,32,9,19};
                int minmax[]=getMinMax(arr);
                System.out.println("Min: "+minmax[0]);
                System.out.println("Max: "+minmax[1]);
                
        }

        // here this single methods returns array of values
        // 1st index will have min value and 2nd index will have the max value
        private static int[] getMinMax(int[] arr) {
                int res[]=new int[2];
                int min=arr[0];
                int max=arr[0];
                // iterating through array to find min and max values in the array
                for(int i=0;i<arr.length;i++) {
                        if(arr[i]<min)
                                min=arr[i];
                        if(arr[i]>max)
                                max=arr[i];
                }
                res[0]=min;
                res[1]=max;
                return res;
        }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

Please Like and Support me as it helps me a lot


Related Solutions

Write two algorithms to find both the smallest and largest numbers in a list of n...
Write two algorithms to find both the smallest and largest numbers in a list of n numbers. In first algorithm, you simply write one loop to find the minimum and maximum. So there will be 2(n - 1) comparisons. In second algorithm, you try to find a method that does at most 1.5n comparisons of array items. Determine the largest list size (i.e., n) that Algorithm 1 can process and still compute the answer within 60 seconds. Report how long...
Write a Python function which finds the smallest even and odd numbers in a given list....
Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for this problem)
A list of 100 numbers has the largest value 90 and the smallest value 12.9, with...
A list of 100 numbers has the largest value 90 and the smallest value 12.9, with a mean 40, a median 50, and a standard deviation 20. However, you accidentally copied the smallest number “12.9” as “1.29”. Is it possible to determine by how much the mean, the median and the standard deviation change? For each quantity, if so, show your computation. Otherwise, explain why
Answer the following questions(5pt) a)   Write an algorithm that finds the largest item in an array...
Answer the following questions(5pt) a)   Write an algorithm that finds the largest item in an array of n items by using divide-and-conquer algorithm design pattern. b)   Analyze the worst-case time complexity of your algorithm in (a), and show the results using asymptotic notation (ϴ)
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in the range from start_num to end_num (inclusive). For example, start_num = 5 and end_num = 12, the prime numbers in that range are: [5, 7, 11]. The smallest gap between any two prime numbers in this list is 2, so the function would return 2. You may want to modify your solution from Problem 1 on Assignment 4, or you can use this starter...
. For n = 2 look at the smallest and largest sample means. How close is...
. For n = 2 look at the smallest and largest sample means. How close is each of these sample means to the population mean? Use the formula x − . This difference is called the sampling error. Do the same for n = 3 and n = 4. What do you notice as n gets larger?
Write a function that will accept a list of numbers and an integer (n). The function...
Write a function that will accept a list of numbers and an integer (n). The function should return a list containing every nth item from the input list, always starting with the first item in the list. The original list should not be modified. For example, if the function is passed the list [8, 3, 19, 26, 32, 12, 3, 7, 21, 16] and the integer 3, it will return the list [8, 26, 3, 16] If the function is...
1. Write a program that computes the smallest and largest of a set of values that...
1. Write a program that computes the smallest and largest of a set of values that are stored in an array. Ask the user for the set size (and hence the array size). Populate the array with user input.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT