62,621
社区成员
发帖
与我相关
我的任务
分享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] + ",");
}
}
}
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]
哥们,仔细一点嘛
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;了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;
}
}
}
for (j = i - 1; j > 0; j--) {中的 j>0 改为 j>=0 就ok了;for (j = i - 1; j > 0; j--) { if (temp < ary[j]) { ary[j + 1] = ary[j]; } else { break; }