/
QuickSortLinkedList.java
378 lines (350 loc) · 10 KB
/
QuickSortLinkedList.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import java.util.*;
public class QuickSortLinkedList
{
public static void main(String[] args)
{
System.out.println("Try it");
SLL s1 = new SLL();
SLL s2 = new SLL();
SLL s3 = new SLL();
s1.append(2);
s1.append(20);
s1.append(30);
s1.append(40);
s1.append(50);
System.out.println("================");
show(s1.getHead());
System.out.println("================");
Node mnode = partition(s1.getHead());
show(s1.getHead());
System.out.println("================");
show(mnode);
System.out.println("================");
Node node = median(s1.getHead());
System.out.println("median["+node.data+"]");
s2.append(1);
s2.append(7);
s2.append(10);
s3.append(3);
s3.append(4);
s3.append(20);
Node s = mergeList(mergeList(s1.getHead(), s2.getHead()), s3.getHead());
show(s);
test1();
test2();
test3();
test4();
test5();
test6();
}
public static void show(Node curr)
{
while(curr != null)
{
System.out.println("["+curr.data+"]");
curr = curr.next;
}
}
public static void test4()
{
System.out.println("begin test4==================");
SLL s1 = new SLL();
s1.append(2);
s1.append(40);
s1.append(100);
s1.append(50);
s1.append(30);
s1.append(20);
s1.append(1);
s1.append(6);
s1.append(20);
s1.append(2);
System.out.println("----------------------------");
show(s1.getHead());
System.out.println("----------------------------");
Node[] prev = new Node[1];
prev[0] = null;
qsLinkedList(s1.getHead(), s1.getTail(), prev);
show(s1.getHead());
System.out.println("begin test4==================");
}
public static void test5()
{
System.out.println("begin test4==================");
SLL s1 = new SLL();
s1.append(1);
s1.append(2);
s1.append(3);
System.out.println("----------------------------");
show(s1.getHead());
System.out.println("----------------------------");
Node[] prev = new Node[1];
prev[0] = null;
qsLinkedList(s1.getHead(), s1.getTail(), prev);
show(s1.getHead());
System.out.println("begin test5==================");
}
public static void test6()
{
System.out.println("begin test6==================");
SLL s1 = new SLL();
s1.append(30);
s1.append(20);
s1.append(10);
System.out.println("----------------------------");
show(s1.getHead());
System.out.println("----------------------------");
Node[] prev = new Node[1];
prev[0] = null;
qsLinkedList(s1.getHead(), s1.getTail(), prev);
show(s1.getHead());
System.out.println("begin test4==================");
}
public static void test2()
{
System.out.println("begin test2()================");
SLL s1 = new SLL();
s1.append(4);
s1.append(8);
show(s1.getHead());
Node[] prev = new Node[1];
prev[0] = null;
Node pivot = qsPartition(s1.getHead(), s1.getTail(), prev);
System.out.println("pivot=["+pivot.data+"]");
System.out.println("================");
show(s1.getHead());
System.out.println("end test1()================");
}
public static void test3()
{
System.out.println("begin test3()================");
SLL s1 = new SLL();
s1.append(4);
show(s1.getHead());
Node[] prev = new Node[1];
prev[0] = null;
Node pivot = qsPartition(s1.getHead(), s1.getTail(), prev);
if(pivot != null)
System.out.println("pivot=["+pivot.data+"]");
System.out.println("================");
show(s1.getHead());
System.out.println("end test3()================");
}
public static void test1()
{
System.out.println("begin test2()================");
SLL s1 = new SLL();
s1.append(4);
s1.append(8);
s1.append(3);
s1.append(6);
s1.append(5);
show(s1.getHead());
Node[] prev = new Node[1];
prev[0] = null;
Node pivot = qsPartition(s1.getHead(), s1.getTail(), prev);
System.out.println("pivot=["+pivot.data+"]");
System.out.println("================");
show(s1.getHead());
System.out.println("end test1()================");
}
public static void swap(Node first, Node second)
{
int tmp = first.data;
first.data = second.data;
second.data = tmp;
}
public static void qsLinkedList(Node head, Node tail, Node[] prev)
{
Node h = head;
Node t = tail;
Node pivot = qsPartition(h, t, prev);
Node myh = h;
Node myt = t;
while(myh != null)
{
System.out.println("myh["+myh.data+"]");
if(myh != myt)
myh = myh.next;
else
break;
}
if(head != tail)
{
if(pivot != null)
System.out.println("pivot["+pivot.data+"]");
if(prev[0] != null)
System.out.println("prev["+prev[0].data+"]");
if(h != null)
System.out.println("h["+h.data+"]");
if(t != null)
System.out.println("t["+t.data+"]");
System.out.println("-----------------------");
//if(h != null && prev[0] != null)
if(h != pivot)
qsLinkedList(h, prev[0], prev);
if(pivot != t)
qsLinkedList(pivot.next, t, prev);
}
}
public static Node qsPartition(Node head, Node tail, Node[] prev)
{
Node curr = head;
Node big = curr;
Node pivot = tail;
if(head == tail)
{
System.out.println("head{"+head.data+"}");
System.out.println("tail{"+tail.data+"}");
System.out.println("pivot{"+pivot.data+"}");
prev[0] = null;
return null;
}
else
{
while(curr != null)
{
if(curr.data < pivot.data)
{
swap(curr, big);
prev[0] = big;
System.out.println("prev{"+prev[0].data+"}");
big = big.next;
}
curr = curr.next;
}
if(big != null)
swap(pivot, big);
return big;
}
}
public static Node partition(Node head)
{
Node mnode = median(head);
if(mnode != null)
{
Node ret = mnode.next;
mnode.next = null;
return ret;
}
else
return mnode;
}
public static Node median(Node head)
{
Node curr = head;
Node mcurr = curr;
if(curr != null)
{
while(curr.next != null)
{
curr = curr.next.next;
if(curr == null)
break;
mcurr = mcurr.next;
}
}
return mcurr;
}
public static int[] merge(int[] arr1, int[] arr2)
{
int[] arr = null;
if(arr1 == null)
return arr2;
else if(arr2 == null)
return arr1;
else
{
int len1 = arr1.length;
int len2 = arr2.length;
arr = new int[len1+len2];
int inx1 = 0;
int inx2 = 0;
int k=0;
while(inx1 < len1 || inx2 < len2)
{
if(inx1 >= len1)
{
arr[k] = arr1[inx2];
inx2++;
}
else if(inx2 >= len2)
{
arr[k] = arr2[inx1];
inx1++;
}
else
{
if(arr1[inx1] < arr2[inx2])
{
arr[k] = arr1[inx1];
inx1++;
}
else
{
arr[k] = arr2[inx2];
inx2++;
}
}
k++;
}
}
return arr;
}
public static Node mergeList(Node s1, Node s2)
{
Node curr = null;
Node head = curr;
while( s1 != null || s2 != null)
{
if(s1 == null)
{
if(curr == null)
head = curr = s2;
else
{
curr.next = s2;
curr = curr.next;
}
s2 = s2.next;
}
else if(s2 == null)
{
if(curr == null)
head = curr = s1;
else
{
curr.next = s1;
curr = curr.next;
}
s1 = s1.next;
}
else
{
if(s1.data < s2.data)
{
if(curr == null)
head = curr = s1;
else
{
curr.next = s1;
curr = curr.next;
}
s1 = s1.next;
}
else
{
if( curr == null)
head = curr = s2;
else
{
curr.next = s2;
curr = curr.next;
}
s2 = s2.next;
}
}
}
return head;
}
}