// Textbook fragment 11.03 /** * Sorts the elements of list in in nondecreasing order according * to comparator c, using the merge-sort algorithm. **/ public static void mergeSort (PositionList in, Comparator c) { int n = in.size(); if (n < 2) return; // the in list is already sorted in this case // divide PositionList in1 = new NodePositionList(); PositionList in2 = new NodePositionList(); int i = 0; while (i < n/2) { in1.addLast(in.remove(in.first())); // move the first n/2 elements to in1 i++; } while (!in.isEmpty()) in2.addLast(in.remove(in.first())); // move the rest to in2 // recur mergeSort(in1,c); mergeSort(in2,c); //conquer merge(in1,in2,c,in); }