Java Program to Sort an Array in Ascending Order and Descending Order

Share

The program prints sorted array both in ascending order and descending order. It uses a Scanner class for input and a temp variable to swap among the elements. Further, nested loops are required in order to iterate and sort each element of an array. Also, there is an alternative approach to sort an array using inbuilt method named sort().

Example 1. Java Program to Sort an Array in an Ascending Order and Descending Order

Program:

import java.util.Scanner;

/*
 * Program to Sort an Array in Ascending Order and Descending Order
 * */public class ArraySortingExample {
    public static void main(String[] args) {
        Scanner ed = new Scanner(System.in);
        int[] a = new int[5];
        int i, j, temp;
        System.out.println("Please Enter 5 elements in the Array");
        for (i = 0; i < 5; i++) {
            a[i] = ed.nextInt();
        }
        for (i = 0; i < 5; i++) {
            for (j = i + 1; j < 5; j++) {
                if (a[i] > a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.println("Sorted Array in Increasing Order:-");
        for (j = 0; j < 5; j++) {
            System.out.println(a[j]);
        }
        for (i = 0; i < 5; i++) {
            for (j = i + 1; j < 5; j++) {
                if (a[i] < a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.println("Sorted Array in Decreasing Order:-");
        for (j = 0; j < 5; j++) {
            System.out.println(a[j]);
        }
    }
}

Output:

Please Enter 5 elements in the Array
45
3
12
5
34
23
Sorted Array in Increasing Order:-
3
5
12
34
45
Sorted Array in Decreasing Order:-
45
34
12
5
3

 

Example 2. Java Program to sort an array in Ascending Order Using Arrays.sort() method

Program:

import java.util.*;
public class ArraySortingExample {
    public static void main(String[] args) {
        Scanner ed = new Scanner(System.in);
        Integer[] arr = new Integer[5];
        int i;
        System.out.println("Please Enter 5 elements in the Array : ");
        for (i = 0; i < 5; i++) {
            arr[i] = ed.nextInt();
        }

        Arrays.sort(arr);
        System.out.println("Sorted Array in Ascending Order " + Arrays.toString(arr));

        Arrays.sort(arr,Comparator.reverseOrder());
        System.out.println("Sorted Array in Descending Order " + Arrays.toString(arr));
    }
}

Output:

Please Enter 5 elements in the Array : 
45
3
234
54
2321

Sorted Array in Ascending Order [3, 45, 54, 234, 2321]
Sorted Array in Descending Order [2321, 234, 54, 45, 3]

 

This post was last modified on August 21, 2022

Sandeep Verma

Published by
Sandeep Verma
Tags: arrays java java program Java Program to Sort the Array in an Ascending Order program sort sort an array in Ascending Order