Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9649dc8

Browse files
committed
sort
1 parentd84764f commit9649dc8

File tree

1 file changed

+255
-0
lines changed

1 file changed

+255
-0
lines changed

‎sort/SortList.java

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
packageAlgorithms.sort;
2+
3+
importAlgorithms.algorithm.others.ListNode;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) {
11+
* val = x;
12+
* next = null;
13+
* }
14+
* }
15+
*/
16+
publicclassSortList {
17+
publicstaticvoidmain(String[]strs) {
18+
ListNoden1 =newListNode(2);
19+
ListNoden2 =newListNode(18);
20+
ListNoden3 =newListNode(3);
21+
ListNoden4 =newListNode(54);
22+
ListNoden5 =newListNode(6);
23+
ListNoden6 =newListNode(90);
24+
ListNoden7 =newListNode(2);
25+
ListNoden8 =newListNode(1);
26+
ListNoden9 =newListNode(19);
27+
ListNoden10 =newListNode(30);
28+
29+
30+
n1.next =n2;
31+
n2.next =n3;
32+
n3.next =n4;
33+
n4.next =n5;
34+
n5.next =n6;
35+
n6.next =n7;
36+
n7.next =n8;
37+
n8.next =n9;
38+
n9.next =n10;
39+
n10.next =null;
40+
41+
ListNoderet =sortList(n1);
42+
System.out.println(ret.toString());
43+
}
44+
45+
publicstaticListNodesortList1(ListNodehead) {
46+
// Nodes should be more than 2.
47+
if (head ==null ||head.next ==null) {
48+
returnhead;
49+
}
50+
51+
// get the mid node.
52+
ListNodemidPre =getMidPre(head);
53+
54+
// Cut the two list.
55+
ListNoderight =midPre.next;
56+
midPre.next =null;
57+
58+
// Sort the left side and the right side.
59+
ListNodeleft =sortList(head);
60+
right =sortList(right);
61+
62+
// Merge the two sides together.
63+
returnmerge(left,right);
64+
}
65+
66+
// get the pre node before mid.
67+
publicstaticListNodegetMidPre1(ListNodehead) {
68+
ListNodeslow =head;
69+
ListNodefast =head;
70+
71+
while (fast !=null &&fast.next !=null &&fast.next.next !=null) {
72+
slow =slow.next;
73+
fast =fast.next.next;
74+
}
75+
76+
returnslow;
77+
}
78+
79+
// get the pre node before mid.
80+
publicstaticListNodegetMidPre(ListNodehead) {
81+
ListNodeslow =head;
82+
83+
// fast提前一点儿。这样就可以得到前一个节点喽。
84+
ListNodefast =head.next;
85+
86+
while (fast !=null &&fast.next !=null) {
87+
slow =slow.next;
88+
fast =fast.next.next;
89+
}
90+
91+
returnslow;
92+
}
93+
94+
publicstaticListNodemerge(ListNodehead1,ListNodehead2) {
95+
ListNodedummy =newListNode(0);
96+
ListNodecur =dummy;
97+
98+
while (head1 !=null &&head2 !=null) {
99+
if (head1.val <head2.val) {
100+
cur.next =head1;
101+
head1 =head1.next;
102+
}else {
103+
cur.next =head2;
104+
head2 =head2.next;
105+
}
106+
107+
cur =cur.next;
108+
}
109+
110+
if (head1 !=null) {
111+
cur.next =head1;
112+
}else {
113+
cur.next =head2;
114+
}
115+
116+
returndummy.next;
117+
}
118+
119+
/*
120+
The Solution 2:
121+
Quick Sort.
122+
*/
123+
publicstaticListNodesortList(ListNodehead) {
124+
if (head ==null) {
125+
returnnull;
126+
}
127+
128+
// Sort the list from 0 to len - 1
129+
returnquickSort(head);
130+
}
131+
132+
// The quick sort algorithm
133+
134+
// All the elements are the same!
135+
publicstaticbooleanisDuplicate(ListNodehead) {
136+
while (head !=null) {
137+
if (head.next !=null &&head.next.val !=head.val) {
138+
returnfalse;
139+
}
140+
141+
head =head.next;
142+
}
143+
144+
returntrue;
145+
}
146+
147+
publicstaticListNodequickSort(ListNodehead) {
148+
if (head ==null) {
149+
returnnull;
150+
}
151+
152+
// 如果整个链是重复的,直接跳过。
153+
if (isDuplicate(head)) {
154+
returnhead;
155+
}
156+
157+
// Use the head node to be the pivot.
158+
ListNodeheadNew =partition(head,head.val);
159+
160+
// Find the pre position of the pivoit.
161+
ListNodecur =headNew;
162+
163+
ListNodedummy =newListNode(0);
164+
dummy.next =headNew;
165+
166+
ListNodepre =dummy;
167+
168+
// Find the pre node and the position of the piviot.
169+
while (cur !=null) {
170+
if (cur.val ==head.val) {
171+
break;
172+
}
173+
174+
// move forward.
175+
cur =cur.next;
176+
pre =pre.next;
177+
}
178+
179+
// Cut the link to be three parts.
180+
pre.next =null;
181+
182+
// Get the left link;
183+
ListNodeleft =dummy.next;
184+
185+
// Get the right link.
186+
ListNoderight =cur.next;
187+
cur.next =null;
188+
189+
// Recurtion to call quick sort to sort left and right link.
190+
left =quickSort(left);
191+
right =quickSort(right);
192+
193+
// Link the three part together.
194+
195+
// Link the first part and the 2nd part.
196+
if (left !=null) {
197+
dummy.next =left;
198+
199+
// Find the tail of the left link.
200+
while (left.next !=null) {
201+
left =left.next;
202+
}
203+
left.next =cur;
204+
}else {
205+
dummy.next =cur;
206+
}
207+
208+
cur.next =right;
209+
210+
// The new head;
211+
returndummy.next;
212+
}
213+
214+
// Return the new head;
215+
publicListNodepartition(ListNodehead,intx) {
216+
if (head ==null) {
217+
returnnull;
218+
}
219+
220+
ListNodedummy =newListNode(0);
221+
dummy.next =head;
222+
223+
ListNodepre =dummy;
224+
ListNodecur =head;
225+
226+
// Record the big list.
227+
ListNodebigDummy =newListNode(0);
228+
ListNodebigTail =bigDummy;
229+
230+
while (cur !=null) {
231+
if (cur.val >=x) {
232+
// Unlink the cur;
233+
pre.next =cur.next;
234+
235+
// Add the cur to the tail of the new link.
236+
bigTail.next =cur;
237+
cur.next =null;
238+
239+
// Refresh the bigTail.
240+
bigTail =cur;
241+
242+
// 移除了一个元素的时候,pre不需要修改,因为cur已经移动到下一个位置了。
243+
}else {
244+
pre =pre.next;
245+
}
246+
247+
cur =pre.next;
248+
}
249+
250+
// Link the Big linklist to the smaller one.
251+
pre.next =bigDummy.next;
252+
253+
returndummy.next;
254+
}
255+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp