"""3-way Quicksort
Simpler to implement than basic quicksort
"""
from random import randint
def qs_3_way(nums):
# Sorting
if len(nums) > 1:
pivot = nums[randint(0, len(nums)-1)]
smaller, greater, equals = [], [], []
for num in nums:
if num < pivot:
smaller.append(num)
elif num > pivot:
greater.append(num)
else:
equals.append(num)
# Recursive Partition
return qs_3_way(smaller) + equals + qs_3_way(greater)
return nums # for easy testing