Question

In: Computer Science

You are a given an array of n elements, design an algorithm to find the minimum...

You are a given an array of n elements, design an algorithm to find the minimum element and the 2nd minimum element in the array using as few comparisons as possible.

For example, A[] can be [5, 6, 7, 3, 2, 1]. You should output 1 as minimum, 2 as 2nd minimum element in the array, and at the same time, minimize the number of comparisons used.


(i) describe the idea behind your algorithm in English
(ii) provide pseudocode
(iii) How many comparisons does your algorithm make?

Solutions

Expert Solution

Idea behind algorithm:
There is no way to find the second minimum element in array without knowing the minimum element because how can we know that an element is smaller than all rest elements in array except one(minimim), without knowing that one(minimum).
So, we have to find the minimum element, then and only we can find the second minimum.

Naive approach: 
find the minimum element in the array in one traversal
traverse the array once again, and find minimum element in array but not equal to the element we found in first traversal.
Efficient approach:
take two variables initially, i.e.  first, second.
now in single traversal of the array,
we can update the two variables in accordance with the current element of array we encounter in each iteration.
compare the current with first & second, and update first & second according to the outcome of comparison( this can be understood in Pseudocode below).


Pseudocode:
START
first = INT_MAX
second = INT_MAX
if len(arr) < 2:
   return "no second largest element exist"
for num in arr:
   if arr[i] < first:
      second = first
      first = arr[i]
   else if first <= arr[i] < second:     // if arr[i] is between first and second
      second = arr[i]
   return second
END;

Comparisons:

for every element in array, we have two if conditions,

so there are at most 2 comparisons for each element in array.

Let n be the number of elements in array.

So, total number of comparisons = 2*n


Related Solutions

You are a given an array of n elements, design an algorithm to find the minimum...
You are a given an array of n elements, design an algorithm to find the minimum element and the 2nd minimum element in the array using as few comparisons as possible. For example, A[] can be [5, 6, 7, 3, 2, 1]. You should output 1 as minimum, 2 as 2nd minimum element in the array, and at the same time, minimize the number of comparisons used. (i) describe the idea behind your algorithm in English (ii) provide java code...
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 42 39 2 6 9 16 20 28 31 34
We are given an array of n numbers A in an arbitrary order. Design an algorithm...
We are given an array of n numbers A in an arbitrary order. Design an algorithm to find the largest and second largest number in A using at most 3/2n -2 comparisons. (i) describe the idea behind your algorithm in English (3 points); (ii) provide pseudocode (5 points); (iii) analyze the number of comparisons used in your algorithm (2 points).
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
You are given an array of n elements, and you notice that some of them are...
You are given an array of n elements, and you notice that some of them are duplicates, that is, they appear more than once in the array. Show how to remove all duplicates from the array in time O( n log2 n ).
Given an array A of n integers, describe an O(n log n) algorithm to decide if...
Given an array A of n integers, describe an O(n log n) algorithm to decide if the elements of A are all distinct. Why does your algorithm run in O(n log n) time?
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
Consider the following algorithm to find the kth largest elementof a given array A of...
Consider the following algorithm to find the kth largest element of a given array A of n numbers. We pick every fifth element of A and place them in the array B. We find the median of B recursively, and use this median of B as a pivot to partition the array A. Depending on k and the number of elements that are smaller than the chosen pivot, we recurse into an appropriate subproblem of A.Answer the following questions.Write the...
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm...
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 0 1 2 3 4 5 6 7 8 9 33 32 27 26 24 22 21 8 7 3 IN C++ PLEASE
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT