package com.iotek.Text;
public class botter {
/**
* 冒泡排序 方法1
*/
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<len;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<len;i++) {
System.out.print(numb[i]+"\t");
}
}
}
//编译结果:
package com.iotek.Text;
public class botter {
/**
* 冒泡排序 方法2
*/
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<leng;i++) {
System.out.print(numb[i]+"\t");
}
System.out.println("\n");
swap(numb);
System.out.println("数组排序后:");
for(int i=0;i<leng;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 com.iotek.Text;
public class botter {
/**
* 冒泡排序 方法3
* */
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<len;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<len;i++) {
System.out.print(numb[i]+"\t");
}
}
}