原始列表到排序后的列表

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

Original list to sorted list

问题

以下是您要翻译的代码部分:

import java.util.ArrayList;
public class InsertionSort<E extends Comparable<E>> {
  
    public void insertionSort(ArrayList<E> list) {
        int n = list.size();
        for (int i = 1; i < n; i++) {
            E key = list.get(i);
            int j = i - 1;
            while (j >= 0 && list.get(j).compareTo(key) > 0) {
                list.set(j + 1, list.get(j));
                j--;
            }
            list.set(j + 1, key);
        }
    }

    public static void main(String[] args) {
        int[] test1 = {3, 5, 2, 4, 1, 8, 7, 6, 9};
        double[] test2 = {1.99, 2.05, 9.01, 6.49, 3.14, 5.55};
        char[] test3 =  "algorithm".toCharArray();
        ArrayList<Integer> list1 = new ArrayList<>();
        for (int num : test1) {
            list1.add(num);
        }
        InsertionSort<Integer> integerSorter = new InsertionSort<>();
        integerSorter.insertionSort(list1);
        System.out.println("Test Example 1");
        System.out.println("Original List: " + list1);
        System.out.println("Sorted List: " + list1);
  
        ArrayList<Double> list2 = new ArrayList<>();
        for (double num : test2) {
            list2.add(num);
        }
        InsertionSort<Double> doubleSorter = new InsertionSort<>();
        doubleSorter.insertionSort(list2);
        System.out.println("\nTest Example 2");
        System.out.println("Original List: " + list2);
        System.out.println("Sorted List: " + list2);

        ArrayList<Character> list3 = new ArrayList<>();
        for (char ch : test3) {
            list3.add(ch);
        }
        InsertionSort<Character> charSorter = new InsertionSort<>();
        charSorter.insertionSort(list3);
        System.out.println("\nTest Example 3");
        System.out.println("Original List: " + list3);
        System.out.println("Sorted List: " + list3);
    }
}

请注意,我已将HTML编码的字符(例如<和>)还原为正常的Java代码。

英文:

So I was trying to practice implementing the insertion sort algorithm using the integer, double, and character values, I was able to sort my list but my original list is displaying me the wrong result. Supposed to display the data values from test1, test2, test3 in my code. Can anyone show me what am I missing because I have been stuck for quite sometime?

import java.util.ArrayList;
public class InsertionSort&lt;E extends Comparable&lt;E&gt;&gt; {
public void insertionSort(ArrayList&lt;E&gt; list) {
int n = list.size();
for (int i = 1; i &lt; n; i++) {
E key = list.get(i);
int j = i - 1;
while (j &gt;= 0 &amp;&amp; list.get(j).compareTo(key) &gt; 0) {
list.set(j + 1, list.get(j));
j--;
}
list.set(j + 1, key);
}
}
public static void main(String[] args) {
int[] test1 = {3, 5, 2, 4, 1, 8, 7, 6, 9};
double[] test2 = {1.99, 2.05, 9.01, 6.49, 3.14, 5.55};
char[] test3 =  &quot;algorithm&quot;.toCharArray();
ArrayList&lt;Integer&gt; list1 = new ArrayList&lt;&gt;();
for (int num : test1) {
list1.add(num);
}
InsertionSort&lt;Integer&gt; integerSorter = new InsertionSort&lt;&gt;();
integerSorter.insertionSort(list1);
System.out.println(&quot;Test Example 1&quot;);
System.out.println(&quot;Original List: &quot; + list1);
System.out.println(&quot;Sorted List: &quot; + list1);
ArrayList&lt;Double&gt; list2 = new ArrayList&lt;&gt;();
for (double num : test2) {
list2.add(num);
}
InsertionSort&lt;Double&gt; doubleSorter = new InsertionSort&lt;&gt;();
doubleSorter.insertionSort(list2);
System.out.println(&quot;\nTest Example 2&quot;);
System.out.println(&quot;Original List: &quot; + list2);
System.out.println(&quot;Sorted List: &quot; + list2);
ArrayList&lt;Character&gt; list3 = new ArrayList&lt;&gt;();
for (char ch : test3) {
list3.add(ch);
}
InsertionSort&lt;Character&gt; charSorter = new InsertionSort&lt;&gt;();
charSorter.insertionSort(list3);
System.out.println(&quot;\nTest Example 3&quot;);
System.out.println(&quot;Original List: &quot; + list3);
System.out.println(&quot;Sorted List: &quot; + list3);
}
}

答案1

得分: 2

你正在使用同一个列表来打印原始列表和排序后的列表。
所以你创建了 list1 作为原始列表。
之后你对 list1 进行了排序。
现在 list1 包含了已排序的项目。它不再保留原始项目的顺序。
现在你将 list1 作为原始和排序后的列表进行打印,但它们最终是相同的。

System.out.println("原始列表: " + list1);
System.out.println("排序后的列表: " + list1);
英文:

You are using same list to print the original list and sorted list.
So you created list1 as original list.
After that you sorted list1.
Now list1 contains the sorted items. It no longer contains the original order of items.
Now you are printing the list1 as original and sorted but they are the same in the end.

System.out.println(&quot;Original List: &quot; + list1);
System.out.println(&quot;Sorted List: &quot; + list1);

答案2

得分: 1

问题在于您同时将“original”和“sorted lists”都打印为相同的列表对象。

因此,在排序过程中修改列表会在原始列表和排序后的列表输出中都反映出这些修改。

因此,在对其进行排序之前,您可以创建列表的单独副本,然后将副本打印为原始列表。

所以通过创建原始列表的单独副本(list1Copylist2Copylist3Copy),

您可以在排序之前将它们打印为原始列表,同时在排序过程中修改原始列表(list1list2list3)。

英文:

The problem is that you are printing the same list object for both the original and sorted lists.

So when you modify the list during the sorting process the modifications are reflected in both the original and sorted list outputs.

So you can create a separate copy of the list before sorting it and then print the copy as the orignal list.

import java.util.ArrayList;
import java.util.Arrays;
public class InsertionSort&lt;E extends Comparable&lt;E&gt;&gt; {
public void insertionSort(ArrayList&lt;E&gt; list) {
int n = list.size();
for (int i = 1; i &lt; n; i++) {
E key = list.get(i);
int j = i - 1;
while (j &gt;= 0 &amp;&amp; list.get(j).compareTo(key) &gt; 0) {
list.set(j + 1, list.get(j));
j--;
}
list.set(j + 1, key);
}
}
public static void main(String[] args) {
int[] test1 = {3, 5, 2, 4, 1, 8, 7, 6, 9};
double[] test2 = {1.99, 2.05, 9.01, 6.49, 3.14, 5.55};
char[] test3 = &quot;algorithm&quot;.toCharArray();
ArrayList&lt;Integer&gt; list1 = new ArrayList&lt;&gt;();
for (int num : test1) {
list1.add(num);
}
InsertionSort&lt;Integer&gt; integerSorter = new InsertionSort&lt;&gt;();
ArrayList&lt;Integer&gt; list1Copy = new ArrayList&lt;&gt;(list1); // Create a copy of the original list
integerSorter.insertionSort(list1);
System.out.println(&quot;Test Example 1&quot;);
System.out.println(&quot;Original List: &quot; + list1Copy);
System.out.println(&quot;Sorted List: &quot; + list1);
ArrayList&lt;Double&gt; list2 = new ArrayList&lt;&gt;();
for (double num : test2) {
list2.add(num);
}
InsertionSort&lt;Double&gt; doubleSorter = new InsertionSort&lt;&gt;();
ArrayList&lt;Double&gt; list2Copy = new ArrayList&lt;&gt;(list2); // Create a copy of the original list
doubleSorter.insertionSort(list2);
System.out.println(&quot;\nTest Example 2&quot;);
System.out.println(&quot;Original List: &quot; + list2Copy);
System.out.println(&quot;Sorted List: &quot; + list2);
ArrayList&lt;Character&gt; list3 = new ArrayList&lt;&gt;();
for (char ch : test3) {
list3.add(ch);
}
InsertionSort&lt;Character&gt; charSorter = new InsertionSort&lt;&gt;();
ArrayList&lt;Character&gt; list3Copy = new ArrayList&lt;&gt;(list3); // Create a copy of the original list
charSorter.insertionSort(list3);
System.out.println(&quot;\nTest Example 3&quot;);
System.out.println(&quot;Original List: &quot; + list3Copy);
System.out.println(&quot;Sorted List: &quot; + list3);
}
}

So by creating separate copies of the original lists (list1Copy, list2Copy, list3Copy).

You can print them as the original lists before sorting, while the original lists (list1, list2, list3) are modified during the sorting process.

huangapple
  • 本文由 发表于 2023年6月5日 00:19:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401347.html
匿名

发表评论

匿名网友

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

确定