Original list to sorted list

67 Views Asked by At

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);
 
    }
}
2

There are 2 best solutions below

2
Wahlstrommm On BEST ANSWER

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.

0
Vishal Kriplani On

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);