Skip to content
Related Articles

Related Articles

Java Program to Sort the Elements of an Array in Ascending Order
  • Last Updated : 16 Nov, 2020

Sort the given array in ascending order such that elements will be arranged from smallest to largest.

Original Array :

-5-981213

Array after sorting

-9-513812

Elements are sorted in such a way that the smallest element will appear on the extreme left which in this case is -9. The largest element will appear on extreme right which in this case is 12.

Approach #1: Bubble sort (using For loop).



  1. Compare adjacent elements with each other.
  2. Use nested for loop to keep track.
  3. Swap the elements if the first element is greater than the second element.

Below is the implementation of the above approach.

Java




// Java Program to sort the elements
// of an array in ascending order
  
class GFG {
    public static void printArray(int[] array)
    {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
    public static void sortArray(int[] array)
    {
        int temporary = 0;
  
        // Sort the array 'arr' elements in ascending order
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] > array[j]) {
                    temporary = array[i];
                    array[i] = array[j];
                    array[j] = temporary;
                }
            }
        }
        // Displaying elements of array after sorting
        System.out.println(
                "Elements of array sorted in ascending order: ");
        printArray(array);
    }
    public static void main(String[] args)
    {
        // Initialize array
        // The array contains 6 elements.
        int[] array = new int[] { -5, -9, 8, 12, 1, 3 };
  
        // Displaying elements of original array
        System.out.print("Elements of original array: ");
  
        // Call printArray method
        printArray(array);
  
        // Call sortArray method
        sortArray(array);
    }
}

Output:

Elements of original array: -5 -9 8 12 1 3 
Elements of array sorted in ascending order: 
-9 -5 1 3 8 12 

Time Complexity: O(n^2), where n is the length of an array.

Approach #2: Arrays.sort()

The sort() method is a java.util.Arrays class method used to sort array elements.  It by default sorts of array elements in ascending order.

Syntax:

Arrays.sort(arrayName);

Implementation

Java




// Java Program to sort the elements of an array
// in ascending order by inbuilt methods.
  
import java.util.Arrays;
  
public class SortExample {
    public static void main(String[] args)
    {
        // Initialize array
        // The array contains 6 elements.
        int[] array = new int[] { -5, -9, 8, 12, 1, 3 };
  
        // Displaying elements of original array
        System.out.print("Elements of original array: ");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
  
        // use Arrays.sort() method to sort array
        // elements in ascending order.
        Arrays.sort(array);
        System.out.println();
  
        // Displaying elements of array after sorting
        System.out.println(
            "Elements of array sorted in ascending order : "
            + Arrays.toString(array));
    }
}

Output:

Elements of original array: -5 -9 8 12 1 3 
Elements of array sorted in ascending order :
[-9, -5, 1, 3, 8, 12]

Time Complexity: O(n log(n)), where n is the size of an array.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :