X1209: Insertion Sort with Comparable

Complete the sort method below. It implements the insertion sort algorithm shown in class and included below. But note that this example uses int[]. You must convert it to using T (generic parameter that extends Comparable<T>) and you must use Comparable interface to compare values and swap them if necessary.

   public void insertionSort(int[] values) {

       for (int i = 1; i < values.length; i++) {
           int key = values[i];   // The item to insert
           int j = i - 1;

           // Shift larger elements to the right
           while (j >= 0 && values[j] > key) {
               values[j + 1] = values[j];
               j--;
           }

           // Insert the key at the correct position
           values[j + 1] = key;
       }
   }

Your job is to write the equivalent condition in the while loop using the Comparable interface.

Your Answer:

Feedback

Your feedback will appear here when you check your answer.