三路快排是一种高效的快速排序算法,特别适用于处理数组中存在大量重复元素的情况。下面是一个使用Python实现的三路快排的示例代码:
```python
def quick_sort(nums):
if len(nums) <= 1:
return nums
pivot = nums[0]
lt = [x for x in nums if x < pivot]
eq = [x for x in nums if x == pivot]
gt = [x for x in nums if x > pivot]
return quick_sort(lt) + eq + quick_sort(gt)
# 示例
nums = [5, 2, 9, 1, 5, 7, 3, 5]
sorted_nums = quick_sort(nums)
print(sorted_nums) # 输出:[1, 2, 3, 5, 5, 5, 7, 9]
```