# 常见的链表排序（Java版）

上篇博客中讲解了九大内部排序算法，部分算法还提供了代码实现，但是那些代码实现都是基于数组进行排序的，本篇博客就以链表排序实现几种常见的排序算法，以飨读者。

``` 1     //快速排序
2     public static void quickSort(ListNode begin, ListNode end){
3         if(begin == null || begin == end)
4             return;
5
6         ListNode index = paration(begin, end);
7         quickSort(begin, index);
8         quickSort(index.next, end);
9     }
10
11     /**
12      * 划分函数，以头结点值为基准元素进行划分
13      * @param begin
14      * @param end
15      * @return
16      */
17     public static ListNode paration(ListNode begin, ListNode end){
18         if(begin == null || begin == end)
19             return begin;
20
21         int val = begin.val;  //基准元素
22         ListNode index = begin, cur = begin.next;
23
24         while(cur != end){
25             if(cur.val < val){  //交换
26                 index = index.next;
27                 int tmp = cur.val;
28                 cur.val = index.val;
29                 index.val = tmp;
30             }
31             cur = cur.next;
32         }
33
34
35         begin.val = index.val;
36         index.val = val;
37
38         return index;
39     }```

``` 1     //归并排序
2     public static ListNode mergeSort(ListNode head){
6
7         while(fast != null && fast.next != null){  //使用快慢指针寻找中间 结点
8             slow = slow.next;
9
10             fast = fast.next;
11             if(fast.next != null)
12                 fast = fast.next;
13         }
14
15         ListNode ptr1 = slow.next;
16         slow.next = null;
17
19         ListNode tmp2 = mergeSort(ptr1);
20         return merge(tmp1, tmp2);
21     }
22
23
24     public static ListNode merge(ListNode start1,  ListNode start2){
25         ListNode header = new ListNode(-1);
27
28         ListNode ptr1 = start1, ptr2 = start2;
29         while(ptr1 != null && ptr2 != null){
30             if(ptr1.val <= ptr2.val){
31                 pre.next = ptr1;
32                 pre = ptr1;
33                 ptr1 = ptr1.next;
34             }else{
35                 pre.next = ptr2;
36                 pre = ptr2;
37                 ptr2 = ptr2.next;
38             }
39         }
40         while(ptr1 != null){
41             pre.next = ptr1;
42             pre = ptr1;
43             ptr1 = ptr1.next;
44         }
45
46         while(ptr2 != null){
47             pre.next = ptr2;
48             pre = ptr2;
49             ptr2 = ptr2.next;
50         }
51
52
54
55     }```

``` 1     //冒泡排序
2     public static ListNode bubbleSort(ListNode head){
5
6         ListNode cur = null, tail = null;
7
9
10         while(cur.next != tail){
11             while(cur.next != tail){
12                 if(cur.val > cur.next.val){
13                     int tmp = cur.val;
14                     cur.val = cur.next.val;
15                     cur.next.val = tmp;
16                 }
17                 cur = cur.next;
18             }
19
20             tail = cur;  //下一次遍历的尾结点是当前结点(仔细琢磨一下里面的道道)
22         }
23
25
26
27     }```

原文作者：沧浪少年
原文地址: https://www.cnblogs.com/bywallance/p/6726251.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。