Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Quick Sort proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Partition a range around a pivot, finalize that pivot, then sort only the remaining sides. Understand duplicate policy, imbalance and why a deterministic last-pivot Lab does not guarantee expected bounds.
1 practice exercise4 interview problems
Recognize it when A comparison sort may trade stability for partitioning and low extra storage.
Checking your account…
Partitioning rearranges one range so values on one side satisfy the pivot relation and values on the other side do not. The pivot reaches a final position for that partition scheme.
During Lomuto partition, the prefix is less than or equal to pivot, the middle has been inspected and is greater, and the suffix is unseen. Swapping an accepted value grows the first region.
def partition_trace(values):
values=values.copy();pivot=values[-1];boundary=0;trace=[]
for index in range(len(values)-1):
if values[index]<=pivot:
values[boundary],values[index]=values[index],values[boundary];trace.append(values.copy());boundary+=1
values[boundary],values[-1]=values[-1],values[boundary];trace.append(values.copy());return traceImplement partition_trace(values). Use the last value as pivot and return array states after each swap, including final pivot placement. values must be nonempty.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def partition_trace(values):
values=values.copy();pivot=values[-1];boundary=0;trace=[]
for index in range(len(values)-1):
if values[index]<=pivot:
values[boundary],values[index]=values[index],values[boundary];trace.append(values.copy());boundary+=1
values[boundary],values[-1]=values[-1],values[boundary];trace.append(values.copy());return tracePredict the two snapshots returned by the existing Lomuto Lab. Rejected comparisons produce no row; accepted swaps, including a swap with itself, do.
| Step | Executed statement | State | Why |
|---|---|---|---|
| Choose pivot | values=values.copy();pivot=values[-1];boundary=0;trace=[] |
pivot=2, boundary=0 |
The copied input permits rearrangement without changing the caller list. |
| Reject 4 | if values[index]<=pivot: |
index=0, boundary=0 |
4 belongs to the greater-than region. |
| Accept 1 | values[boundary],values[index]=values[index],values[boundary];trace.append(values.copy());boundary+=1 |
values=[1, 4, 3, 2], boundary then 1 |
The swap extends the at-most-pivot prefix by one item. |
| Reject 3 | if values[index]<=pivot: |
index=2, boundary=1 |
The inspected 3 remains on the greater side. |
| Place pivot | values[boundary],values[-1]=values[-1],values[boundary] |
values=[1, 2, 3, 4] |
The pivot is now final for this range; neither recursive side should include index 1. |
| Output | |
|---|---|
print(partition_trace([4, 1, 3, 2])) |
[[1, 4, 3, 2], [1, 2, 3, 4]] |
A sorted result for this small partition does not mean partition generally sorts either side. The second Lab recurses on both sides and exposes the worst-case depth of its deterministic pivot choice.
After pivot placement, exclude the pivot from both recursive ranges. Base cases of size zero or one prevent repeated boundaries and guarantee termination.
def quick_sort(values):
values=values.copy()
def sort(left,right):
if left>=right:return
pivot=values[right];boundary=left
for index in range(left,right):
if values[index]<=pivot:values[boundary],values[index]=values[index],values[boundary];boundary+=1
values[boundary],values[right]=values[right],values[boundary]
sort(left,boundary-1);sort(boundary+1,right)
sort(0,len(values)-1);return valuesImplement quick_sort(values). Return a sorted copy using in-place partitioning on the copy.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def quick_sort(values):
values=values.copy()
def sort(left,right):
if left>=right:return
pivot=values[right];boundary=left
for index in range(left,right):
if values[index]<=pivot:values[boundary],values[index]=values[index],values[boundary];boundary+=1
values[boundary],values[right]=values[right],values[boundary]
sort(left,boundary-1);sort(boundary+1,right)
sort(0,len(values)-1);return valuesBalanced partitions give O(n log n); consistently extreme partitions give O(n squared) and O(n) recursion depth. Random pivots give the expected bound for distinct keys, but do not repair this two-way partition on all-equal input: every pivot still leaves n minus one values on one side. A three-way equal region avoids that duplicate-driven degeneration. To bound recursion depth, recurse on the smaller side and process the larger side iteratively; visiting the smaller side first while still recursively calling both sides does not itself bound the stack.
Say: “These regions are the partition invariant; each scan step classifies one value, then the pivot is final.” State duplicate policy, input mutation, average and worst-case time, and recursion-space behavior.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Quick Sort proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Quicksort with random/order-balanced pivots | O(n log n) | O(n^2) | The expected random-pivot bound assumes distinct keys or duplicate-aware partitioning. This deterministic two-way <= Lab is quadratic on all-equal inputs even if pivot positions are randomized. |
The Lab copies O(n) input storage and uses O(log n) balanced/O(n) worst-case frames.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
If the pivot reaches right, sort(left, boundary) repeats the whole range.
Prevent it: Exclude its finalized index from the child interval.
next_right = boundarynext_right = boundary - 1Where you will hit this: Partition and Quick Sort(opens in a new tab)
Retaining only one pivot while using strict lower/higher groups deletes duplicate values.
Prevent it: Keep every equal value in a three-way partition, or retain duplicates in the in-place scheme.
equal = [pivot]equal = [value for value in values if value == pivot]Where you will hit this: Partition and Quick Sort(opens in a new tab)
Appending the same mutable list makes earlier trace entries show later swaps.
Prevent it: Copy only for an intentional trace snapshot; this copying is not free.
trace.append(values)trace.append(values.copy())Where you will hit this: Car Fleet(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27
python-docs · checked 2026-07-27