Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Original list to sorted list

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading