求冒泡排序的java代码

2022-07-29 教育 81阅读
方法一: package basic.javastu; public class NumberTest {
public static void main(String[] args) { // TODO Auto-generated method stub

int[] numb=new int[]{3,42,57,1,32,24};

int len=numb.length;

int i,j;

int temp;

System.out.println("排序前的数组各个值:");

for(i=0;i
{

System.out.print(numb[i]+"\t");

}

System.out.println("\n");

for(i=1;i<=len;i++)

{

for(j=len-1;j>=1;j--)

{

if(numb[j]>numb[j-1])

{

temp=numb[j];

numb[j]=numb[j-1];

numb[j-1]=temp;

}

}

}

System.out.println("排序后的数组各个值:");

for(i=0;i
{

System.out.print(numb[i]+"\t");

}

}
}
方法二: package basic.javastu; public class NumberTest2 {
public static void main(String[] args) { // TODO Auto-generated method stub

int[] numb=new int[]{3,42,57,1,32,24};

int leng=numb.length;

System.out.println("排序前的数组各个值:");

for(int i=0;i
{

System.out.print(numb[i]+"\t");

}

System.out.println("\n");

swap(numb);

System.out.println("数组排序后:"); for(int i=0;i {
System.out.print(numb[i]+"\t");
} }

private static int[] swap(int[] numb) { int n2[]=numb; int len=n2.length; int i,j; int temp; for(i=1;i<=len;i++)
{
for(j=len-1;j>=1;j--)
{
if(n2[j]>n2[j-1])
{
temp=n2[j];
n2[j]=n2[j-1];
n2[j-1]=temp;
}
}
} return n2; } }

方法三: package basic.javastu; public class NumberTest3 {
public static void main(String[] args) { // TODO Auto-generated method stub

int[] numb=new int[]{3,42,57,1,32,24};

int leng=numb.length;

System.out.println("排序前的数组各个值:");

for(int i=0;i
{

System.out.print(numb[i]+"\t");

}

System.out.println("\n");

swap(numb);

System.out.println("数组排序后:"); for(int i=0;i {
System.out.print(numb[i]+"\t");
} }

private static void swap(int[] numb) { int len=numb.length; int i,j; int temp; for(i=1;i<=len;i++)
{
for(j=len-1;j>=1;j--)
{
if(numb[j]>numb[j-1])
{
temp=numb[j];
numb[j]=numb[j-1];
numb[j-1]=temp;
}
}
} } }
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com