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 java code
(iii) How many comparisons does your algorithm make?

Solutions

Expert Solution

Here are the solution to above problem:

  1. We are iterating over the complete array and then comparing the values to find the smallest.
  2. /**
    * The Class SecondSmallest.
    */
    class FindSmallest {

       /**
       * Prints the smallest.
       *
       * @param arr the arr
       */
       static void printSmallest(int arr[]) {
           int first = arr.length;
           int second = arr.length;
           int arraySize = arr.length;

           if (arraySize < 2) {
               System.out.println("Invalid");
               return;
           }
           first = second = Integer.MAX_VALUE;
           for (int i = 0; i < arraySize; i++) {
               if (arr[i] < first) {
                   second = first;
                   first = arr[i];
               } else if (arr[i] < second && arr[i] != first) {
                   second = arr[i];
               }
           }
           if (second == Integer.MAX_VALUE)
               System.out.println("There is no second" + "smallest element");
           else
               System.out.println("The smallest element is " + first + " and second Smallest" + " element is " + second);
       }

       /**
       * The main method.
       *
       * @param args the arguments
       */
       public static void main(String[] args) {
           int arr[] = { 5, 6, 7, 3, 2, 1 };
           printSmallest(arr);
       }
    }

  3. it will be O(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 pseudocode (iii)...
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