Java:Sorting an array
//This program will take some random values and sort them in ascending order
import static java.lang.Math.abs;
import java.util.*;
import java.util.*;
public class SortingAnArray {
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int n;
n=obj.nextInt();
int a[]=new int[n];
Random ob=new Random();
for(int i=0;i<n;i++)
{
a[i]=ob.nextInt()%9;
if(a[i]<0)
a[i]=abs(a[i]);
}
System.out.println("Elements before sorting");
for(int i:a)
{
System.out.printf("%d ",i);
}
System.out.println();
Arrays.sort(a);
System.out.println("Elements after sorting");
for(int i:a)
{
System.out.printf("%d ",i);
}
System.out.println();
}
}
No comments:
Post a Comment