Open In App

Java Program to Sort Names in an Alphabetical Order

Last Updated : 15 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

For, sorting names in an Alphabetical order there are multiple ways to sort the array, like using inbuilt Arrays.sort() method or using normal sorting algorithms like the bubble sort, merge sort. Here let’s use the bubble sort and inbuilt sort.

Example:

Input : Array[] = {"Sourabh", "Anoop, "Harsh", "Alok", "Tanuj"}
Output: Array[] = {"Alok", "Anoop", "Harsh", "Sourabh", "Tanuj"}
 
Input : Array[] = {"Bob", "Alice"}
Output: Array[] = {"Alice", "Bob"}

Approach: Brute-Force Approach

The idea is to compare the strings on the basis of their unicode and swap them in accordance with the returned int value based on the comparison between the two strings using compareTo() method.

In the input, the user has to enter the number of names and the names and on the output, it will sort and display them in alphabetical order. For this, we are going to use the compareTo() method and compare one string with the rest of the strings.

CompareTo() is used to compare two strings lexicographically. Each character of both strings is converted into its unicode value. Lexicographical order is nothing but alphabetical order. 

This method returns an int data-type which is based on the comparison between the two string. If it returns>0 then the parameter passed to compareTo() method is lexicographically first whereas if returns < 0 then string calling the method is lexicographically correct.

Steps

  • Using CompareTo() method compare one string with the rest of the strings
  • To swap the elements based on the comparison between the two string.
  • Print the Sorted Names in an Alphabetical Order.

Below is the implementation of the above approach:

Java




// Java Program to Sort Names in an Alphabetical Order
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // storing input in variable
        int n = 4;
       
        // create string array called names
        String names[]
            = { "Rahul", "Ajay", "Gourav", "Riya" };
        String temp;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
               
                // to compare one string with other strings
                if (names[i].compareTo(names[j]) > 0) {
                    // swapping
                    temp = names[i];
                    names[i] = names[j];
                    names[j] = temp;
                }
            }
        }
       
        // print output array
        System.out.println(
            "The names in alphabetical order are: ");
        for (int i = 0; i < n; i++) {
            System.out.println(names[i]);
        }
    }
}


Output

The names in alphabetical order are: 
Ajay
Gourav
Rahul
Riya

Time Complexity: O(N2)

Approach: Inbuilt Sort function

  • Using inbuilt Arrays.sort() method to sort the array.
  • Print the Sorted Names in an Alphabetical Order.

Below is the implementation of the above approach:

Java




// Java Program to Sort Names in an Alphabetical Order
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        // storing input in variable
        int n = 4;
        // create string array called names
        String names[]
            = { "Rahul", "Ajay", "Gourav", "Riya" };
        // inbuilt sort function
        Arrays.sort(names);
        // print output array
        System.out.println(
            "The names in alphabetical order are: ");
        for (int i = 0; i < n; i++) {
            System.out.println(names[i]);
        }
    }
}


Output

The names in alphabetical order are: 
Ajay
Gourav
Rahul
Riya

Time Complexity: O(n log n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads