java 排序 为什么不对

来源:百度知道 编辑:UC知道 时间:2024/05/13 08:27:21
public class Tyou {
public static void main(String []args)
{
int []a4={100,2,4,5,6,3,3,5,7,33,3,65,3,45};
for(int i=0;i<a4.length-1;i++)
{
int temp;
//System.out.println(a4[i]);
if(a4[i]>a4[i+1])
{
temp=a4[i];
a4[i]=a4[i+1];
a4[i+1]=temp;
}
}
for(int i=0;i<a4.length;i++)
{
System.out.println(a4[i]);
}

}
}

应该这么写。。
public class Tyou{
public static void main(String []args)
{
int []a4={100,2,4,5,6,3,3,5,7,33,3,65,3,45};
for(int i=0;i<a4.length-1;i++)
{
for(int j=i+1;j<a4.length;j++)
{
int temp;
if(a4[i]>a4[j])
{
temp=a4[i];
a4[i]=a4[j];
a4[j]=temp;
}
}
}

for(int i=0;i<a4.length;i++)
{
System.out.println(a4[i]);
}
}
}