美文网首页
九. Sort 1 mergesort and quicksor

九. Sort 1 mergesort and quicksor

作者: 何大炮 | 来源:发表于2018-03-23 12:04 被阅读0次

    Comparing the mergesort and quicksort, the mergesort needs O(n) space complexity to realize, while quicksort doesn't need it, for the exchange is utilised here. However, the space complexity is log n, for a stack is necessary for its recursion.

    As we see, the differences between them are the method after the comparison, as mergesort uses a new array to deposit result of comparison while quicksort take advantage of exchange to rearrange an order of elements.

    All in all, the time complexity of them are nlogn.(height = logn, maximum of comparisons in a level = n )

    More info about Quick sort

    class Solution:
        """
        @param A: an integer array
        @return: nothing
        """
        def mergesort(self, A):
            mid = int(len(A)/2)
            if mid == 0 or not A:
                return A
    
            A_1 = self.mergesort(A[:mid])
            A_2 = self.mergesort(A[mid:])
    
            if not A_2 or not A_1:
                return A_1 + A_2
    
            i = 0
            j = 0
            merge = []
            while j < len(A_2) and i < len(A_1):
                if A_1[i] > A_2[j]:
                    merge.append(A_2[j])
                    j += 1
                else:
                    merge.append(A_1[i])
                    i += 1
    
            merge.extend( A_1[i:])
            merge.extend( A_2[j:])
                    
            return merge
    
    
        def quiksort(self, A, low, high):
            if low < high:
                pivot = self.pivot_count(A,low, high)
                self.quiksort(A, low, pivot-1)
                self.quiksort(A, pivot+1, high)
    
    
        def pivot_count(self, A, low,high):
            pivot = A[high]
            leftwall = low
    
            # divide 2 sides
            for i in range(low, high):
                if A[i] < pivot:
                    A[leftwall], A[i] = A[i], A[leftwall]
                    leftwall += 1
            # rebuild a pivot
            A[leftwall], A[high] = A[high], A[leftwall]
    
            return leftwall
    

    quick_sort 改进
    思路:因为key的值最好每次都能把数组分成二分的两半,所以key的值最好是区域内比较居中的值,所以每次把区域内的首元素、尾元素、中间的元素做比较,选出不大不小的那个,然后把选出来的这个值,交换到数组的尾部,以便调整后它能回到数组中间的位置

    class Solution:
        """
        @param A: an integer array
        @return: nothing
        """
        def sortIntegers2(self, A):
             # 快排  三者取中
            def quick_sort(low, high, array):
                if low < high:
                    pivot, array = find_pivot(low, high, array)
                    quick_sort(low, pivot-1, array)
                    quick_sort(pivot+1, high, array)
    
    
            def find_pivot(low, high, array):
            # 取中
                middle = (low+high)//2
                list_3 = [array[low], array[middle], array[high]]
            # 冒泡排序
                for i in (0, 1):
                    for j in range(i, 2):
                        if list_3[i] < list_3[i + 1]:
                            list_3[i], list_3[i + 1] = list_3[i + 1], list_3[i]
            # 调换位置
                array[low] = list_3[0]
                array[middle] = list_3[2]
                array[high] = list_3[1]
                array_return = array
    
                pivot = high
                left_wall = low
                for i in range(low, high):
                    if array[i] < array[high]:
                        array[i], array[left_wall] = array[left_wall], array[i]
                        left_wall += 1
    
                array[left_wall], array[pivot] = array[pivot], array[left_wall]
    
                return left_wall, array_return
             
            quick_sort(0, len(A)-1, A)
    

    相关文章

      网友评论

          本文标题:九. Sort 1 mergesort and quicksor

          本文链接:https://www.haomeiwen.com/subject/onntcftx.html