Java Program to Sort an Array in Ascending Order

Problem Description

Write a Java Program to Sort the Array in an Ascending Order.

Problem Solution

Enter the size of the array, and then enter all the elements of that array. Then, the program uses a for loop to sort the array in ascending order by comparing each element of the array with all the other elements in the array. If an element is smaller than the other elements, it is swapped with the larger element. This process is repeated until the entire array is sorted in ascending order.

Example:

Original Array:

4 3 2 6 1

Sorted Array:

1 2 3 4 6
Program/Source Code

Here is the source code of the Java Program to Sort the Array in an Ascending Order. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
advertisement
  1. /*
  2.  * Java Program to Sort an Array in Ascending Order
  3.  */
  4.  
  5. import java.util.Scanner;
  6. public class Ascending _Order 
  7. {
  8.     public static void main(String[] args) 
  9.     {
  10.         int n, temp;
  11.         Scanner s = new Scanner(System.in);
  12.         System.out.print("Enter no. of elements you want in array:");
  13.         n = s.nextInt();
  14.         int a[] = new int[n];
  15.         System.out.println("Enter all the elements:");
  16.         for (int i = 0; i < n; i++) 
  17.         {
  18.             a[i] = s.nextInt();
  19.         }
  20.         for (int i = 0; i < n; i++) 
  21.         {
  22.             for (int j = i + 1; j < n; j++) 
  23.             {
  24.                 if (a[i] > a[j]) 
  25.                 {
  26.                     temp = a[i];
  27.                     a[i] = a[j];
  28.                     a[j] = temp;
  29.                 }
  30.             }
  31.         }
  32.         System.out.print("Ascending Order:");
  33.         for (int i = 0; i < n - 1; i++) 
  34.         {
  35.             System.out.print(a[i] + ",");
  36.         }
  37.         System.out.print(a[n - 1]);
  38.     }
  39. }
Program Explanation

1. The program begins by importing the Scanner class to take input from the user.
2. A class called Ascending_Order is defined.
3. The main method is created with a Scanner object and two integer variables: n and temp.
4. It prompts the user to enter the size of the array and each element of the array.
5. The first for loop is used to traverse the array.
6. The nested for loop is used to compare each element of the array with all the other elements in the array.
7. If the first element is greater than the second element, the two elements are swapped using a temporary variable.
8. The sorted array is printed in ascending order using a for loop.

Program Output
$ javac Ascending _Order.java
$ java Ascending _Order 
 
Enter no. of elements you want in array:5
Enter all the elements:
4
3
2
6
1
Ascending Order:1,2,3,4,6

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.