Explain bubble, merge, quick sort.
Skip to solutionKEEP THE
mediumDSA
Compare common sorting algorithms.
219 views
01
Understand the problem
sorting
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Bubble/insertion are O(n²), simple, fine for tiny inputs. Merge sort is stable O(n log n) with O(n) extra space. Quicksort averages O(n log n) in place but O(n²) worst case with bad pivots. Most language libraries use hybrids (e.g. Timsort, introsort).
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Quicksort + mergesort
Run Playgrounddef 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.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 89 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.