Coding interview : 链表排序(选择,插入,快排,归并)

本文章分别给出链表排序的几种方法,对于学习如何操作链表很有帮助。

选择排序

public class SelectSort {
//节点定义
    static class Node {
        Node next;
        int v;

        Node(int v){
            this.v = v;
        }
    }
//排序函数
    public static Node sort(Node head){
        Node ph = new Node(0);
        ph.next = head;

        Node nph = new Node(0);
        Node npt = nph;

        Node min;
        while((min = getMin(ph))!=null){
            npt.next = min;
            npt = npt.next;
        }
        return nph.next;

    }
//获取剩余链表中最小的节点
    private static Node getMin(Node ph){

        if(ph.next==null) return null;

        Node pmin = ph;

        Node tmp = ph;
        while(tmp.next!=null){
            if(tmp.next.v< pmin.next.v){
                pmin = tmp;
            }

            tmp = tmp.next;
        }

        Node min = pmin.next;
        pmin.next = pmin.next.next;
        min.next=null;
        return min;
    }
//测试main函数入口
    public static void main(String[]args){
        Node h = new Node(0);

        Node r = h;
        for(int i=10;i>=1;i--){
            r.next = new Node(new Random().nextInt(100));
            r = r.next;
        }

        Node nh = sort(h);

        while(nh!=null){
            System.out.println(nh.v);
            nh = nh.next;
        }
    }
}

插入排序

public class InsertSort {
//节点定义
    static class Node{
        Node next;
        int v;

        Node(int v){
            this. v = v;
        }
    }
//排序
    public static Node sort(Node h){
        Node cur = h;
        Node nh = new Node(0);
        Node next;
        while (cur!=null){
            next = cur.next;
            insertHelper(nh,cur);
            cur = next;
        }

        return nh.next;
    }
//插入到已排序的链表之中
    private static void insertHelper(Node nh,Node n){
        Node p = nh;
        while(p.next!=null){
            if(p.next.v > n.v) {
                break;
            }
            p = p.next;
        }

        n.next = p.next;
        p.next = n;
    }
//测试函数入口
    public static void main(String[]args){
        Node h = new Node(0);
        Node p = h;
        for(int i=1;i<100;i++){
            p.next = new Node(new Random().nextInt(100));
            p = p.next;
        }
        Node nh = sort(h);
        while(nh!=null){
            System.out.println(nh.v);
            nh = nh.next;
        }
    }
}

快速排序

public class QuickSort {

//节点定义
    static class Node {
        Node next;
        int v;

        Node(int v){
            this.v = v;
        }
    }
//排序
    public static Node sort(Node head){

        if (head == null)
            return null;
        Node less = new Node(0);
        Node lessT = less;
        Node eq = new Node(0);
        Node eqT = eq;
        Node large = new Node(0);
        Node largeT = large;

        Node cur = head, next = null;

        int key = cur.v;
        while(cur!=null){
            next = cur.next;
            if(cur.v == key) {
                cur.next = eqT.next;
                eqT.next = cur;
                eqT = eqT.next;
            } else if (cur.v< key){
                cur.next = lessT.next;
                lessT.next = cur;
                lessT = lessT.next;
            }else if(cur.v > key){
                cur.next = largeT.next;
                largeT.next = cur;
                largeT = largeT.next;
            }
            cur = next;
        }
        Node lessH = sort(less.next);

        Node largeH = sort(large.next);

        Node nH = null,nT = null;


        if(lessH!=null){
            nH = lessH;
            nT = lessH;
            while(nT.next!=null){
                nT = nT.next;
            }
        }

        if(eq.next!=null){
            if(nH==null){
                nH = eq.next;
            }
            if(nT!=null){
                nT.next = eq.next;
            }
            nT = eq.next;
            while(nT.next!=null){
                nT = nT.next;
            }
        }

        if(largeH!=null){
            if(nH==null){
                nH = largeH;
            }
            if(nT!=null){
                nT.next = largeH;
            }
        }
        return nH;
    }
//测试入口
    public static void main(String[]args){
        Node head = new Node(0);
        Node p = head;
        for(int i=1;i<10;i++){
            p.next = new Node(new Random().nextInt(1000));
            p = p.next;
        }

        Node q = sort(head);

        while(q!=null){
            System.out.println(q.v);
            q = q.next;
        }
    }
}

归并排序

//节点定义
static class Node{
        int v;
        Node next;

        Node(int v){
            this.v = v;
        }
    }
//排序
    public static Node sort(Node head){
        if(head==null) return null;

        if(head.next == null) return head;

        Node slow = head,fast = head.next;

        while(fast !=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }

        Node mid = slow;
        Node firstSeg = head;
        Node secondSeg = mid.next;
        mid.next = null;

        Node left = sort(firstSeg);
        Node right = sort(secondSeg);

        Node leftNext,rightNext;

        Node nHead = new Node(0);
        Node nTail = nHead;
        while(left!=null && right!=null){
            if(left.v<right.v){
                leftNext = left.next;
                left.next = nTail.next;
                nTail.next = left;
                nTail = nTail.next;
                left = leftNext;
            } else {
                rightNext = right.next;
                right.next = nTail.next;
                nTail.next = right;
                nTail = nTail.next;
                right = rightNext;
            }
        }

        if (left != null) {
            nTail.next = left;
        }

        if (right != null) {
           nTail.next = right;
        }
        return nHead.next;
    }
//测试入口
    public static void main(String[]args){
        Node h = new Node(0);
        Node p = h;
        for(int i=1;i<10;i++){
            p.next = new Node(new Random().nextInt(100));
            p = p.next;
        }

        Node nh = sort(h);

        while(nh!=null){
            System.out.println(nh.v);
            nh = nh.next;
        }
    }

猜你喜欢

转载自blog.csdn.net/zhumingyuan111/article/details/79605157
今日推荐