Ascending Descending Order Java

Ascending order is the order in which the number starts from lowest and move to the greatest, whereas Descending order is the order in which the number start from the greatest and move toward the lowest.

Check out the simple java code for ascending and descending of a give set of numbers.

Ascending Descending Order Java

public class AscendingOrder {

    public static void main(String[] args) {
        int[] a = {23, 44, 55, 66, 11, 3, 6, 23, 44};
        int temp;
        //for ascending order
        for (int i = 0; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.println("The array in ascending order is : ");
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }

}

Output:

The array in ascending order is :
3
6
11
23
23
44
44
55
66

public class DescendingOrder {

    public static void main(String[] args) {
        int[] a = {23, 44, 55, 66, 11, 3, 6, 23, 44};
        int temp;
        //for descending order
        for (int i = 0; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] < a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.println("The array in descending order is : ");
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }

}

Output:

The array in descending order is :
66
55
44
44
23
23
11
6
3