mediumDSA

Compare common sorting algorithms.

219 views
01

Understand the problem

Explain bubble, merge, quick sort.

sorting
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Quicksort + mergesort
Run Playground
def quicksort(a):
    if len(a) <= 1: return a
    pivot = a[len(a) // 2]
    less = [x for x in a if x < pivot]
    eq   = [x for x in a if x == pivot]
    more = [x for x in a if x > pivot]
    return quicksort(less) + eq + quicksort(more)

def mergesort(a):
    if len(a) <= 1: return a
    mid = len(a) // 2
    l, r = mergesort(a[:mid]), mergesort(a[mid:])
    out, i, j = [], 0, 0
    while i < len(l) and j < len(r):
        if l[i] <= r[j]: out.append(l[i]); i += 1
        else: out.append(r[j]); j += 1
    return out + l[i:] + r[j:]


# --- demo ---
print(quicksort([5, 2, 8, 1, 9]))   # [1, 2, 5, 8, 9]
print(mergesort([5, 2, 8, 1, 9]))   # [1, 2, 5, 8, 9]
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.