10个元素的一维数组如果使用冒泡排序进行,需要几次冒泡循环才能完成排序 为什么 谢谢! 10个元素的一维数组如果使用冒泡排序进行,需要几次冒泡循环才能完成排序 为什么 谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看if那段,自己想逻辑。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+"次!"); } 外层循环10次,内层n-i次所以是10+9+8+7+6+5.....+1 = 55次循环楼上的注意,lz问的是循环次数,不是比较次数! 循环次数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+"次!"); } 文件输入流 如何打印? 图片加载类(GUI) 怎么知道DB里面有多少table 我的jBuilder9.0为什么不能创建Web应用程序? 哪里下载 lowagie 包? 对包不甚理解,我以为......望指点,先谢 一个小问题不知怎处理 我是新手,各位大虾,请问学习JAVA有没有什么好用的书!!谢谢。有分挣哦! 一个简单的问题? 在Java中写文本文件,怎样换行? java中label的事件怎么处理?? 求两数之各
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+"次!");
}
外层循环10次,内层n-i次所以是10+9+8+7+6+5.....+1 = 55次循环楼上的注意,lz问的是循环次数,不是比较次数!
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+"次!");
}