Java 插入排序不起作用

huangapple go评论70阅读模式
英文:

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 &lt; x.length; ++i) {
		int j = 0;
		while (y[j] &lt; x[i] &amp;&amp; j &lt; i) ++j;
		for (int k = i-1; k &gt;= 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&lt;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 &lt; x.length; ++i) {
        int j = 0;
        while (y[j] &lt; x[i] &amp;&amp; j &lt; i) ++j;
        for (int k = i-1; k &gt;= 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(&quot;Befor sorting&quot;);
    for(int k=0; k&lt;size.length; ++k ) {
        size[k]=(int)(Math.random()*20);
        System.out.print(size[k]);
        System.out.print(&quot; &quot;);
    }
    size = sort(size);

    System.out.println(&quot;\nAfter sorting&quot;);
    for(int i = 0; i&lt;size.length; i++){
        System.out.print(size[i]);
        System.out.print(&quot; &quot;);
    }
}}

答案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&lt;size.length; ++k ) {
    
    

 System.out.print(size[k] + &quot; &quot; );
}

huangapple
  • 本文由 发表于 2020年8月28日 22:45:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63636035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定