Question

In: Computer Science

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 it takes Algorithm 2 to compute this answer.

Solutions

Expert Solution

Algorithm 1:

max = A[0], min = A[0]
for each i in A
    if A[i]>max then max = A[i]
    if A[i]<min then min = A[i]

Algorithm 2:

if (a[0] > a[1]) {

    curr_max = A[0]

    curr_min = A[1]

  } else {

    curr_max = A[1]

    curr_min = A[0]

  }

  for (i = 2 to n-2) {

    if (A[i] > A[i + 1]) {

      min = min(curr_min, A[i + 1])

      max = max(curr_max, A[i])

    } else {

      min = min(curr_min, A[i])

      max = max(curr_max, A[i + 1])

    }

    i = i + 2

  }

  if (n % 2 == 1) {

    min = min(curr_min, A[n-1])

    max = max(curr_max, A[n - 1])

  }

2*(n-1) = 60 => n = 31

Hence, the largest list size that Algorithm 1 can process and still compute the answer within 60 seconds = 31.

Now, time taken by algorithm 2 = 1.5 * 31 = 46.5


Related Solutions

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).
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
Question III: Programming\flowcharts\algorithms Sort the following list from the smallest to the largest using the bubble...
Question III: Programming\flowcharts\algorithms Sort the following list from the smallest to the largest using the bubble sort algorithm (show all passes) [4 points] 20 10 30 5 0 Write a python program that asks the user to enter the number of seconds. The program will print the whole hours, whole minutes and remaining seconds. For example: if the user entered the number of seconds as 3663 then the output will be: 1 hour(s), 1 minute(s), 3 second(s). [5 points] Note:...
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
• 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
The sum of two numbers is 34. a)Find the largest possible product of these numbers.
  1-The sum of two numbers is 34.    a)Find the largest possible product of these numbers.    b)What would be the largest possible product if the sum if the two numbers were "k"? 2-Sixty meters of fencing are used to fence a rectangular garden.    a)Find the dimensions that will give that maximum area.    b)What would be the maximum area if "k" feet of fencing were used in terms of "k"? THANK YOU
Let A and B be two events. Find the largest and smallest possible values P(A U...
Let A and B be two events. Find the largest and smallest possible values P(A U B) can take in terms of P(A) and P(B) and give examples in which these values can be attained.
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
. 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT