Signup/Sign In
LAST UPDATED: MARCH 5, 2021

Java Program To Sort an Array in Ascending Order

In this tutorial, we will learn how to sort the elements of an array in ascending order. Sorting refers to arranging data in order either alphabetically or numerically. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: Enter the Array Elements: 5 4 6 7 8 2 3 1 9

Output: The Sorted Array in Ascending Order: 1 2 3 4 5 6 7 8 9

Approach 1: Using Loops

Approach 2: Using Arrays.sort()

Let us look at each of these methods separately.

Program 1: Sort the Elements of an Array in Ascending Order

In this approach, we will see how to use loops to sort an array in ascending order. We can sort the array using manual sorting like using for loops. What we can do is use two for loops, one to traverse the array from the starting and another for loop inside the outer one to traverse the next element. In the body, we can compare the adjacent elements and swap if they are not in order. We can also use a temporary variable for the swapping of elements.

Algorithm

  1. Start
  2. Declare an array
  3. Ask the user to initialize the array
  4. Declare a temporary variable to store the elements while swapping.
  5. Use two for loops for the same.
  6. Use the first for loop to hold the elements and traverse through all the elements.
  7. Use the second for loop to compare with the remaining elements.
  8. Sort the elements by comparing and swapping.
  9. Display the updated array.
  10. Stop

Below is the code for the same.

The below program demonstrates how to sort an array in ascending order using loops.

/*Java Program to Sort an Array in Ascending Order*/
import java.util.Arrays;
import java.util.Scanner;
import java.util.Collections;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n;    //Array Size Declaration
        System.out.println("Enter the number of elements :");
        n=sc.nextInt();    //Array Size Initialization
        
        Integer arr[]=new Integer[n];    //Array Declaration
        System.out.println("Enter the elements of the array :");
        for(int i=0;i<n;i++)     //Array Initialization
        {
            arr[i]=sc.nextInt();
        }
        
        int temp = 0;    //Temporary variable to store the element
        
         for (int i = 0; i < arr.length; i++)   //Holds each Array element
         {     
            for (int j = i+1; j < arr.length; j++)    //compares with remaining Array elements
            {     
               if(arr[i] > arr[j]) //Compare and swap
               {    
                   temp = arr[i];    
                   arr[i] = arr[j];    
                   arr[j] = temp;    
               }     
            }     
        }    
          
        System.out.println();    
            
        //Displaying elements of array after sorting    
        System.out.println("Elements of array sorted in ascending order: ");    
        for (int i = 0; i < arr.length; i++) 
        {     
            System.out.print(arr[i] + " ");    
        }    
    }
}


Enter the number of elements :10
Enter the elements of the array :
6 7 8 4 3 5 1 8 9 7
Elements of array sorted in ascending order:
1 3 4 5 6 7 7 8 8 9

Program 2: Sort the Elements of an Array in Ascending Order

In this approach, we will see how to use Arrays.sort() to sort an array in ascending order. The Arrays class of ‘java.util’ package provides the sort method that takes an array as an argument and sorts the array. This is a direct sorting method and the array can be sorted in ascending order with just one method call.

Algorithm:

  1. Start
  2. Declare an array
  3. Ask the user to initialize the array
  4. Use the Arrays.sort() to sort the elements in ascending order.
  5. Print the updated array.
  6. Stop

Below is the code for the same.

The below program demonstrates how to sort an array in ascending order using Arrays.sort().

/*Java Program to Sort an Array in Ascending Order*/

import java.util.Arrays;
import java.util.Scanner;

public class AscendingOrder
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n;    //Array Size Declaration
        System.out.println("Enter the number of elements :");
        n=sc.nextInt();    //Array Size Initialization
        
        int arr[]=new int[n];    //Array Declaration
        System.out.println("Enter the elements of the array :");
        for(int i=0;i<n;i++)     //Array Initialization
        {
            arr[i]=sc.nextInt();
        }
        
        Arrays.sort(arr);    //Sorts the Array in Ascending Order
 
        System.out.printf("Sorted arr[] : %s",
                          Arrays.toString(arr));   //Prints the sorted Array
    }
}


Enter the number of elements : 10
Enter the elements of the array : 1 4 2 5 6 8 2 3 9 8
Sorted arr[] : [1, 2, 2, 3, 4, 5, 6, 8, 8, 9]



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.