sort – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 21 Aug 2022 10:43:16 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg sort – Programmerbay https://programmerbay.com 32 32 Java Program to Sort an Array in Ascending Order and Descending Order https://programmerbay.com/java-program-to-sort-the-array-in-an-ascending-order-and-descending-order/ https://programmerbay.com/java-program-to-sort-the-array-in-an-ascending-order-and-descending-order/#respond Thu, 11 Aug 2022 10:00:59 +0000 https://www.programmerbay.com/?p=2339 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]

 

]]>
https://programmerbay.com/java-program-to-sort-the-array-in-an-ascending-order-and-descending-order/feed/ 0