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;
        }
    };
  • 相关阅读:
    [转]ExtJS3.0与KindEditor4.1.2整合
    [转]Ext.grid常用属性和方法
    Extjs gridpanel 合并单元格
    [转]Httrack工具与使用指南
    [转]Teleport Ultra/Teleport Pro的冗余代码批量清理方法
    [转]最全的用正则批量去除Teleport Pro整站下载文件冗余代码
    Teleport Pro使用教程
    如何通过Dreamweaver批量对整个站点或目录进行代码搜索或部分全部替换
    [转]使用DW正则表达式批量替换实例介绍
    [转]tppabs是什么?如何去除tppabs?
  • 原文地址:https://www.cnblogs.com/lakeone/p/5756808.html
Copyright © 2011-2022 走看看