-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path36.1 String Sorting Bubble Sort.java
89 lines (75 loc) · 2.26 KB
/
36.1 String Sorting Bubble Sort.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Write a program to take "n" String inputs from user and store them in array (where "n" is no. of String objects specified by user at run-time). Sort the array in ascending order and display the array. If "n" entered by user is less than 2, then display message "Invalid".
Input Format
Your program should take the input of "n" string objects.
Constraints
No. of string objects entered by the user should be greater than 1.
Output Format
Your program should display the array of strings in sorted ascending order.
Sample Input 0
4
India
America
Australia
France
Sample Output 0
America
Australia
France
India
*/
import java.io.*;
import java.util.*;
public class CLASS
{
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n>=2)
{
String []s = new String[n];
for(int i=0;i<n;i++)
{
s[i] = sc.next();
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
int k=0,l=0;
while(k<s[j].length() && l<s[j+1].length())
{
if(s[j].charAt(k) > s[j+1].charAt(l))
{
String temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
break;
}
else if(s[j].charAt(k) < s[j+1].charAt(l))
{
break;
}
else if(s[j+1].length() == l+1)
{
String temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
break;
}
k++;l++;
}
}
}
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}
}
else
{
System.out.print("Invalid");
}
}
}