排序数组 (Leecode 912, 快排, 堆排, 归并排序, 冒泡排序)

快速排序

    # 快排
    def sortArray(self, nums: List[int]) -> List[int]:
        def partition(start, end, nums):
            flag = start - 1
            for k in range(start, end):
                if nums[k] < nums[end]:
                    flag += 1
                    nums[flag], nums[k] = nums[k], nums[flag]
            nums[flag+1], nums[end] = nums[end], nums[flag+1]
            return flag+1

        def quick_sort(i, j, nums):
            if i >= j:
                return
            index = partition(i, j, nums)
            quick_sort(i, index-1, nums)
            quick_sort(index+1, j, nums)
        
        quick_sort(0, len(nums)-1, nums)
        return nums

堆排序

 # 堆排序
    def sortArray(self, nums: List[int]) -> List[int]:
        def max_heapify(heap, size, root): # 默认左右子树都是调整好的堆,现在调整根节点
            left = root * 2 + 1
            right = root * 2 + 2
            larger = root
            if left < size and heap[left] > heap[larger]:
                larger = left
            if right < size and heap[right] > heap[larger]:
                larger = right
            if larger != root:
                heap[larger], heap[root] = heap[root], heap[larger]
                max_heapify(heap, size, larger)

        def build_heap(heap): # 建堆
            size = len(heap)
            start = size // 2 - 1
            for i in range(start, -1, -1):
                # print(i)
                max_heapify(heap, size, i)

        def heap_sort(nums):  # 堆排序
            build_heap(nums)
            for i in range(len(nums)-1, -1, -1):
                nums[0], nums[i] = nums[i], nums[0]
                max_heapify(nums, i, 0)
        
        heap_sort(nums)
        return nums

归并排序

# 归并排序
    def sortArray(self, nums: List[int]) -> List[int]:
        def merge(nums1, nums2):
            res = []
            i = 0
            j = 0
            while i < len(nums1) and j < len(nums2):
                if nums1[i] <= nums2[j]:
                    res.append(nums1[i])
                    i += 1
                else:
                    res.append(nums2[j])
                    j += 1
            if i < len(nums1):
                for k in range(i, len(nums1)):
                    res.append(nums1[k])
            if j < len(nums2):
                for k in range(j, len(nums2)):
                    res.append(nums2[k]) 
            return res

        def mergesort(nums):
            if len(nums) <= 1:
                return nums
            mid = len(nums) // 2
            left = mergesort(nums[:mid])
            right = mergesort(nums[mid:])
            return merge(left, right) 
        
        return mergesort(nums)
            

冒泡排序

# 冒泡排序
    def sortArray(self, nums: List[int]) -> List[int]:
        for i in range(len(nums)): # 控制循环次数
            flag = True
            for j in range(1, len(nums)-i): # 冒泡
                if nums[j] < nums[j-1]:
                    nums[j], nums[j-1] = nums[j-1], nums[j]
                    flag = False
            print(nums)
            if flag:
                return nums
        return nums
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值