Sort the Array in Alternate Manner (Ascending and Descending)

 Sort the Array in Alternate Manner (Ascending and Descending)


I/P:

5
 1 7 11 16 19


O/P:

19 1 16 7 11


PROGRAM:

import java.util.*;
public class AlternateSortArray {

    public static void  Alternate (ArrayList<Integeral)
    {
        ArrayList<Integeral2=new ArrayList<>(al);
        
        Collections.sort(al);
        ArrayList<Integeral3=new ArrayList<>(al2);
        Collections.sort(al3,Collections.reverseOrder());
        int j=0,k=0;
        for(int i=0;i<al.size();i++)
        {
            if(i%2==0)
            {
                System.out.print(al3.get(j)+" ");
                j++;
            }
            else{
                System.out.print(al.get(k)+" ");
                k++;
            }
        }

       

    }
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int iNum=sc.nextInt();//5
        ArrayList<Integeral=new ArrayList<>();
        for(int i=0;i<iNum;i++)
        {
            al.add(sc.nextInt());
        }
        Alternate(al);
    }
    
}

// I/P:


// 5
// 1 7 11 16 19

// O/P:
// 19 1 16 7 11

Comments

Popular posts from this blog

PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION

PROGRAM TO FIND THE COUNT OF PARTICULAR NUMBER IN AN ARRAY

TELEPHONE BOOK USING HASHMAP