-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.py
38 lines (33 loc) · 1.01 KB
/
quicksort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# quick sort
def partition(nums, start=0, end=None):
# print('partition', nums, start, end)
if end is None:
end = len(nums) - 1
l, r = start, end-1
while r > l:
if nums[l] <= nums[end]:
l += 1
# Decrement right pointer if number is greater than pivot
elif nums[r] > nums[end]:
r -= 1
# Two out-of-place elements found, swap them
else:
nums[l], nums[r] = nums[r], nums[l]
# print(' ', nums, l, r)
# Place the pivot between the two parts
if nums[l] > nums[end]:
nums[l], nums[end] = nums[end], nums[l]
return l
else:
return end
def quicksort(nums, start=0, end=None):
# print('quicksort', nums, start, end)
if end is None:
nums = list(nums)
end = len(nums) - 1
if start < end:
pivot = partition(nums, start, end)
quicksort(nums, start, pivot-1)
quicksort(nums, pivot+1, end)
return nums
quicksort([3,4,2,8,4,5])