How to sort a String ArrayList using insertion sort method i

How to sort a String ArrayList using insertion sort method into an initially empty list. Do not insert duplicate words. Java Im having trouble understanding how to make a method that takes an arraylist and sorts it this way. Please do not ignore the bold parts of the question. I know how to do a standard insertion sort for an ArrayList, im having trouble understanding what I bolded.

Solution

According to your requirement I have created JAVA code as below.

----------------------------------------------------------------------------------------------------------------------------------------------------------

Program:

String[] inArray = { \"Names according to Your requiremnet \"};
for(int i = 1; i < inArray.length; i++) { // Loop of number of times same as the array length
String key = inArray[i];

int k = i - 1;

while (k >= 0 && key.compareTo(inArray[k]) < 0) {
inArray[k + 1] = inArray[k];
k--;
}
inArray[k + 1] = key;
}
System.out.println(Arrays.toString(inArray));

----------------------------------------------------------------------------------------------------------------------------------------------------------

If you have any doubt please reply in comment

How to sort a String ArrayList using insertion sort method into an initially empty list. Do not insert duplicate words. Java Im having trouble understanding how

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site