Finding the second smallest integer in array

后端 未结 19 1880
無奈伤痛
無奈伤痛 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: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);
    
        }
    

提交回复
热议问题