ICSE Class 10 Computer Applications Question 5 of 6

Solved 2015 Question Paper ICSE Class 10 Computer Applications — Question 5

Back to all questions
5
Question

Question 8

Write a program to input twenty names in an array. Arrange these names in descending order of letters, using the bubble sort technique.

Answer
import java.util.Scanner;

public class KboatArrangeNames
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        String names[] = new String[20];
        System.out.println("Enter 20 names:");
        for (int i = 0;  i < names.length; i++) {
            names[i] = in.nextLine();
        }

        //Bubble Sort
        for (int i = 0; i < names.length - 1; i++) {
            for (int j = 0; j < names.length - 1 - i; j++) {
                if (names[j].compareToIgnoreCase(names[j + 1]) < 0) {
                    String temp = names[j + 1];
                    names[j + 1] = names[j];
                    names[j] = temp;
                }
            }
        }
        
        System.out.println("\nSorted Names");
        for (int i = 0;  i < names.length; i++) {
            System.out.println(names[i]);
        }
    }
}
Output
BlueJ output of KboatArrangeNames.java