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...
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
• 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...
"Using Python, code to find the smallest and largest planet mass (if known), and plot these...
"Using Python, code to find the smallest and largest planet mass (if known), and plot these as two curves against the year of discovery" Basically looking to use data from an excel sheet where 'disc_year' and 'pl_mass' are columns of data; and code Python to find the maximum value of mass and the minimum value of mass for each year, then plot this as two curves. There are multiple planets for any given year hence multiple values for mass. Below...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT