英文:
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<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);
}
}
答案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("Original List: " + list1);
System.out.println("Sorted List: " + list1);
答案2
得分: 1
问题在于您同时将“original”和“sorted lists”都打印为相同的列表对象。
因此,在排序过程中修改列表会在原始列表和排序后的列表输出中都反映出这些修改。
因此,在对其进行排序之前,您可以创建列表的单独副本,然后将副本打印为原始列表。
所以通过创建原始列表的单独副本(list1Copy
、list2Copy
、list3Copy
),
您可以在排序之前将它们打印为原始列表,同时在排序过程中修改原始列表(list1
、list2
、list3
)。
英文:
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<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<>();
ArrayList<Integer> list1Copy = new ArrayList<>(list1); // Create a copy of the original list
integerSorter.insertionSort(list1);
System.out.println("Test Example 1");
System.out.println("Original List: " + list1Copy);
System.out.println("Sorted List: " + list1);
ArrayList<Double> list2 = new ArrayList<>();
for (double num : test2) {
list2.add(num);
}
InsertionSort<Double> doubleSorter = new InsertionSort<>();
ArrayList<Double> list2Copy = new ArrayList<>(list2); // Create a copy of the original list
doubleSorter.insertionSort(list2);
System.out.println("\nTest Example 2");
System.out.println("Original List: " + list2Copy);
System.out.println("Sorted List: " + list2);
ArrayList<Character> list3 = new ArrayList<>();
for (char ch : test3) {
list3.add(ch);
}
InsertionSort<Character> charSorter = new InsertionSort<>();
ArrayList<Character> list3Copy = new ArrayList<>(list3); // Create a copy of the original list
charSorter.insertionSort(list3);
System.out.println("\nTest Example 3");
System.out.println("Original List: " + list3Copy);
System.out.println("Sorted List: " + 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论