[LeetCode] 148. Sort List (Linked List) - Using Quick Sort(小改动)

Quick Sort 的 原始 Psuedo Code.


原Quick Sort算法,如果遇到Linked list/Array中有非常多相同的元素时,性能会非常差。

(假设现在输入的是10000个node.val = 1的linked list. 那每次循环,都是只剔除掉一个元素,那要10000次quicksort递归... Orz.)

因此在每次Paritition过程中,将链表分成3段, Left, Reference, Right。(其中Left 与 Right 可能为空。) 

与参照值reference值相同的,全部放入reference这条链表中, 而这条链表在后续过程中是不需要继续递归的。

在Quick Sort的递归过程中,分别讨论Left, Right为空时的情况,来将三条链表链接到一起。


class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def sortList(self, head):
        """
        sort list using quick sort
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None:
            return None
        tail = self.get_tail(head)
        head, tail = self.quick_sort(head, tail)
        tail.next = None
        return head

    def quick_sort(self, head, tail):
        """
        Sort in place
        :param head:
        :param tail:
        :return:
        """
        if head is not tail:
            head_left, tail_left, head_ref, tail_ref, head_right, tail_right = self.quicksort_partition(head, tail)
            if head_left is None:  # if there is no node in left part after partition
                head = head_ref
            else:
                head_left, tail_left = self.quick_sort(head_left, tail_left)
                head = head_left
                tail_left.next = head_ref
            if head_right is None:  # if there is no node in right part after partition
                tail = tail_ref
            else:
                head_right, tail_right = self.quick_sort(head_right, tail_right)
                tail_ref.next = head_right
                tail = tail_right
        return head, tail


    def quicksort_partition(self, head, tail):
        reference = tail
        head_ref, tail_ref = reference, reference
        head_left, tail_left, head_right, tail_right = None, None, None, None

        sentinel = ListNode(None)  # use sentinel to simplify the code
        sentinel.next = head
        node = sentinel
        while node.next is not tail:
            node = node.next
            if node.val > reference.val:  # put node into right part
                if head_right is not None:
                    tail_right.next = node
                    tail_right = node
                else:  # right part is empty
                    head_right = node
                    tail_right = node
            elif node.val < reference.val:  # put node into left part
                if head_left is not None:
                    tail_left.next = node
                    tail_left= node
                else:  # left part is empty
                    head_left = node
                    tail_left = node
            else:  # put node into reference part
                tail_ref.next = node
                tail_ref = node
        return head_left, tail_left, head_ref, tail_ref, head_right, tail_right

    def get_tail(self, node):
        while node.next:
            node = node.next
        return node



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值