-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBubbleSort.java
More file actions
34 lines (30 loc) · 1019 Bytes
/
Copy pathStringBubbleSort.java
File metadata and controls
34 lines (30 loc) · 1019 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
29
30
31
32
33
class StrBubble {
public static void main(String[] args) {
String[] strs = {
"this", "is", "a", "test",
"of", "a", "string", "sort"
};
int a, b, size = strs.length;
String t;
// Display original array
System.out.print("Original array is: ");
for (int i = 0; i < size; i++)
System.out.print(" " + strs[i]);
System.out.println();
// This is the bubble sort for strings.
for (a = 1; a < size; a++) {
for (b = size - 1; b >= a; b--) {
if (strs[b - 1].length() > strs[b].length()) {
// Exchange elements if out of order.
t = strs[b - 1];
strs[b - 1] = strs[b];
strs[b] = t;
}
}
}
System.out.print("Sorted array is: ");
for(int i = 0; i < size; i++)
System.out.print(" " + strs[i]);
System.out.println();
}
}