Modify Insertionsort Algorithm Sorts Descending Order Rather Ascending Order Public Static Q37149840

Modify the insertionSort algorithm so that it sorts indescending order rather than ascending order.

public static void insertionSort(Comparable[] list)
   {
       for (int index = 1; index <list.length; index++)
       {
           Comparable key =list[index];
           int position =index;
           // Shift largervalues to the right
           while (position> 0 && key.compareTo(list[position – 1]) < 0)
           {
              list[position] = list[position – 1];
              position–;
           }
           list[position] =key;
       }
   }


Answer


public static void insertionSort(Comparable[] list) { for (int index = 1; index < list.length;

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.