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

How can I make a method to fit this criteria? sort the list using insertion into

ID: 3789541 • Letter: H

Question

How can I make a method to fit this criteria?

sort the list using insertion into an initially empty list. Do not insert duplicate words.

PLEASE DO NOT ANSWER WITH ANYTHING THAT DOES NOT FULLY ANSWER MY QUESTION

NO, I CANNOT REMOVE THE DUPLICATES AFTER THE SORT

YES, IT HAS TO SORT THE ARRAYLIST USING INSERTION INTO AN INITIALLY EMPTY LIST. NO, I CANNOT INSERT DUPLICATE WORDS

please do not continue to waste my questions like 7 other people have done so far despite my specific request.....I realize there are other ways to do it, im having issues making a method that does this specifically

public static void insertionSortAList(ArrayList list) {

for (int i = 1; i < list.size(); i++) {

String currentElement = list.get(i);

int k;

for (k = i - 1; k >= 0 && list.get(k).compareTo(currentElement) > 0; k--) {

list.set(k+1, list.get(k));

}

list.set(k+1, currentElement); } }

"Clear" Question: How do I make this method sort an ArrayList list using insertion into an initially empty list. Do not insert duplicate words... Cant be anymore clear than that. As far as sample input.. well the parameter clearly states that the method is supposed to take an Arraylist as its input. I stated that I CANNOT REMOVE THE DUPLICATES AFTER THE SORT because thats what the 6 other people that answered my question told me to do when my question clearly says that I cannot insert duplicate words. Does sample input and output really need to be given.....(input = unsorted arraylist; output = sorted arraylist without duplicates). I can't believe I have to pay for this garbage...I guess im getting a bunch of high school kids answering my questions.

Explanation / Answer

import java.util.ArrayList;

public class Temp {
   public static ArrayList<String> insertionSortAList(ArrayList<String> list) {
       ArrayList<String> sortedList = new ArrayList<String>();
       for (int i = 0; i < list.size(); i++) {
           String currentElement = list.get(i);
           if(sortedList.size() == 0){
               sortedList.add(currentElement);
           }
           else{
               int k;
               for(k = 0; k < sortedList.size(); k++){
                   if(sortedList.get(k).compareTo(currentElement) > 0 && !sortedList.contains(currentElement)){
                       sortedList.add(k, currentElement);
                       break;
                   }
                   if(k == sortedList.size() - 1 && !sortedList.contains(currentElement)){
                       sortedList.add(currentElement);
                       break;
                   }
               }
           }
       }
       return sortedList;
   }

  
   public static void main(String args[]){
       ArrayList<String> arr = new ArrayList();
       arr.add("String4");
       arr.add("String1");
       arr.add("String2");
       arr.add("String2");
       arr.add("String5");
       arr.add("String5");
       System.out.println(arr);
       System.out.println(insertionSortAList(arr));
   }
}

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