Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Visualizer › Sorting

Quick Sort

Picks a pivot, partitions the array so smaller elements go left and bigger ones go right, then sorts both sides recursively.

How it works

Complexity

Code (Python)

def quick_sort(a, lo=0, hi=None):
    if hi is None:
        hi = len(a) - 1
    if lo < hi:
        pivot, i = a[hi], lo
        for j in range(lo, hi):
            if a[j] < pivot:
                a[i], a[j] = a[j], a[i]
                i += 1
        a[i], a[hi] = a[hi], a[i]     # pivot in place
        quick_sort(a, lo, i - 1)
        quick_sort(a, i + 1, hi)
    return a

Practise: Arrays problems