Finding the second smallest integer in array

后端 未结 19 1883
無奈伤痛
無奈伤痛 2021-01-18 05:48

We are required in our assignment to find the second smallest integer in one array recursively. However, for the sake of understanding the subject more, I want to do it iter

相关标签:
19条回答
  • 2021-01-18 06:23

    Here is TimeComlexity Linear O(N):

      public static int secondSmallest(int[] arr) {
        if(arr==null || arr.length < 2) {
            throw new IllegalArgumentException("Input array too small");
        }
    
        //implement
        int firstSmall = -1;
        int secondSmall = -1;
    
        //traverse to find 1st small integer on array
        for (int i = 0; i<arr.length;i++)
            if (firstSmall == -1 || arr[firstSmall]>arr[i])
                firstSmall = i;
    
         //traverse to array find 2 integer, and skip first small
        for (int i = 0;i<arr.length;i++) {
            if (i != firstSmall && (secondSmall == -1 || arr[secondSmall] > arr[i]))
                secondSmall = i;
        }
    
        return arr[secondSmall];
    }
    
    0 讨论(0)
  • 2021-01-18 06:23

    public class SecondSmallestNumberInArray 
            {
                public static void main(String[] args) 
                {
                    int arr[] = { 99, 76, 47, 85, 929, 52, 48, 36, 66, 81, 9 };
                    int smallest = arr[0];
                    int secondSmallest = arr[0];
    
                    System.out.println("The given array is:");
                    boolean find = false;
                    boolean flag = true;
    
                    for (int i = 0; i < arr.length; i++) 
                    {
                        System.out.print(arr[i] + "  ");
                    }
    
                    System.out.println("");
    
                    while (flag) 
                    {
                        for (int i = 0; i < arr.length; i++) 
                        {
                            if (arr[i] < smallest) 
                            {   
                                find = true;
                                secondSmallest = smallest;
                                smallest = arr[i];
                            } else if (arr[i] < secondSmallest) {   
                                find = true;
                                secondSmallest = arr[i];
                            }
                        }
                        if (find) {
                            System.out.println("\nSecond Smallest number is Array : ->  " + secondSmallest);
                            flag = false;
                        } else {
                            smallest = arr[1];
                            secondSmallest = arr[1];
                        }
                    }
                }
            }
    
        **Output is**
    
        D:\Java>java SecondSmallestNumberInArray
    
        The given array is:
    
        99  76  47  85  929  52  48  36  66  81  9
    
        Second Smallest number is Array : ->  36
    
        D:\Java>
    
    0 讨论(0)
  • 2021-01-18 06:23

    Here's a Swift version that runs in linear time. Basically, find the smallest number. Then assign the 2nd minimum number as the largest value. Then loop through through the array and find a number greater than the smallest one but also smaller than the 2nd smallest found so far.

    func findSecondMinimumElementLinear(in nums: [Int]) -> Int? {
        // If the size is less than 2, then returl nil.
        guard nums.count > 1 else { return nil }
    
        // First, convert it into a set to reduce duplicates.
        let uniqueNums = Array(Set(nums))
    
        // There is no point in sorting if all the elements were the same since it will only leave 1 element
        // after the set removed duplicates.
        if uniqueNums.count == 1 { return nil }
    
        let min: Int = uniqueNums.min() ?? 0 // O(n)
        var secondMinNum: Int = uniqueNums.max() ?? 0 // O(n)
        // O(n)
        for num in uniqueNums {
            if num > min && num < secondMinNum {
                secondMinNum = num
            }
        }
    
        return secondMinNum
    }
    
    0 讨论(0)
  • 2021-01-18 06:26

    Try this one.

        public static void main(String args[]){
            int[] array = new int[]{10, 30, 15, 8, 20, 4};
    
            int min, secondMin;
    
            if (array[0] > array[1]){
                min = array[1];
                secondMin = array[0];
            }
            else{
                min = array[0];
                secondMin = array[1];
            }
    
            for (int i=2; i<array.length; i++){
                if (array[i] < min){
                    secondMin = min;
                    min = array[i];
                }
                else if ((array[i] > min) && (array[i] < secondMin)){
                    secondMin = array[i];
                }
            }
           System.out.println(secondMin);
      }
    
    0 讨论(0)
  • 2021-01-18 06:31

    Find the second minimum element of an array in Python, short and simple

    def second_minimum(arr):
        second = arr[1]
        first = arr[0]
    
        for n in arr:
            if n < first:
                first = n
            if n > first and n < second  :
                second = n
    
        return second
    
    print(second_minimum([-2, 4, 5, -1, 2, 3, 0, -4, 1, 99, -6, -5, -19]))
    
    0 讨论(0)
  • 2021-01-18 06:31
    public static void main(String[] args)  {
        int arr[] = {6,1,37,-4,12,46,5,64,21,2,-4,-3};
        int lowest =arr[0];
        int sec_lowest =arr[0];
        for(int n : arr){
            if (lowest >  n)
            {
                sec_lowest = lowest;
                lowest = n;
    
            }
            else if (sec_lowest > n && lowest != n)
                sec_lowest = n;
        }
    
        System.out.println(lowest+"   "+sec_lowest);
    
        }
    
    0 讨论(0)
提交回复
热议问题