zoukankan      html  css  js  c++  java
  • 183.面试题 17.14. 最小K个数(快速排序)

    简单画图

    class Solution(object):
        def smallestK(self, arr, k):
            """
            :type arr: List[int]
            :type k: int
            :rtype: List[int]
            """
            self.quick_sort(arr, 0, len(arr) - 1)
            print(arr)
    
        def quick_sort1(self, arr, start, end):
            """快排1"""
            if start >= end: return
            left, right = start, end
            temp = arr[left]
            while left < right:
                while left < right and arr[right] >= temp:
                    right -= 1
                arr[left] = arr[right]
    
                while left < right and arr[left] <= temp:
                    left += 1
                arr[right] = arr[left]
    
            arr[left] = temp
            self.quick_sort(arr, start, left-1)
            self.quick_sort(arr, left+1, end)
    
        def quick_sort(self, arr, start, end):
            """快排2: 对应图"""
            if start >= end: return
            left, right = start, end
            temp = arr[left]
            while left < right:
                while left < right and arr[right] >= temp:
                    right -= 1
                while left < right and arr[left] <= temp:
                    left += 1
                arr[right], arr[left] = arr[left], arr[right]
    
            arr[start], arr[left] = arr[left], temp
            self.quick_sort(arr, start, left-1)
            self.quick_sort(arr, left+1, end)
    
    
    s1 = Solution()
    arr = [1,3,5,7,2,4,6,8,1]; k = 4
    root = s1.smallestK(arr, k)
    print(root)
    
  • 相关阅读:
    架构设计
    git 常用命令
    C# 加载C++的dll
    windows 服务部署管理
    wpf 模板绑定控件属性
    golang开启module模式 go mod
    使用docker安装redis
    使用docker安装elasticsearch
    使用docker安装etcd
    使用docker安装mysql5.7
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/15222318.html
Copyright © 2011-2022 走看看