KnowledgeBoat Logo

Computer Applications

A class teacher wants to arrange the names of her students in alphabetical order. Define a class NameSorter that stores the given names in a one-dimensional array. Sort the names in alphabetical order using Bubble Sort technique only and display the sorted names.

Aryan, Zoya, Ishaan, Neha, Rohan, Tanya, Manav, Simran, Kabir, Pooja

public class NameSorter
{
    void bubbleSort(String names[]) {
        int len = names.length;
        _______(1)_________ {
            _______(2)_________ {
                _______(3)_________ {
                    String t = names[j];
                    _______(4)_________
                    _______(5)_________
                }
            }
        }
    }
    
    public static void main(String[] args) {
        
        String arr[] = {"Aryan", "Zoya", 
                        "Ishaan", "Neha", 
                        "Rohan", "Tanya", 
                        "Manav", "Simran", 
                        "Kabir", "Pooja"
                       };
                       
        NameSorter obj = new NameSorter();
        obj.bubbleSort(arr);
        
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

Java Arrays

2 Likes

Answer

  1. for (int i = 0; i < len - 1; i++)
  2. for (int j = 0; j < len - 1 - i; j++)
  3. if (names[j].compareToIgnoreCase(names[j + 1]) > 0)
  4. names[j] = names[j + 1];
  5. names[j + 1] = t;

Explanation

public class NameSorter
{
    void bubbleSort(String names[]) {
        int len = names.length;
        for (int i = 0; i < len - 1; i++) {
            for (int j = 0; j < len - 1 - i; j++) {
                if (names[j].compareToIgnoreCase(names[j + 1]) > 0) {
                    String t = names[j];
                    names[j] = names[j + 1];
                    names[j + 1] = t;
                }
            }
        }
    }
    
    public static void main(String[] args) {
        
        String arr[] = {"Aryan", "Zoya", 
                        "Ishaan", "Neha", 
                        "Rohan", "Tanya", 
                        "Manav", "Simran", 
                        "Kabir", "Pooja"
                       };
                       
        NameSorter obj = new NameSorter();
        obj.bubbleSort(arr);
        
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

Variable Description Table

Method bubbleSort:

Variable NameData TypePurpose
namesString[]An array of Strings that holds the list of names to be sorted.
lenintThe length of the names array, indicating the number of names to be sorted.
iintAn index variable used as a loop counter for the outer loop to control the number of passes.
jintAn index variable used as a loop counter for the inner loop to compare adjacent elements.
tStringA temporary variable used to hold a name value during the swapping process.

Method main:

Variable NameData TypePurpose
arrString[]An array of Strings containing the names to be sorted.
objNameSorterAn instance of the NameSorter class used to call the bubbleSort method.
iintAn index variable used as a loop counter to iterate over the sorted names for printing.

Program Explanation

Let's analyze the Java program step by step to understand its functionality:

1. Class Definition:

  • The class NameSorter is defined, which contains a method bubbleSort and the main method to execute the program.

2. bubbleSort Method:

  • The method bubbleSort accepts an array of strings names[] as a parameter. This method will sort the strings in the array in alphabetical order.

3. Outer and Inner Loops:

  • The bubbleSort uses two nested loops to iterate through the array:
    • The outer loop runs from the beginning of the array to the second-to-last element (len - 1). The variable i serves as the counter for this loop.
    • The inner loop runs from the start of the array up to the last unsorted element (len - 1 - i). The variable j is the counter for the inner loop.
    • The decreasing upper bound in the inner loop len - 1 - i is because with each complete pass through the array, the largest unsorted element gets correctly positioned at the end, reducing the number of elements needing comparison.

4. Comparison and Swapping:

  • Inside the inner loop, a comparison is made between adjacent elements using compareToIgnoreCase:
    • The compareToIgnoreCase method is used to compare two strings lexicographically, returning a result based on whether the first string is considered greater, equal, or smaller than the second string, without regard to case.
    • If names[j] is lexicographically greater than names[j + 1] (i.e., compareToIgnoreCase returns a positive number), the elements are swapped. This ensures larger elements "bubble up" to their correct position.

5. Swapping Logic:

  • A temporary string t is used to facilitate the swapping of names[j] and names[j+1]. The algorithm stores names[j] in t, assigns names[j+1] to names[j], and finally assigns t to names[j+1].

6. Main Method:

  • The main method initializes an array arr with several names in mixed order.
  • An instance obj of the NameSorter class is created.
  • The bubbleSort method is called on the obj instance, passing the arr to be sorted.
  • After sorting, the program iterates through the sorted array and prints each name to the console, displaying the names in alphabetical order.

Output

A class teacher wants to arrange the names of her students in alphabetical order. Define a class NameSorter that stores the given names in a one-dimensional array. Sort the names in alphabetical order using Bubble Sort technique only and display the sorted names. Practice Test ICSE Computer Applications Class 10

Answered By

2 Likes


Related Questions