Question

In: Computer Science

3. Suppose we want to find the 2nd largest and 2nd smallest elements simultaneously for an...

3. Suppose we want to find the 2nd largest and 2nd smallest elements simultaneously for an input of n numbers stored in an array A[1:n]. Compare the following strategies in terms of their exact number of comparisons. Strategy 1: adapt the two separate For loops idea for minimum and maximum. Strategy 2: Run mergesort to sort the numbers in ascending or descending order, then output the 2nd ranked and (n-1)th ranked elements. First write each algorithm in pseudocde, then analyze the exact number of comparisons required by the algorithm. Note, asymptotic calculations will only get you partial credit for this problem. Can you find a better strategy for this problem than the two given? If yes, write the algorithm and analyze the exact number of comparisons required by the algorithm. If not, prove that one of the given strategies is optimal.

Solutions

Expert Solution

Scenario 1:

If we use 2 separate  for  loops for finding the second minimum and second maximum, the time complexity will be O(2n).

Algorithm with 2 for loops
Initialize 4 variables max,secondMax,min and secondMin as,
   max = secondMax = min = secondMin = arr[1] // initialize to start element of array
Start traversing the array,
If the current element in array say arr[i] is greater
than max. Then update max and secondMax as,
secondMax = max
max = arr[i]
Else If the current element is greater than secondMax
secondMax = arr[i]
Print the value of secondMax.
  
  
Start traversing the array,
If the current element in array say arr[i] is less than
min. Then update min and secondMin as,
secondMin = min
min = arr[i]
Else If the current element is less than secondMin
secondMin = arr[i]
Print the value of secondMin.

Scenario 2 :

Merge sort uses Divide and Conquer approach. And it takes O(n log(n)), finding an element takes O(1) time complexity.

  • If it is only one element in the list it is already sorted, return.
  • Divide the list recursively into two halves until it can no more be divided.
  • Merge the smaller lists into new list in sorted order.

For more information please refer those images below:

#######Please give a positive rating for this answer.This helps me write more. Thank you. Have a nice day#######


Related Solutions

Problem 5: Find Smallest Elements In this problem, we will write a function to find the...
Problem 5: Find Smallest Elements In this problem, we will write a function to find the smallest elements of a list. Define a function named find_smallest() that accepts two parameters: x and n. The parameter x is expected to be a list of values of the same time, and n is expected to be an either integer, or the value None, and should have a default value of None. • If n is set to None, then the function should...
Problem 3: Minimum In this problem, we will write a function to find the smallest element...
Problem 3: Minimum In this problem, we will write a function to find the smallest element of a list. We are, in a sense, reinventing the wheel since the min() function already performs this exact task. However, the purpose of this exercise is to have you think through the logic of how such a function would be implemented from scratch. Define a function named minimum(). The function should accept a single parameter named x, which is expected to be a...
• 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
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...
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.
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
1. Using only the periodic table arrange the following elements in order of increasing atomic radius: (write order from smallest to largest)
  1. Using only the periodic table arrange the following elements in order of increasing atomic radius: (write order from smallest to largest) a) polonium, selenium, sulfur, oxygen b) gallium, indium, thallium, aluminum c) silicon, sodium, sulfur, argon 2. Using only the periodic table arrange the following elements in order of increasing ionization energy: (write order from lowest to highest) a) polonium, selenium, sulfur, oxygen b) gallium, indium, thallium, aluminum c) silicon, sodium, sulfur, argon 3. a) Write the complete...
Problem 3 (5 marks). Suppose elements a[i] and a[i+k] are in the wrong order and we...
Problem 3 . Suppose elements a[i] and a[i+k] are in the wrong order and we swap them. Prove that this will remove at least 1 inversion but at most 2k − 1 inversions. (This is textbook problem 7.3.) Further explain why both the lower bound of 1 and the upper bound of 2k − 1 can be attained for any i, k, where k > 0.
Suppose we want to test whether or not three means are equal. We want to perform...
Suppose we want to test whether or not three means are equal. We want to perform this test with a 2% significance level. If we perform an ANOVA test, what is the probability of the test producing accurate results (avoiding a Type I error)? Suppose we, instead, run three separate hypothesis tests (t-tests), each with 2% significance level. Mean 1 = Mean 2 Mean 1 = Mean 3 Mean 2 = Mean 3 What is the probability that all three...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT