Question

Write a Java program that implements Insertion Sort and sorts the following Array reverse alphabetically, printing...

Write a Java program that implements Insertion Sort and sorts the following Array reverse alphabetically, printing the results to the console.

{"five", "three", "eight","one","two","four","nine","seven","six","ten"}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

OUTPUT:


CODE:
/**********************************************************
*InsertionSort.java
***********************************************************/

package com.example.sort;

public class InsertionSort {
    public static void main(String[] args){
        String[] array = new String[]{"five", "three", "eight","one","two","four","nine","seven","six","ten"};
        sort(array);
        System.out.println("Reverse Sorted Array");
        for(String str: array ){
            System.out.println(str);
        }
    }

    //insertion sort algorithm
    static void sort(String[] arr) {
        for(int i=1; i<arr.length; i++) {
            String data = arr[i];
            int j = i-1;
            /*Move elements of arr[0..i-1], that are
            less than data, to one position ahead
            of their current position*/
            while(j >=0 &&  data.compareTo(arr[j]) > 0) {
                arr[j+1] = arr[j];
                j--;
            }
            arr[j+1] = data;
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a Java program that implements Insertion Sort and sorts the following Array reverse alphabetically, printing...
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