Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Problem statement: Sort the given array in ascending order such that elements will be arranged from smallest to largest. Consider an illustration below:

Let the original array be as follows: 

-5 -9 8 12 1 3

Array generated after sorting the above array is as follows: 

-9 -5 1 3 8 12
  • 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 the extreme right which in this case is 12.

Approaches:

  1. Using bubble sort(naive)
  2. Using sort() method of arrays class(optimal)

Approach 1: Using Bubble sort

Algorithm:

  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.

Example

Java




// Java Program to Sort Elements of an Array
// in Ascending Order
 
// Main class
class GFG {
 
    // Declaration global variable length
    static int length;
 
    // Method 1
    // To print the array
    public static void printArray(int[] array)
    {
        // Iterating using for loops
        for (int i = 0; i < length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
 
    // Method 2
    // To sort an array
    public static void sortArray(int[] array)
    {
        int temporary = 0;
 
        // Sort the array 'arr' elements in ascending order
        // using nested for loops
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < 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);
    }
 
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing custom array elements
        // The array contains 6 elements.
        int[] array = new int[] { -5, -9, 8, 12, 1, 3 };
 
        // Initialize length
        length = array.length;
 
        // 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: Using sort() method of Arrays class

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);

Parameters:  Array to be sorted 

Return Type: NA

Example

Java




// Java Program to sort the elements of an array
// in Ascending Order by Inbuilt Methods
 
// Importing Arrays class from java.util package
import java.util.Arrays;
 
// Main class
public class GFG {
 
    // Main driver method
    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] + " ");
        }
 
        // Using 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.



Last Updated : 06 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads