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

Appearance settings

Latest commit

 

History

History
History
53 lines (47 loc) · 1.45 KB

File metadata and controls

53 lines (47 loc) · 1.45 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package algorithms;
/**
* @Auther: Jesper
* @Date: 2019/2/13 15:42
* @Description: 单链表的快排
*
* 我们只需要两个指针p1和p2,这两个指针均往next方向移动,移动的过程中保持p1之前的key都小于选定的key,p1和p2之间的key都大于选定的key,那么当p2走到末尾时交换p1与key值便完成了一次切分。
*/
public class QuickSortList {
class ListNode {
ListNode next;
int val;
}
public ListNode sortList(ListNode head){
//采用快速排序
quickSort(head, null);
return head;
}
public static void quickSort(ListNode head, ListNode end){
if (head != end){
ListNode node = partion(head, end);
quickSort(head, node);
quickSort(node.next, end);
}
}
public static ListNode partion(ListNode head, ListNode end){
ListNode p1 = head, p2 = head.next;
//走到末尾才停
while (p2 != end){
//大于key值时,p1向前走一步,交换p1与p2的值
if (p2.val < head.val){
p1 = p1.next;
int temp = p1.val;
p1.val = p2.val;
p2.val = temp;
}
p2 = p2.next;
}
//当有序时,不交换p1和key值
if (p1 != head){
int temp = p1.val;
p1.val = head.val;
head.val = temp;
}
return p1;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.