I'm trying to sort an array that include 5 numbers(size=4) from largest to smallest, but I spend a long time and I don't know why my code don't take the first element to be sorted..
This is My code:
public class sortingS
{
public static void main(String []args)
{
int a[]={5,-2,10,14,-1};
for(int i=0; i<a.length; i++)
{
for(int j=0; j<i; j++)
{
if(a[i]>a[j+1])
{
int temp=a[j+1];
a[j+1]=a[i];
a[i]=temp;
}
}
}
for(int i=0; i<a.length; i++){
System.out.println(a[i]);
}
}
}
and this is the output:
5 14 10 -1 -2