Explore Java Hacks and Tutorials
Be Social. Be Updated.

Sort Array in Increasing Order in Java


An example code on sorting an array of (any numeric datatype) in increasing order in a single statement.

Sorting Array in Increasing Order




import java.util.*;
class SortIntArray
{


public static void main(String args[])
{


// Create Scanner object
Scanner s=new Scanner(System.in);


// Take no.of elements in array
int n=s.nextInt();


// Create an array of n elements
int[] a=new int[n];


// Loop till end of array
for(int i=0;i<n;i++)
{


// Read int from user and store
a[i]=s.nextInt();


}


Arrays.sort(a);

// Say user that is sorted array
System.out.println("Sorted array");


// 'i' represents every element in array 'a'
for(int i:a)
{
System.out.println(i);
}


}


}


Output


5
3
5
6
8
7
Sorted array
3
5
6
7
8


There in fact no analysis that can be done, the logic is written by the developers of the class java.util.Array therefore removing the burden for the app developers to do this. That's why Java developers love to code in Java! Happy java coding :)



Posted by at on

Tags: Arrays Java Utilities