package sorting; import java.util.Comparator; /** * Class full of static sorting methods. Used to sort packets received from a * server containing image metadata. * * TODO: Implement mergeSort() and selectionSort(). * * insertionSort is implemented for you as an example. * * @author pattersp * */ public class PacketSorter { /** * Sorts the given array of packets in ascending order according to the * comparator using mergesort. You may create as many private helper * functions as you wish to implement this method. * * A note about ascending order: * * When the method is finished, it should be true that: * comparator.compare(array[i - 1], array[i]) <= 0 for all i from 1 through * array.length. * * @param array * the packets to sort * @param comparator * The comparator the will be used to compare two packets. */ public static void mergeSort(Packet[] array, Comparator comparator) { // TODO: Add your merge sort algorithm here. throw new UnsupportedOperationException(); } /** * Sort the array of packets in ascending order using selection sort. * * A note about ascending order: * * When the method is finished, it should be true that: * comparator.compare(array[i - 1], array[i]) <= 0 for all i from 1 through * array.length. * * @param array * the array of packets that will be sorted. * @param comparator * The comparator the will be used to compare two packets. */ public static void selectionSort(Packet[] array, Comparator comparator) { // TODO: Add your selection sort algorithm here. throw new UnsupportedOperationException(); } /** * Sort the array of packets in ascending order using insertion sort. * * A note about ascending order: * * When the method is finished, it should be true that: * comparator.compare(array[i - 1], array[i]) <= 0 for all i from 1 through * array.length. * * @param array * the array of packets that will be sorted. * @param comparator * The comparator the will be used to compare two packets. */ public static void insertionSort(Packet[] array, Comparator comparator) { for (int outerIndex = 1; outerIndex < array.length; outerIndex++) { Packet currentPacket = array[outerIndex]; int innerIndex = outerIndex - 1; while (innerIndex >= 0 && comparator.compare(currentPacket, array[innerIndex]) < 0) { array[innerIndex + 1] = array[innerIndex]; innerIndex--; } array[innerIndex + 1] = currentPacket; } } }