Skip to content

Commit 582fe03

Browse files
authored
Merge pull request crossoverJie#52 from ryandonglin/master
添加链表排序算法
2 parents 519ddad + 427834e commit 582fe03

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Java 知识点,继续完善中。
8181
- [合并两个排好序的链表](https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/MergeTwoSortedLists.java)
8282
- [两个栈实现队列](https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/TwoStackQueue.java)
8383
- [动手实现一个 LRU cache](http://crossoverjie.top/2018/04/07/algorithm/LRU-cache/)
84+
- [链表排序](./src/main/java/com/crossoverjie/algorithm/LinkedListMergeSort.java)
8485

8586
### Netty 相关
8687
- [SpringBoot 整合长连接心跳机制](https://crossoverjie.top/2018/05/24/netty/Netty(1)TCP-Heartbeat/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.crossoverjie.algorithm;
2+
3+
/**
4+
* 链表排序, 建议使用归并排序
5+
*
6+
* @author 6563699600@qq.com
7+
* @date 6/7/2018 11:42 PM
8+
* @since 1.0
9+
*/
10+
public class LinkedListMergeSort {
11+
12+
final static class Node {
13+
int e;
14+
Node next;
15+
16+
public Node() {
17+
}
18+
19+
public Node(int e, Node next) {
20+
this.e = e;
21+
this.next = next;
22+
}
23+
}
24+
25+
public Node mergeSort(Node first, int length) {
26+
27+
if (length == 1) {
28+
return first;
29+
} else {
30+
Node middle = new Node();
31+
Node tmp = first;
32+
33+
/**
34+
* 后期会对这里进行优化,通过一次遍历算出长度和中间元素
35+
*/
36+
for (int i = 0; i < length; i++) {
37+
if (i == length / 2) {
38+
break;
39+
}
40+
middle = tmp;
41+
tmp = tmp.next;
42+
}
43+
44+
/**
45+
* 这里是链表归并时要注意的细节
46+
*/
47+
Node right = middle.next;
48+
middle.next = null;
49+
50+
Node leftStart = mergeSort(first, length / 2);
51+
Node rightStart;
52+
if (length % 2 == 0) {
53+
rightStart = mergeSort(right, length / 2);
54+
} else {
55+
rightStart = mergeSort(right, length / 2 + 1);
56+
}
57+
return mergeList(leftStart, rightStart);
58+
}
59+
}
60+
61+
public Node mergeList(Node left, Node right) {
62+
63+
Node head = new Node();
64+
Node result = head;
65+
66+
/**
67+
* 思想就是两个链表同时遍历,将更的元素插入结果中,同时更更大的元素所属的链表的指针向下移动
68+
*/
69+
while (!(null == left && null == right)) {
70+
Node tmp;
71+
if (left == null) {
72+
result.next = right;
73+
break;
74+
} else if (right == null) {
75+
result.next = left;
76+
break;
77+
} else if (left.e >= right.e) {
78+
tmp = left;
79+
result.next = left;
80+
result = tmp;
81+
left = left.next;
82+
} else {
83+
tmp = right;
84+
result.next = right;
85+
result = tmp;
86+
right = right.next;
87+
}
88+
}
89+
90+
return head.next;
91+
}
92+
93+
public static void main(String[] args) {
94+
95+
Node head = new Node();
96+
97+
head.next = new Node(7,
98+
new Node(2,
99+
new Node(5,
100+
new Node(4,
101+
new Node(3,
102+
new Node(6,
103+
new Node(11, null)
104+
)
105+
)
106+
)
107+
)
108+
)
109+
);
110+
111+
int length = 0;
112+
113+
for (Node e = head.next; null != e; e = e.next) {
114+
length++;
115+
}
116+
117+
118+
LinkedListMergeSort sort = new LinkedListMergeSort();
119+
head.next = sort.mergeSort(head.next, length);
120+
121+
122+
for (Node n = head.next; n != null; n = n.next) {
123+
System.out.println(n.e);
124+
}
125+
126+
}
127+
}

0 commit comments

Comments
 (0)