Sort Array Elements in Ascending Order using Java

Programming is always a fun and creative task. The more creative you can think the better programmer you will become. However, programming isn’t always about solving problems sometimes there’s also a fun aspect to it which makes it way more satisfying. If you are a programmer then you might have already played with patterns, strings, etc multiple times. Well, it’s also like that you are already familiar with arrays and numbers, so what about sorting array elements in ascending order. So in today’s article, we will Sort Array Elements in Ascending Order using Java. With the help of this program, you will be able to sort an array with the smallest element at first and the largest one at the last. Practising these types of questions also helps to get an upper edge in Competitive Programming.

 

So open up your IDE and let’s get started real quick. Once you understand the logic practice this program on your own to make your brain work in a problem-solving way. This is a pretty easy question so you might not get asked in an interview but practising this question will make your grip more firm in the Java language.

 

Also Read: Print Mirror Lower Star Triangle in Java

 

What’s The Approach?

 

  • There are two ways by which we can sort array elements in ascending order, we’re going to look at the second method. The first method is the basic and slowest while the second method is extremely easy and blazing fast.

 

  • We will use the sort() method of java.util.Arrays class. This method takes input parameters as an array which we want to sort.

 

  • And at last, we will convert the sorted array into a string by using theArrays.toString() method and will print the same.

 

Java Program To Sort Array Elements in Ascending Order

 

Input Parameters in the array :

 

-5 -9 8 12 1 3

// 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 TechDecodeTutorials {

    // 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 the 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:

 

-9, -5, 1, 3, 8, 12

Ethix

I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published. Required fields are marked *