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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.