Question

Data Structure and Algorithm in Java Q. Rewrite the INSERTION-SORT method to sort into descending order...

Data Structure and Algorithm in Java

Q. Rewrite the INSERTION-SORT method to sort into descending order instead of ascending order.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class InsertionSortArray {

    public static void insertionSort(int[] numbers) {
        for (int i = 1; i < numbers.length; i++) {
            int temp = numbers[i];
            int j = i - 1;

            while (j >= 0 && temp > numbers[j]) {
                numbers[j + 1] = numbers[j];
                j--;
            }
            numbers[j + 1] = temp;
        }
    }

    public static void print(int[] arr) {
        for (int i = 0; i < arr.length; ++i) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] arr = {2, 9, 1, 6, 3, 7};
        System.out.print("Original array: ");
        print(arr);
        insertionSort(arr);
        System.out.print("Sorted array: ");
        print(arr);
    }
}

Add a comment
Know the answer?
Add Answer to:
Data Structure and Algorithm in Java Q. Rewrite the INSERTION-SORT method to sort into descending order...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT