Answer to Question #300586 in Java | JSP | JSF for Akash Srivastava

Question #300586



An array of 5 string is given where each string contains 2 characters, Now you have to sort these strings using insertion sort, like in a dictionary.


Input


Input contains 5 strings of length 2 separated by spaces.


String contains only uppercase English letters.


Output


Print the sorted array.

1
Expert's answer
2022-02-24T12:20:01-0500
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] strings = new String[5];
        for (int i = 0; i < strings.length; i++) {
            strings[i] = in.next();
        }
        for (int i = 1; i < strings.length; i++) {
            int cur = i;
            for (int j = i - 1; j >= 0; j--) {
                if (strings[j].charAt(0) > strings[cur].charAt(0) ||
                        (strings[j].charAt(0) == strings[cur].charAt(0)
                                && strings[j].charAt(1) > strings[cur].charAt(1))) {
                    String tmp = strings[j];
                    strings[j] = strings[cur];
                    strings[cur] = tmp;
                    cur = j;
                }
            }
        }
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS