Finding the second smallest integer in array

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

    Try this, program gives solution for both lowest value and second lowest value of array.

    Initialize min and second_min with first element of array.Find out the min value and compare it with second_min value . If it (second_min) is greater than current element of array and min value then the second_min value replace with current element of array.

    In case arr[]={2,6,12,15,11,0,3} like this , temp variable used to store previous second_min value.

    public class Main
        {
            public static void main(String[] args) {
                //test cases.
                int arr[]={6,12,1,11,0};
                //int arr[]={0,2,10,3,-3}; 
                //int arr[]={0,0,10,3,-3}; 
                //int arr[]={0,2 ,10, 3,-3}; 
                //int arr[]={12,13,1,10,34,1};
                //int arr[]={2,6,12,15,11,0,3};
                //int arr[]={2,6,12,15,1,0,3};
                //int arr[]={2,6,12,15};
                //int arr[]={0,1};
                //int arr[]={6,16};
                //int arr[]={12};
                //int arr[]={6,6,6,6,6,6};
                int position_min=0;
                int min=arr[0];int second_min=arr[0]; int temp=arr[0];
                if(arr.length==1)
                {
                    System.out.println("Lowest value is "+arr[0]+"\n Array length should be greater than 1. ");
                }
                else if(arr.length==2)
                {
                    if(arr[0]>arr[1])
                    {
                        min=arr[1];
                        second_min=arr[0];
                        position_min=1;
                    }
                    else
                    {
                        min=arr[0];
                        second_min=arr[1];
                        position_min=0;
                    }
                    System.out.println("Lowest value is "+min+"\nSecond lowest value is "+second_min);
                }
                else
                {
                    for( int i=1;i<arr.length;i++)
                    {
                       if(min>arr[i])
                       {
                           min=arr[i];
                           position_min=i;
                       }
                    }
                    System.out.println("Lowest value is "+min);
                    for(int i=1;i<arr.length;i++)
                    {
                        if(position_min==i)
                        {
    
                        }
                        else
                        {
                             if(second_min > min & second_min>arr[i])
                             {
                                 temp=second_min;
                                 second_min=arr[i];
                             }
                             else if(second_min == min )
                             {
                                 second_min=arr[i];
                             }
                        }
                    }
    
                    if(second_min==min )
                    {
                        second_min=temp;
                    }
                    //just for message if in case all elements are same in array.
                    if(temp==min && second_min==min)
                    {
                        System.out.println("There is no Second lowest element in array.");
                    }
                    else{
                        System.out.println("\nSecond lowest value is "+second_min);
                    }
    
                }
    
        }
        }
    
    0 讨论(0)
提交回复
热议问题