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

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 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?
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
Find the smallest n ∈ N such that 2(n + 5)^2 < n^3 and call it...
Find the smallest n ∈ N such that 2(n + 5)^2 < n^3 and call it n^0,Show that 2(n + 5)^2 < n^3 for all n ≥ n^0.
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a set of integers. MUST USE JAVA AND QUICKSELECT. Algorithm 1: Procedure SELECT( k,S) { if ISI =1 then return the single element in S    else { choose an element randomly from S;               let S1, S2, and S3 be the sequences of elements in S                 less than, equal to, and greater than m, respectively;              if IS1I >=k then return SELECT(k,S1)              ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT