zoukankan      html  css  js  c++  java
  • 链表的归并排序与快速排序

    链表数据结构

    struct ListNode {
        int val;
        ListNode *next;
        ListNode(int x) : val(x), next(NULL) {}
    };

    链表归并排序

    class Solution {
    public:
        ListNode* sortList(ListNode* head) {
            if (head == nullptr || head->next == nullptr)
                return head;
            ListNode *fast = head, *slow = head;
            while (fast->next && fast->next->next)
            {
                slow = slow->next;
                fast = fast->next->next;
            }
    
            fast = slow->next;
            slow->next = nullptr;
            head = sortList(head);
            fast = sortList(fast);
            return merge(head, fast);
        }
    
    private:
        ListNode* merge(ListNode *l1, ListNode *l2)
        {
            ListNode empty(-1);
            ListNode *tail = ∅
            ListNode *tmp;
            for (; l1 || l2; tail = tail->next)
            {
                if (l1 == nullptr)
                {
                    tail->next = l2;
                    break;
                }
                if (l2 == nullptr)
                {
                    tail->next = l1;
                    break;
                }
                if (l1->val <= l2->val)
                {
                    tmp = l1;
                    l1 = l1->next;
                }
                else
                {
                    tmp = l2;
                    l2 = l2->next;
                }
                tail->next = tmp;
            }
    
            return empty.next;
        }
    };

    基于值交换的链表快速排序

    class Solution {
    public:
        ListNode* sortList(ListNode* head) {
            return sort(head, nullptr);
        }
    private:
        void swap(ListNode *a, ListNode *b)
        {
            int tmp = a->val;
            a->val = b->val;
            b->val = tmp;
        }
        ListNode *sort(ListNode *begin, ListNode *end)
        {
            if (begin == nullptr || begin == end || begin->next == end)
                return begin;
            int key = begin->val;
            ListNode *p = begin;
            ListNode *q = begin->next;
            for (; q != end; q = q->next)
            {
                if (q->val < key)
                {
                    p = p->next;
                    swap(p, q);
                }
            }
            swap(p, begin);
            sort(begin, p);
            sort(p->next, end);
    
            return begin;
        }
    };


    基于节点交换的链表快速排序

    class Solution {
    public:
        ListNode* sortList(ListNode* head) {
            return sort(head, nullptr);
        }
    private:
        ListNode *sort(ListNode *begin, ListNode *end)
        {
            if (begin == nullptr || begin == end || begin->next == end)
                return begin;
            ListNode *part = patition(begin, end);
    
            ListNode *first = sort(begin, part);
            ListNode *second = sort(part->next, end);
            part->next = second;
    
            return first;
        }
    
        ListNode *patition(ListNode * &begin, ListNode *end)
        {
            ListNode *head = begin;
            ListNode *tail = begin;
            ListNode *cur = begin->next;
            while (cur != end)
            {
                if (cur->val < begin->val)
                {
                    tail->next = cur->next;
                    cur->next = head;
                    head = cur;
                    cur = tail;
                }
    
                tail = cur;
                cur = cur->next;
            }
            ListNode *part = begin;
            begin = head;
            return part;
        }
    };
  • 相关阅读:
    linux启动流程
    树-二叉平衡树AVL
    算法导论第六章 堆排序
    算法导论基础(第一~五章)
    树-二叉查找树
    Java:基础
    【转】为什么C++编译器不能支持对模板的分离式编译
    压缩和解压缩命令
    Makefile编程
    1.什么是Mybatis?
  • 原文地址:https://www.cnblogs.com/lakeone/p/5756808.html
Copyright © 2011-2022 走看看