- 第1次推导
public class test {
public static void main(String[] args) {
int[] ints = {6, 5, 9, 5};
int tmp;
if(ints[0]>ints[1]){
tmp = ints[0];
ints[0] = ints[1];
ints[1] = tmp;
}
if(ints[1]>ints[2]){
tmp = ints[1];
ints[1] = ints[2];
ints[2] = tmp;
}
if(ints[2]>ints[3]){
tmp = ints[2];
ints[2] = ints[3];
ints[3] = tmp;
}
System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);
if(ints[0]>ints[1]){
tmp = ints[0];
ints[0] = ints[1];
ints[1] = tmp;
}
if(ints[1]>ints[2]){
tmp = ints[1];
ints[1] = ints[2];
ints[2] = tmp;
}
System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);
if(ints[0]>ints[1]){
tmp = ints[0];
ints[0] = ints[1];
ints[1] = tmp;
}
System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);
}
}
- 第2次推导
public class test {
public static void main(String[] args) {
int[] ints = {6, 5, 9, 5};
int tmp;
int i=0;
int j=1;
for(int a = 3; a>0; a--){
if(ints[i]>ints[j]){
tmp = ints[i];
ints[i] = ints[j];
ints[j] = tmp;
}
i++;
j++;
}
System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);
i=0;
j=1;
for(int a = 2; a>0; a--){
if(ints[i]>ints[j]){
tmp = ints[i];
ints[i] = ints[j];
ints[j] = tmp;
}
i++;
j++;
}
System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);
i=0;
j=1;
for(int a = 1; a>0; a--){
if(ints[i]>ints[j]){
tmp = ints[i];
ints[i] = ints[j];
ints[j] = tmp;
}
i++;
j++;
}
System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);
}
}
- 最终完善
public class test {
public static void main(String[] args) {
int[] ints = {6, 5, 9, 2, 5, 6,7,3};
for(int b = ints.length-1; b>0; b--){
int tmp;
int i=0;
for(int a = b; a>0; a--){
if(ints[i]>ints[i+1]){
tmp = ints[i];
ints[i] = ints[i+1];
ints[i+1] = tmp;
}
i++;
}
}
for (int anInt : ints) {
System.out.print(anInt + ",");
}
}
}
标签:tmp,数组,int,System,冒泡排序,++,ints,out
From: https://www.cnblogs.com/dogleftover/p/17736992.html