The method below is intended to use an insertion sort. For example: if I had a String ArrayList of elements ["code", "computer", "area", "school"], it should sort the elements by insertion sort into ["area", "code", "computer","school"]. I'm having a hard time understanding the logic, and have watched many videos.
I'm mainly struggling to understand what the two for loops are doing.
public static int insertSort(ArrayList<String> list) { int count = 0; for (int i = 1; i < list.size(); i++) { String toInsert = list.get(i); int j; for (j = i; j > 0; j--) { count++; if (toInsert.compareTo(list.get(j-1)) >= 0) { break; } } list.add(j, list.remove(i)); } return count; }