Java Program to to sort the elements of an array in ascending order


April 11, 2022, Learn eTutorial
913

Here we are explaining how to write a java program to sort the array elements. So first we have to read the limit of the array and read the array elements. Then we will perform the sorting by using nested for loop. Then we will display the sorted array.

What is the syntax of nested for loop in java?

If a for loop exists inside a for loop is called nested for loop. The syntax of the nested for loop is shown below.


for (Initialization; Condition; Incremnet) {
    for (Initialization; Condition; Incremnet)
        {
             //Statement of inside loop

        }
    // Statement of outer loop
}
 

How to implement the java program to sort the array elements?

First, we have to declare the class sort. Then open the main function. Create an object of the scanner class as s. Read the limit of the array into the variable l. Declare and initialize an array a[] of size l. Then read the numbers into the array a[] using for loop. Then we will perform the sorting operation. For that, we have to use nested for loop. The outer loop takes each number in the array from zero'th position to l-1.The inner loop check number a[i] with a[j] where j=i+1 to l-1.After the first iteration, we will get the smallest number at position zero. After exiting from the loop display the sorted array as the array elements using for the loop.

ALGORITHM

STEP 1: Declare the class Sort with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the variables l, temp as an integer.

STEP 4: Create the object of the scanner class as s.

STEP 5: Read the limit of the array into the variable l.

STEP 6: Declare and initialize an integer array a[]  of size l.

STEP 7: Read the elements of the array into a[i] using for loop.

STEP 8: By using nested for loop, check each number, with its next number to the last number, and place in sorted order by swapping.

STEP 9: Display the sorted array as each element of a[i] using for loop.

 

Java Source Code

                                          import java.util.Scanner;
public class Sort {
    public static void main(String[] args) {
        int l, temp;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the limit of the array:");
        l = s.nextInt();
        int a[] = new int[l];
        System.out.println("Enter the elements:");
        for (int i = 0; i < l; i++) {
            a[i] = s.nextInt();
        }
        for (int i = 0; i < l; i++) {
            for (int j = i + 1; j < l; j++) {
                if (a[i] > a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.println("Sorted Array..");
        for (int i = 0; i < l; i++) {
            System.out.println(a[i]);
        }

    }
}
                                      

OUTPUT

Enter the limit of the array:4
Enter the elements:
1444
2
30
4
Sorted Array..
2
4
30
1444