-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlphabetical_order.java
More file actions
28 lines (26 loc) · 949 Bytes
/
Copy pathAlphabetical_order.java
File metadata and controls
28 lines (26 loc) · 949 Bytes
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
// Define a class and store the given city names in a single dimensional array.
// Sort these names in alphabetical order using the Bubble Sort technique only.
// INPUT : Delhi, Bangalore, Agra, Mumbai, Calcutta
// OUTPUT : Agra, Bangalore, Calcutta, Delhi, Mumbai [15]
import java.io.*;
class Alphabetical_order {
public static void main(String args[]) {
String[] name = { "Delhi", "Bangalore", "Agra", "Mumbai", "Calcutta" };
int i, j;
String temp;
// bubble sort begins
for (i = 0; i < 4; i++) {
for (j = 0; j < 4 - i; j++) {
if ((name[j].compareTo(name[j + 1])) > 0) {
temp = name[j];
name[j] = name[j + 1];
name[j + 1] = temp;
}
}
}
// printing
for (i = 0; i < 5; i++) {
System.out.println(name[i]);
}
}
}