九. Sort 3 Median

Idea:Here we still use the method 'quicksort'

class Solution:
    """
    @param nums: A list of integers
    @return: An integer denotes the middle number of the array
    """
    def median(self, nums):
        # write your code here
        def quicksort(nums, low, high):
            pivot = get_pivot(nums, low, high)
            if pivot == (len(nums)-1)//2:
                return nums[pivot]
            else:
                if pivot > (len(nums)-1)//2:
                    return quicksort(nums, low, pivot-1)
                else:
                    print(pivot)
                    return quicksort(nums, pivot+1, high)
        
        def get_pivot(nums, low, high):
            pivot = high
            leftwall = low
            for i in range(low, high):
                if nums[pivot] >= nums[i]:
                    nums[leftwall], nums[i] = nums[i], nums[leftwall]
                    leftwall +=1
            
            nums[leftwall], nums[high] = nums[high], nums[leftwall]
            
            return leftwall
            
        return quicksort(nums, 0, len(nums)-1)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,499评论 0 10
  • 我们总在说要做一个坚强独立的人,可只有自己知道,很多时候你其实也想要一个把你当成小孩子,宠着你的人。 前一段时间因...
    筱陌_阅读 763评论 0 0
  • gulp是基于node的前端工具,可以对代码压缩,自动刷新页面,编译各种预处理器,给css加浏览器前缀,加版本号,...
    Jser_zk阅读 485评论 1 1
  • 我和老张相识于高中,我在认识他两个月时把他搞定。 高中三年,我们一起经历了老师,父母的反对,走过所有人的不看好,我...
    七十与他阅读 158评论 1 2