62,614
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
int[] tempNums = { 1, 2, 3, 4, 5, 123, 99, 100, 9, 11 };
int count = 0;
for (int i = 0; i < tempNums.length; i++) {
System.out.println("i为"+i+"时");
System.out.print("j为");
for (int j = 0; j < tempNums.length - i - 1; ++j) {
System.out.print(j+",");
count++;
if (tempNums[j] > tempNums[j + 1]) {
int temp = tempNums[j];
tempNums[j] = tempNums[j + 1];
tempNums[j + 1] = temp;
}
}
System.out.println("\n-------------------");
}
System.out.println("循环了"+count+"次!");
}
public static void main(String[] args) {
int[] tempNums = { 1, 2, 3, 4, 5, 6, 7, 10, 9, 11 };
int count = 0;
for (int i = 0; i < tempNums.length; i++) {
for (int j = 0; j < tempNums.length - i - 1; ++j) {
if (tempNums[j] > tempNums[j + 1]) {
count++;
int temp = tempNums[j];
tempNums[j] = tempNums[j + 1];
tempNums[j + 1] = temp;
}
}
}
System.out.println("排序了"+count+"次!");
}