Skip to content

Instantly share code, notes, and snippets.

@potatowagon
Last active July 31, 2021 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potatowagon/9069aae31a2ea710dd78547b903c6f51 to your computer and use it in GitHub Desktop.
Save potatowagon/9069aae31a2ea710dd78547b903c6f51 to your computer and use it in GitHub Desktop.

Code

class Solution:
    def sortArray(self, nums: List[int]) -> List[int]:
        def partition(nums, start, end):
            i = start
            p = nums[end]
            j = end - 1
            while i < j:
                while nums[i] < p:
                    i += 1
                while nums[j] >= p and i < j:
                    j -= 1
                if i < j:
                    nums[j], nums[i] = nums[i], nums[j]
            if nums[j] >= p :
                nums[j], nums[end] = p, nums[j]
                return nums, j
            return nums, end
                     
        def qs(nums, start, end):
            
            if start >= end:
                return nums
            nums, p = partition(nums, start, end)
            nums = qs(nums, start, p-1)
            nums = qs(nums, p+1, end)
                
            return nums
        return qs(nums, 0, len(nums)-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment