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...
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
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...
Write an Intel 8085 assembly program to find the largest of N numbers stored in memory...
Write an Intel 8085 assembly program to find the largest of N numbers stored in memory using the algorithm below. Hand trace (execute) the program showing the changes made to all affected registers and memory locations. Max = a(1) For i = n to N If max < a(i) then max = a(i) Next i
Python - You are given a sorted (from smallest to largest) array A of n distinct...
Python - You are given a sorted (from smallest to largest) array A of n distinct integers which can be positive, negative or zero. Design the fastest algorithm you can for deciding if there is an index i such that A[i] = i.
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT