Pendulum Array

Input=1,2,3,4,5
Output=1,3,5,4,2


class sorthigh
{
  public static void main(String[]args)
{




   int a[]={1,2,3,4,5};
   int n=a.length;
   int k=0;
   if(n%2==0)
     k=0;
     else
     k=1;
     while(k<=n-1)
     {
         for(int i=k;i<n;i+=2)
         {
             if(i!=n-1)
             {
                 int t=a[i];
                 a[i]=a[i+1];
                 a[i+1]=t;
                }
            }
           k++;
        }
        for(int i=0;i<n;i++)
        {
            System.out.print(a[i]+" ");
        }
      
    }
}

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...