插入排序问题.

笑_长 2014-02-28 08:44:58
我写的这段插入排序代码哪里出错了呢?求帮助.
用for循环,不要改成while.谢谢~!

package test;

public class Insertion {

public static void main(String[] args) {
int[] ary = { 8, 3, 5, 1, 4, 2, 7 };
int i;
int j;
for (i = 1; i < ary.length; i++) {
int temp = ary[i];
for (j = i - 1; j > 0; j--) {
if (temp < ary[j]) {
ary[j + 1] = ary[j];
} else {
break;
}
}
ary[j + 1] = temp;
}
for (int k = 0; k < ary.length; k++) {
System.out.print(ary[k] + ",");
}

}

}


输出结果为:8,1,2,3,4,5,7,
...全文
123 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
代码间的舞者 2014-03-01
  • 打赏
  • 举报
回复
引用 2 楼 u012724379 的回复:
刚才手贱了,你把
 for (j = i - 1; j > 0; j--) {
中的 j>0 改为 j>=0 就ok了;
+1 不然,第一个数永远不会参与比较
-江沐风- 2014-03-01
  • 打赏
  • 举报
回复
引用 5 楼 u013316128 的回复:
[quote=引用 4 楼 u013316128 的回复:] [quote=引用 2 楼 u012724379 的回复:] 刚才手贱了,你把
 for (j = i - 1; j > 0; j--) {
中的 j>0 改为 j>=0 就ok了;
非常感谢,这个问题解决了.然后我把它包装了一下,包装之后输出却是有问题的,麻烦你看一看.
package test;

public class Test {

	public static void main(String[] args) {
		int[] ary = { 5, 8, 4, 1, 6, 4, 7 };
		InsertSortf is=new InsertSortf();
		is.sort(ary);
		for (int k = 0; k < ary.length; k++) {
			System.out.print(ary[k] + " ");
		}
	}

}
class  InsertSortf{
	public void sort(int[] ary){
		int j;
		for (int i = 1; i < ary.length; i++) {
			int temp = ary[i];
			for (j = i - 1; j >= 0; j--) {
				if (temp < ary[j]) {
					ary[j + 1] = ary[j];
				} else {
				}
			}
			ary[j + 1] = temp;
		}
	}
}
[/quote] 晕死,问题已经解决.忘记写 break;了[/quote] 哥们,仔细一点嘛
笑_长 2014-03-01
  • 打赏
  • 举报
回复
引用 4 楼 u013316128 的回复:
[quote=引用 2 楼 u012724379 的回复:] 刚才手贱了,你把
 for (j = i - 1; j > 0; j--) {
中的 j>0 改为 j>=0 就ok了;
非常感谢,这个问题解决了.然后我把它包装了一下,包装之后输出却是有问题的,麻烦你看一看.
package test;

public class Test {

	public static void main(String[] args) {
		int[] ary = { 5, 8, 4, 1, 6, 4, 7 };
		InsertSortf is=new InsertSortf();
		is.sort(ary);
		for (int k = 0; k < ary.length; k++) {
			System.out.print(ary[k] + " ");
		}
	}

}
class  InsertSortf{
	public void sort(int[] ary){
		int j;
		for (int i = 1; i < ary.length; i++) {
			int temp = ary[i];
			for (j = i - 1; j >= 0; j--) {
				if (temp < ary[j]) {
					ary[j + 1] = ary[j];
				} else {
				}
			}
			ary[j + 1] = temp;
		}
	}
}
[/quote] 晕死,问题已经解决.忘记写 break;了
笑_长 2014-03-01
  • 打赏
  • 举报
回复
引用 2 楼 u012724379 的回复:
刚才手贱了,你把
 for (j = i - 1; j > 0; j--) {
中的 j>0 改为 j>=0 就ok了;
非常感谢,这个问题解决了.然后我把它包装了一下,包装之后输出却是有问题的,麻烦你看一看.
package test;

public class Test {

	public static void main(String[] args) {
		int[] ary = { 5, 8, 4, 1, 6, 4, 7 };
		InsertSortf is=new InsertSortf();
		is.sort(ary);
		for (int k = 0; k < ary.length; k++) {
			System.out.print(ary[k] + " ");
		}
	}

}
class  InsertSortf{
	public void sort(int[] ary){
		int j;
		for (int i = 1; i < ary.length; i++) {
			int temp = ary[i];
			for (j = i - 1; j >= 0; j--) {
				if (temp < ary[j]) {
					ary[j + 1] = ary[j];
				} else {
				}
			}
			ary[j + 1] = temp;
		}
	}
}
-江沐风- 2014-02-28
  • 打赏
  • 举报
回复
刚才手贱了,你把
 for (j = i - 1; j > 0; j--) {
中的 j>0 改为 j>=0 就ok了;
-江沐风- 2014-02-28
  • 打赏
  • 举报
回复
for (j = i - 1; j > 0; j--) {                if (temp < ary[j]) {                    ary[j + 1] = ary[j];                } else {                    break;                }

62,621

社区成员

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

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