← All resources

Write a program to input a string array and sort it Alphabetically using Selection Sort technique

<h3>Output</h3>
Enter the size of the array:
5
Enter the names:
John
Thomas
Daniel
Gabriel
Arial
The sorted array:
Arial
Daniel
Gabriel
John
Thomas
import java.io.*;
public class String_Selection_Sort
{
    public static void main()throws IOException
    {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        int i, j, n, minp;
        System.out.println("Enter the size of the array:");
        n = Integer.parseInt(r.readLine());
        String a[] = new String[n], tmp, min;
        System.out.println("Enter the names:");
        for(i=0;i < n;i++)
        {
            a[i] = r.readLine();
        }
        //sorting the array
        for(i=0;i < n;i++)
        {
            min = a[i];
            minp = i;
            for(j=i+1;j < n;j++)
            {
                if(min.compareToIgnoreCase(a[j])>0)
                {
                    min = a[j];
                    minp = j;
                }
            }
            tmp = a[minp];
            a[minp] = a[i];
            a[i] = tmp;
        }
        //printing the array
        System.out.println("The sorted array:");
        for(i=0;i < n;i++)
        {
            System.out.println(a[i]);
        }
    }
}