Finding the second smallest integer in array

后端 未结 19 2207
無奈伤痛
無奈伤痛 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:37

    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        System.out.println("Enter array size = ");
        int size=in.nextInt();
        int[] n = new int[size];
        System.out.println("Enter "+ size +" values ");
    
        for(int i=0;in[i]){
                ssmall=small;
                small=n[i];
                }else if(ssmall>n[i])
                    ssmall=n[i];
        }
    
        // finding second small if first element itself small
        if(small==n[0]){
            ssmall=n[1];
            for(int i=1;in[i]){
                    ssmall=n[i];
                    }
            }
        }
    
        System.out.println("Small "+ small+" sSmall "+ ssmall);
    
        in.close();
    }
    

提交回复
热议问题