def partition(tup, f, lt=(), gt=()):
    if len(tup) == 0: return lt, gt
    x, *xs = tup 
    if f(x):
      return partition(xs, f, (x, *lt), gt)
    return partition(xs, f, lt, (x, *gt))
      
def qsort(tup):
  if len(tup) == 0: return ()
  x, *xs = tup 
  # lt = tuple(a for a in xs if a <= x)
  # gt = tuple(b for b in xs if b > x)
  lt, gt = partition(xs, lambda a: a <= x)
  return (*qsort(lt), x, *qsort(gt))

print(qsort((5,3,1,4,2))) 

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: