Java Program to Sort an Array in Ascending Order

Java Program to Sort an Array in Ascending Order

Java Program to Sort an Array in Ascending Order

import java.util.Scanner;

public class ExArraySortElement {
  public static void main(String[] args) {
    int n, temp;
    //scanner class object creation
    Scanner s = new Scanner(System.in);

    //input total number of elements to be read
    System.out.print("Enter the elements you want : ");
    n = s.nextInt();

    //integer array object
    int a[] = new int[n];

    //read elements 
    System.out.println("Enter all the elements:");
    for (int i = 0; i < n; i++) {
      a[i] = s.nextInt();
    }

    //sorting elements
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (a[i] > a[j]) {
          temp = a[i];
          a[i] = a[j];
          a[j] = temp;
        }
      }
    }

    //print sorted elements 		
    System.out.println("Ascending Order:");
    for (int i = 0; i < n; i++) {
      System.out.println(a[i]);
    }
  }
}
Enter the elements you want : 10
Enter all the elements:
12
25
99
66
33
8
9
54
47
36
Ascending Order:
8
9
12
25
33
36
47
54
66
99

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment