1. 程式人生 > >Java單鏈表歸併排序

Java單鏈表歸併排序

概念

歸併排序是建立在歸併操作上的一種有效的排序演算法。該演算法是採用分治法(Divide and Conquer)的一個非常典型的應用,歸併排序將兩個已排序的表合併成一個表。

歸併排序基本原理

通過對若干個有序結點序列的歸併來實現排序。
所謂歸併是指將若干個已排好序的部分合併成一個有序的部分。

這裡寫圖片描述

單鏈表實現歸併排序

找到中間點拆分連結串列

//找到中間點,然後分割
    public ListNode getMiddle(ListNode head) {
        if (head == null) {
            return head;
        }
        //快慢指標
ListNode slow, fast; slow = fast = head; while (fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } return slow; }

合併排好序的連結串列

// 合併排好序的連結串列
    public ListNode merge(ListNode a, ListNode b) {
        ListNode dummyHead, curr;
        dummyHead = new
ListNode(0); curr = dummyHead; while (a != null && b != null) { if (a.val <= b.val) { curr.next = a; a = a.next; } else { curr.next = b; b = b.next; } curr = curr.next; } curr.next = (a == null
) ? b : a; return dummyHead.next; }

單鏈表的歸併排序

//單鏈表的歸併排序
    public ListNode merge_sort(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        //得到連結串列中間的數
        ListNode middle = getMiddle(head);
        ListNode sHalf = middle.next;
        //拆分連結串列
        middle.next = null;
        //遞迴呼叫
        return merge(merge_sort(head), merge_sort(sHalf));
    }

Application.java

public static void main(String[] args) {
        ListNode head = new ListNode(0);
        ListNode l1 = new ListNode(2);
        ListNode l2 = new ListNode(5);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(8);
        ListNode l5 = new ListNode(4);
        ListNode l6 = new ListNode(2);
        ListNode l7 = new ListNode(1);

        head.next = l1;
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        l5.next = l6;
        l6.next = l7;
        l7.next = null;

        ListNode p = head;
        while (p.next != null) {
            System.out.print(p.val);
            p = p.next;
        }
        System.out.print(p.val);
        System.out.println();

        new MergeSort().merge_sort(head);

        p = head;
        while (p != null) {
            System.out.print(p.val);
            p = p.next;
        }
    }

執行效果圖

這裡寫圖片描述