62,621
社区成员
发帖
与我相关
我的任务
分享public class MaopaoSort {
static int[] array = {1,2,0,3,5,4};
public static void maopaoSort(int[] a)
{
//外层循环,是需要进行比较的轮数,一共进行5次即可
for(int i=0;i<a.length-1;i++)
{
boolean flag = false;
//内存循环,是每一轮中进行的两两比较
for(int j=0;j<a.length-i-1;j++)
{
if(a[j] > a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag = true;
}
}
System.out.println("第"+(i+1)+"轮排序后的数组为: "+Arrays.toString(a));
if(flag == false)
{
System.out.println("本轮中的两两比较未发生元素互换,排序已经完成啦");
return;
}
}
}