美文网首页
九. Sort 3 Median

九. Sort 3 Median

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

    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)
    

    相关文章

      网友评论

          本文标题:九. Sort 3 Median

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