10个元素的一维数组如果使用冒泡排序进行,需要几次冒泡循环才能完成排序 为什么 谢谢!

zxk123ha 2010-04-22 05:27:00
10个元素的一维数组如果使用冒泡排序进行,需要几次冒泡循环才能完成排序 为什么 谢谢!
...全文
1584 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxk123ha 2010-04-22
  • 打赏
  • 举报
回复
明白了 楼上各位 感激不尽
ineedaname 2010-04-22
  • 打赏
  • 举报
回复
正解为45次,里面的循环会比外面-1。所以是1+2+3......+9
ineedaname 2010-04-22
  • 打赏
  • 举报
回复
循环次数
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+"次!");
}
欢乐极客 2010-04-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ineedaname 的回复:]

看if那段,自己想逻辑。
Java code

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++……
[/Quote]

外层循环10次,内层n-i次。。。

所以是10+9+8+7+6+5.....+1 = 55次循环。。。

楼上的注意,lz问的是循环次数,不是比较次数!
luozhangwen 2010-04-22
  • 打赏
  • 举报
回复
冒泡排序好慢的. 还不如用封装好了的排序
ineedaname 2010-04-22
  • 打赏
  • 举报
回复
看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+"次!");
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧