質問編集履歴

2

他の残りのコードも追記しました

2018/11/29 09:02

投稿

Miyuu_apple38
Miyuu_apple38

スコア12

test CHANGED
File without changes
test CHANGED
@@ -92,7 +92,7 @@
92
92
 
93
93
  public static void main(String[] args) {
94
94
 
95
- List l = new List(25);
95
+ List l = new List(15);
96
96
 
97
97
  l.makeList();
98
98
 

1

全てのコードを追記しました。

2018/11/29 09:02

投稿

Miyuu_apple38
Miyuu_apple38

スコア12

test CHANGED
File without changes
test CHANGED
@@ -6,11 +6,13 @@
6
6
 
7
7
  ```Java
8
8
 
9
+
10
+
9
- List Merge(List list1, List list2) {
11
+ List Merge(List list1, List list2) {
10
-
11
-
12
-
12
+
13
+
14
+
13
- Cell c1 = header.next;
15
+ Cell c1 = header.next;
14
16
 
15
17
  Cell c2 = list2.header;
16
18
 
@@ -73,3 +75,269 @@
73
75
  最後から二行目がエラーになります。
74
76
 
75
77
  list3というlist1とlist2というマージされた1つのリストを返すには、list1にlist2を加えるという考え方ではないのでしょうか。アドバイスいただけると幸いです!
78
+
79
+
80
+
81
+ **3.追記**
82
+
83
+ 線形リスト(連結リスト)でのマージソートです。
84
+
85
+ ```Java
86
+
87
+ import java.util.Random;
88
+
89
+
90
+
91
+ public class MergeSort {
92
+
93
+ public static void main(String[] args) {
94
+
95
+ List l = new List(25);
96
+
97
+ l.makeList();
98
+
99
+ l.printList();
100
+
101
+ l.mergeSort();
102
+
103
+ l.printList();
104
+
105
+ }
106
+
107
+ }
108
+
109
+
110
+
111
+ class List {
112
+
113
+
114
+
115
+ Cell header = new Cell("HEADER");
116
+
117
+ Cell sentinel = new Cell("");
118
+
119
+ int dataNum;
120
+
121
+
122
+
123
+ List(int num) {
124
+
125
+ dataNum = num;
126
+
127
+ header.next = sentinel;
128
+
129
+ }
130
+
131
+
132
+
133
+ void makeList() {
134
+
135
+ int[] tmpList = new int[dataNum];
136
+
137
+
138
+
139
+ for (int i=0; i<dataNum; i++) {
140
+
141
+ tmpList[i]=i;
142
+
143
+ }
144
+
145
+
146
+
147
+ for (int i=0; i<dataNum-1; i++) {
148
+
149
+ Random rnd = new Random();
150
+
151
+ int offset = rnd.nextInt(dataNum-i);
152
+
153
+ int tmp;
154
+
155
+
156
+
157
+ tmp = tmpList[i];
158
+
159
+ tmpList[i] = tmpList[i + offset];
160
+
161
+ tmpList[i + offset] = tmp;
162
+
163
+ }
164
+
165
+
166
+
167
+ for (int i=0; i<dataNum; i++) {
168
+
169
+ insertTop(new Cell(new Integer(tmpList[i])));
170
+
171
+ }
172
+
173
+ }
174
+
175
+
176
+
177
+ void mergeSort() {
178
+
179
+ int center = dataNum/2;
180
+
181
+ Cell curr = header.next;
182
+
183
+
184
+
185
+ for (int i =0; i<center; i++) curr = curr.next;
186
+
187
+
188
+
189
+ List list1 = MergeSortRec(header.next,curr,center);
190
+
191
+ List list2 = MergeSortRec(curr,sentinel,dataNum-center);
192
+
193
+ List list3 = Merge(list1,list2);
194
+
195
+
196
+
197
+ header = list3.header;
198
+
199
+ sentinel = list3.sentinel;
200
+
201
+ }
202
+
203
+
204
+
205
+ List MergeSortRec(Cell start,Cell end, int num) {
206
+
207
+ int center2 = num/2;
208
+
209
+ start = header.next;
210
+
211
+ Cell c = header.next;
212
+
213
+
214
+
215
+ for (int i=0; i<center2 ; i++) start=start.next;
216
+
217
+ List list1 = MergeSortRec(c,start,center2);
218
+
219
+ if(center2==1){
220
+
221
+ if((Integer)c.data>(Integer)start.data){
222
+
223
+ swap(c,start);
224
+
225
+ }
226
+
227
+ }
228
+
229
+
230
+
231
+ List list2 = MergeSortRec(start,end,num-center2);
232
+
233
+ if(center2==1){
234
+
235
+ if((Integer)start.data>(Integer)end.data){
236
+
237
+ swap(start,end);
238
+
239
+ }
240
+
241
+ }
242
+
243
+
244
+
245
+ List list3 = Merge(list1,list2);
246
+
247
+
248
+
249
+ header = list3.header;
250
+
251
+ end = list3.sentinel;
252
+
253
+
254
+
255
+ return list3;
256
+
257
+
258
+
259
+ }
260
+
261
+
262
+
263
+ List Merge(List list1, List list2){
264
+
265
+ // 最初に示したコードが下に入ります
266
+
267
+ }
268
+
269
+
270
+
271
+ void swap(Cell c1,Cell c2) {
272
+
273
+ if ((c1 != null) && (c2 != null)) {
274
+
275
+ Object tmp = c1.data;
276
+
277
+ c1.data = c2.data;
278
+
279
+ c2.data = tmp;
280
+
281
+ }
282
+
283
+ }
284
+
285
+
286
+
287
+ void insertTop(Cell c) {
288
+
289
+ c.next = header.next;
290
+
291
+ header.next = c;
292
+
293
+
294
+
295
+ }
296
+
297
+
298
+
299
+ void printList() {
300
+
301
+ Cell curr = header.next;
302
+
303
+
304
+
305
+ while (curr.next !=null) {
306
+
307
+ System.out.print(curr.data + " ");
308
+
309
+ curr = curr.next;
310
+
311
+ }
312
+
313
+ System.out.println();
314
+
315
+ }
316
+
317
+ }
318
+
319
+
320
+
321
+ class Cell {
322
+
323
+ Cell next;
324
+
325
+ Object data;
326
+
327
+
328
+
329
+ Cell(Object obj) {
330
+
331
+ next = null;
332
+
333
+ data = obj;
334
+
335
+ }
336
+
337
+ }
338
+
339
+
340
+
341
+
342
+
343
+ ```