Skip to content

Commit

Permalink
Create sort-colors.py
Browse files Browse the repository at this point in the history
classical quicksort
  • Loading branch information
gabedonnan authored Mar 21, 2023
1 parent f6fccaa commit 56ab32a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions sort-colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
def partition(low: int, high: int) -> int:
nonlocal nums
pivot = nums[high]
i = low
for j in range(low, high, 1):
if nums[j] <= pivot:
nums[i], nums[j] = nums[j], nums[i]
i += 1
nums[i], nums[high] = nums[high], nums[i]
return i

def quicksort(low: int, high: int) -> None:
nonlocal nums
if low < high:
qVal = partition(low, high)
quicksort(low, qVal - 1)
quicksort(qVal + 1, high)

quicksort(0, len(nums) - 1)

0 comments on commit 56ab32a

Please sign in to comment.