英文:
Java Insertion Sort doesnt sort
问题
public class Sort {
public static int[] sort(int[] x) {
int[] y = new int[x.length];
for (int i = 0; i < x.length; ++i) {
int j = 0;
while (y[j] < x[i] && j < i) ++j;
for (int k = i - 1; k >= j; --k) y[k + 1] = y[k];
y[j] = x[i];
}
return y;
}
public static void main(String[] args) {
int[] size = new int[10];
for (int k = 0; k < size.length; ++k) {
size[k] = (int)(Math.random() * 20);
System.out.println(size[k]);
}
System.out.println(Arrays.toString(sort(size))); // Added Arrays.toString() to print sorted array
}}
英文:
I have written a java sorting algorithm by insertion, the code compiles but doesnt sort:(. If anyone could point out any flaws, Id be very thankful, Java doesnt...
public class Sort {
public static int[] sort(int[] x) {
int[] y = new int[x.length];
for(int i = 0; i < x.length; ++i) {
int j = 0;
while (y[j] < x[i] && j < i) ++j;
for (int k = i-1; k >= j; --k) y[k+1] = y[k];
y[j]=x[i];
}
return y;
}
public static void main(String[] args) {
int[] size = new int[10];
for(int k=0; k<size.length; ++k ) {
size[k]=(int)(Math.random()*20);
System.out.println(size[k]);
}
System.out.println(sort(size));
}}
答案1
得分: 2
[I@39ed3c8d
是在对整型数组调用 toString()
时返回的结果,然后您使用 System.out.println
进行打印。
您可能希望改为 System.out.println(Arrays.toString(sort(size)));
。
英文:
[I@39ed3c8d
is returned by invoking toString()
on an int array, which you then print with System.out.println
.
You presumably want System.out.println(Arrays.toString(sort(size)));
instead.
答案2
得分: 0
你正在打印大小的引用,即 [I@39ed3c8d
。
这应该能帮助你理解:
public class Sort {
public static int[] sort(int[] x) {
int[] y = new int[x.length];
for(int i = 0; i < x.length; ++i) {
int j = 0;
while (y[j] < x[i] && j < i) ++j;
for (int k = i-1; k >= j; --k) y[k+1] = y[k];
y[j]=x[i];
}
return y;
}
public static void main(String[] args) {
int[] size = new int[10];
System.out.println("Befor sorting");
for(int k=0; k<size.length; ++k ) {
size[k]=(int)(Math.random()*20);
System.out.print(size[k]);
System.out.print(" ");
}
size = sort(size);
System.out.println("\nAfter sorting");
for(int i = 0; i<size.length; i++){
System.out.print(size[i]);
System.out.print(" ");
}
}}
英文:
You are printing the reference of size i.e., [I@39ed3c8d
.
This should help you to understand:
public class Sort {
public static int[] sort(int[] x) {
int[] y = new int[x.length];
for(int i = 0; i < x.length; ++i) {
int j = 0;
while (y[j] < x[i] && j < i) ++j;
for (int k = i-1; k >= j; --k) y[k+1] = y[k];
y[j]=x[i];
}
return y;
}
public static void main(String[] args) {
int[] size = new int[10];
System.out.println("Befor sorting");
for(int k=0; k<size.length; ++k ) {
size[k]=(int)(Math.random()*20);
System.out.print(size[k]);
System.out.print(" ");
}
size = sort(size);
System.out.println("\nAfter sorting");
for(int i = 0; i<size.length; i++){
System.out.print(size[i]);
System.out.print(" ");
}
}}
答案3
得分: 0
以下是翻译好的内容:
那个随机数是结果:
System.out.println(sort(size));
请改为这样做:
size = sort(size);
for (int k=0; k<size.length; ++k ) {
System.out.print(size[k] + " ");
}
英文:
That random is result of:
System.out.println(sort(size));
Do this instead:
size = sort(size);
for(int k=0; k<size.length; ++k ) {
System.out.print(size[k] + " " );
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论