Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I write this program so that the sorting algorithm puts the values in de

ID: 3554278 • Letter: H

Question

How can I write this program so that the sorting algorithm puts the values in descending order, rather than ascending??

Here's my code:

//sort array using insertion sory

public void sort()

   {

       int insert; //temporary variable to hold element to insert

      

       //loop over data.length - 1 elements

       for(int next = 1; next<data.length; next++)

       {

           //store value in current element

           insert = data[next];

          

           //initialize location to place element

           int moveItem = next;

          

           //search for place to put current element

           while(moveItem > 0 && data[moveItem - 1] > insert)

           {

               //shift element right one slot

               data[moveItem] = data[moveItem - 1];

               moveItem--;

           }

          

           data[moveItem] = insert; //place inserted element

           printPass(next, moveItem); //output pass of algorithm

       }

   }

Explanation / Answer

is this Java?

while(moveItem < 0 && data[moveItem - 1] > insert) should do the trick

Let me know if that doesn't work.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote