06. 快速排序 #66
utterances-bot
started this conversation in
Comments
Replies: 5 comments 4 replies
-
三路快排def quickSort(nums, left, right):
if left >= right:
return
random_index = random.randint(left, right)
mid = nums[random_index]
nums[left], nums[random_index] = nums[random_index], nums[left]
i = left + 1
j = left
k = right + 1
while i < k:
if nums[i] < mid:
nums[i], nums[j + 1] = nums[j + 1], nums[i]
i += 1
j += 1
elif nums[i] > mid:
nums[i], nums[k - 1] = nums[k - 1], nums[i]
k -= 1
else:
i += 1
nums[left], nums[j] = nums[j], nums[left]
quickSort(nums, left, j - 1)
quickSort(nums, k, right) |
Beta Was this translation helpful? Give feedback.
0 replies
-
稍微写了下,感觉这样子对新手比较友好一些 |
Beta Was this translation helpful? Give feedback.
0 replies
-
@AldousShou 快排是原地排序 |
Beta Was this translation helpful? Give feedback.
2 replies
-
快排不应该是坑位互换吗?如果示例采用: [4,7,5,8,6,1,3],是不是就会有问题的? |
Beta Was this translation helpful? Give feedback.
1 reply
-
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
06.快速排序 | 算法通关手册
快速排序 # 1. 快速排序算法思想 # 快速排序(Quick Sort)基本思想: 通过一趟排序将无序序列分为独立的两个序列,第一个序列的值均比第二个序列
https://algo.itcharge.cn/01.Array/02.Array-Sort/06.Array-Quick-Sort/
Beta Was this translation helpful? Give feedback.
All reactions