Java Program to Sort array elements in ascending order

Java Program to Sort array elements in ascending order

There are multiple ways and algorithms to sort the array in ascending order. In this tutorial we will see some easy ways of sorting an array in ascending order.

For example:

Suppose we have an unsorted array arr = [3, 1, 9, 4, 2, 1, 5]

After sorting in ascending order output will be

[1, 1, 2, 3, 4, 5, 9]

Program 1: Sorting array using Arrays.sort() method

import java.util.*;
public class Main{
    public static void main (String[]args){
        Scanner sc = new Scanner (System.in);
    
        //taking array size and array element as input
        System.out.println ("Enter the size of array: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < size; i++){
	        System.out.print("Please give value for index " + i + " : ");
	        arr[i] = sc.nextInt();
        }
        
        Arrays.sort(arr);
        System.out.println ("Array sorted in ascending order is ");
        for (int i = 0; i < size; ++i)
            System.out.print(arr[i] + " ");
    }
}

Output

Enter the size of array: 
5
Please give value for index 0 : 3
Please give value for index 1 : 1
Please give value for index 2 : 4
Please give value for index 3 : 2
Please give value for index 4 : 3
Array sorted in ascending order is 
1 2 3 3 4 

Program 2: Sorting array in ascending order using Insertion Sort

import java.util.*;
public class Main{
    public static void main (String[]args){
        Scanner sc = new Scanner (System.in);
    
        //taking array size and array element as input
        System.out.println ("Enter the size of array1: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < size; i++){
	        System.out.print("Please give value for index " + i + " : ");
	        arr[i] = sc.nextInt();
        }
        
        //insertion sorting logic
        for (int i = 1; i < size; ++i) {
            int key = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
        }
        for (int i = 0; i < size; ++i)
            System.out.print(arr[i] + " ");
    }
}

Output

Enter the size of array: 
5
Please give value for index 0 : 3
Please give value for index 1 : 1
Please give value for index 2 : 2
Please give value for index 3 : 4
Please give value for index 4 : 1
Array sorted in ascending order is 
1 1 2 3 4 

Program 3: Array Sorting in ascending order using Bubble sort

import java.util.*;
public class Main{
    public static void main (String[]args){
        Scanner sc = new Scanner (System.in);
    
        //taking array size and array element as input
        System.out.println ("Enter the size of array: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < size; i++){
	        System.out.print("Please give value for index " + i + " : ");
	        arr[i] = sc.nextInt();
        }
        
        //bubble sorting logic
        int temp = 0;  
        for(int i=0; i < size; i++){  
            for(int j=1; j < (size-i); j++){  
                if(arr[j-1] > arr[j]){  
                    temp = arr[j-1];  
                    arr[j-1] = arr[j];  
                    arr[j] = temp;  
                }  
            }  
        }  
        
        System.out.println ("Array sorted in ascending order is ");
        for (int i = 0; i < size; ++i)
            System.out.print(arr[i] + " ");
    }
}

Output

Enter the size of array: 
6
Please give value for index 0 : 3
Please give value for index 1 : 1
Please give value for index 2 : 4
Please give value for index 3 : 2
Please give value for index 4 : 5
Please give value for index 5 : 3
Array sorted in ascending order is 
1 2 3 3 4 5