-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort Strings.txt
203 lines (115 loc) · 2.56 KB
/
Sort Strings.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* 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
*/
import java.io.*;
class question2008
{
public static void main(String args[]) throws IOException
{
String words[]= {"Delhi", "Bangalore", "Agra", "Mumbai", "Calcutta"};
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int len=words.length;
int i,j;
String t;
for(i=0; i<len-1; i++)
{
for(j=0; j<len-1-i; j++)
{
if(words[j].compareTo(words[j+1])>0)
{
t=words[j+1];
words[j+1]=words[j];
words[j]=t;
}
}
}
System.out.println("The cities in alphabetical order are : ");
for(i=0; i<len; i++)
{
System.out.print(words[i]+ " ");
}
}
}
import java.util.*;
import java.io.*;
import java.util.Arrays;
class city{
public static void main(String args[])
{
String names[] = new String[6];
for(int i=0;i<6;i++)
{
names[i]= args[i];
}
Arrays.sort(names);
System.out.println(Arrays.toString(names));
}
}
class sorting
{
public static void main(String[] input)
{
int k=input.length;
String temp=new String();
String names[]=new String[k+1];
for(int i=0;i<k;i++)
{ names[i]=input[i];
}
for(int i=0;i<k;i++)
for(int j=i+1;j<k;j++)
{
if(names[i].compareTo(names[j])<0)
{
temp=names[i];
names[i]=names[j];
names[j]=temp;
}
System.out.println(“Sorted order is”);
for(int i=0;i<k;i++)
{
System.out.println(names[i]);
}
}
}
Output:
Java sorting Harish Ramesh Mahesh Rakesh
Sorted order is
Ramesh
Rakesh
Mahesh
Harish
Java sorting sai hari teja ravi sandeep
Sorted order is
teja
sandeep
sai
ravi
hari
package introtoarrays1;
public class IntroToArrays1 {
public static void main(String[] args) {
String [] name = new String[6];
String tempStr;
name[0]="Bob";
name[1]="Jenny";
name[2]="Charlie";
name[3]="Jerry";
name[4]="Roberta";
name[5]="Anita";
for(int t=0;t<name.length-1;t++){
for (int i = 0; i < name.length - 1; i++) {
if (name[i].compareTo(name[1 + 1]) > 0) {
tempStr = name[i];
name[i] = name[i + 1];
name[i + 1] = tempStr;
}
}
}
for(int i=0;i<name.length;i++){
System.out.println(name[i]);
}
}
}