Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

19.9 I successfully wrote code for the scenario below. I would like someone from

ID: 3674581 • Letter: 1

Question

19.9

I successfully wrote code for the scenario below. I would like someone from your team to rewrite the code using a different approach. This will allow me to see how someone else may go about coding this program and ultimately coming out with the same results. Please code using Java and verify it will run in a Java IDE. My code can be found below. Thanks.

19.9 — Sort Arraylist

Please code using Java and verify it will run in a Java IDE.

Write the following method that sorts an ArrayList:

public static <E extends Comparable<E>>

void sort(ArrayList<E> list)

This is my code for the program. It runs successfully, no issues. Please re-code using a different approach so I can compare and contrast a different ways to write the same program.

import java.util.*;

class Sort
{
   public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<>();
       list.add(83);
       list.add(104);
       list.add(65);
       list.add(944);
       list.add(23);
       list.add(1);
       System.out.print("Integer list for sorted: ");
       System.out.println(list);
       sort(list);
       System.out.print("Sorted Integer list: ");
       System.out.println(list);
   }

public static <E extends Comparable<E>> void sort(ArrayList<E> list) {
       for (int i = 0; i < list.size() - 1; i++) {
           for (int j = i + 1; j < list.size(); j++) {
               if (list.get(i).compareTo(list.get(j)) > 0) {
                   E tmp = list.get(j);
                   list.set(j, list.get(i));
                   list.set(i, tmp);                 
               }
           }
       }
   }
}

Explanation / Answer

import java.util.*;

class Sort
{
   public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<>();
       list.add(83);
       list.add(104);
       list.add(65);
       list.add(944);
       list.add(23);
       list.add(1);
       System.out.print("Integer list for sorted: ");
       System.out.println(list);
       sort(list);
       System.out.print("Sorted Integer list: ");
       System.out.println(list);
   }

/* your solution is simple sorting by comparing two elements */
/* the below mentioned code is for insertion sort
lets understand the insertion sort algorithm. We took an list with 6 integers.
We took a variable key, in which we put each element of the list, in each pass,
starting from the second element, that is list[1]. Then using the while loop, we iterate,
until i becomes less than -1 or we find an element which is greater than key,
and then we insert the key at that position.

In the above list, first we pick 104 as key, we compare it with 83(element before 104),
83 is smaller than key, we do nothing and 104 becomes the ket now. Then we pick 65, and compare it with 83 and 104,
it is less than 83 and 104, so we shift it to the start of list
And this goes on, until complete list gets sor
*/
    public static <E extends Comparable<E>> void sort(ArrayList<E> list) {
        for (int j = 1; j < list.size(); j++) {
            E key = list.get(j);
            int i = j-1;
            while ( (i > -1) && ( list.get(i).compareTo(key) > 0 ) ) {
                list.set(i+1, list.get(i));
                i--;
            }
            list.set(i+1, key);
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote