啊。。。。排序的一个问题,理解不能啊。。。。
java吧
全部回复
仅看楼主
level 7
abc7585 楼主
大家觉得这两个输出分别会是什么样的呢?他们为什么不一样啊。。。。
2012年12月17日 22点12分 1
level 7
abc7585 楼主
谁帮忙看下。。。多谢[88]
2012年12月18日 02点12分 2
level 9
一个是升序输出 一个正常输出。。。。为何要用 j?..直接用a[i+1]不就行么。。。
2012年12月18日 02点12分 3
呃。。。我的问题主要在那两个输出上。。。为什么不一样啊。。。想不明白。。。
2012年12月18日 04点12分
level 4
晚了!你这是典型的冒泡排序!应该
正确的
代码是:
public static void main(String[] args) {
int[] a = { 3, 5, 1, 9, 7, 2, 0 };
for (int i = 0; i < a.length; i++) {
int temp = a[i];
int count = 0;
for (int j = i + 1; j < a.length; j++) {
if (temp > a[j]) {
count = j;
temp = a[j];
a[count] = a[i];
a[i] = temp;
}}
System.out.println(a[i]);
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}}
其中可以省掉count直接代码是:
public static void main(String[] args) {
int[] a = { 3, 5, 1, 9, 7, 2, 0 };
for (int i = 0; i < a.length; i++) {
int temp = a[i];
for (int j = i + 1; j < a.length; j++) {
if (temp > a[j]) {
temp = a[j];
a[j] = a[i];
a[i] = temp;
}}
System.out.println(a[i]);
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}}
2012年12月18日 03点12分 4
level 7
楼主自己可以测试下啊。。
2012年12月18日 09点12分 5
level 7
上面程序的结果,楼主的自己看看吧
2012年12月18日 09点12分 6
1