-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortNames.txt
40 lines (36 loc) · 1014 Bytes
/
SortNames.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Scanner;
public class SortNames
{
public static void main(String[] args)
{
int i, j ,n;
String temp;
System.out.println("Enter no. of names to be entered: ");
Scanner sc=new Scanner(System.in)
n=sc.nextInt();
String names[] = new String[n];
System.out.print("Enter Names : ");
for(i=0; i<n; i++)
{
names[i] = sc.nextLine();
}
System.out.println("\nSorting Words/Names in Alphabetical Order...\n");
for(i=0; i<n; i++)
{
for(j=1; j<n; j++)
{
if(names[j-1].compareTo(names[j])>0)
{
temp=names[j-1];
names[j-1]=names[j];
names[j]=temp;
}
}
}
System.out.println("Names Sorted in Alphabetical Order: ");
for(i=0;i<5;i++)
{
System.out.println(names[i]);
}
}
}