Simple java program for sorting numbers in ascending order – Java Program to Sort the Elements of an Array in Ascending Order

Simple java program for sorting numbers in ascending order: In the previous article, we have seen Java Program to Take Input and Print an Array of String

In this article we are going to see how we can sort an Array in Ascending order in Java.

Java Program to Sort the Elements of an Array in Ascending Order

Ascending order in java: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to Sort the Elements of an Array in Ascending Order.

Method-1: Sort an array in increasing order using an user defined function

Approach:

  • Ask the user to input the size and store it.
  • Create an array of the specified size.
  • Print the array elements.
  • Sort the elements using the user-defined function sortArr( ).
  • Print the sorted array elements.

Program:

import java.util.Scanner;
import java.util.Arrays;
public class array
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Asking the user for array size
        System.out.println("Enter the array size : ");
        int size = scan.nextInt();

        // Creating the array
        int arr[] = new int[size];

        System.out.println("Enter the array elements : ");
        // Takes the elements as input from the user
        for(int i = 0;i<size;i++)
        {
            arr[i] = scan.nextInt();
        }

        // Prints the array before and after sorting
        System.out.println("The array elements are : "+Arrays.toString(arr));
        sortArr(arr,size);
        System.out.println("The array elements after sorting in ascending order are : "+Arrays.toString(arr));
    }
    
    // Function to sort the array
    static void sortArr(int arr[],int size)
    {
        int temp;
        // Uses Bubble Sort to sort the array
        for (int i = 0; i < size; i++) {
            // Compares and replaces the element with all the remaining elements in the array
            for (int j = i+1; j < size; j++) {     
                if(arr[i] > arr[j]) {    
                    temp = arr[i];    
                    arr[i] = arr[j];    
                    arr[j] = temp;    
                }     
            }     
        }    
    }
}

Output:

Enter the array size : 6
Enter the array elements : 6 1 5 3 4 2
The array elements are : [6, 1, 5, 3, 4, 2]
The array elements after sorting in ascending order are : [1, 2, 3, 4, 5, 6]

Method-2: Sorting an Array in increasing order Using Arrays.sort function

Approach:

  • Ask the user to input the size and store it.
  • Create an array of the specified size.
  • Print the array elements.
  • Sort the elements using the Arrays.sort function.
  • Print the sorted array elements.

Program:

import java.util.Scanner;
import java.util.Arrays;
public class array
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Asking the user for array size
        System.out.println("Enter the array size : ");
        int size = scan.nextInt();

        // Creating the array
        int arr[] = new int[size];

        System.out.println("Enter the array elements : ");
        // Takes the elements as input from the user
        for(int i = 0;i<size;i++)
        {
            arr[i] = scan.nextInt();
        }

        // Prints the array before and after sorting
        System.out.println("The array elements are : "+Arrays.toString(arr));
        Arrays.sort(arr);
        System.out.println("The array elements after sorting in ascending order are : "+Arrays.toString(arr));
    }
}

Output:

Enter the array size : 6
Enter the array elements : 6 1 5 3 4 2
The array elements are : [6, 1, 5, 3, 4, 2]
The array elements after sorting in ascending order are : [1, 2, 3, 4, 5, 6]

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Related Java Programs: