# Quicksort

> A fast sort. It splits items around a pivot, then sorts each side.

**Field:** Software Development  
**Category:** Algorithms & Data Structures  
**Also known as:** Quick Sort, Partition-Exchange Sort

## Think of it like

Think of it like lining up kids. Pick one kid; short kids go left, tall kids go right.

## Definition

Quicksort picks one item, called the pivot, and puts smaller items to its left. Larger items go to its right. Then it does the same to each side, until every piece is sorted. It sorts in place, so it needs little extra memory. On average it takes O(n log n) steps and is very fast in practice. With a bad pivot every time, it slows to O(n²).

## Example

Emma's quicksort picks 5 as the pivot for 8, 3, 5, 9, 1. It moves 3 and 1 left and 8 and 9 right, then sorts each side.

## Why it matters

It is one of the fastest sorts in real use, and many built-in sorts start from it.

## Common mistake

Always picking the first item as the pivot. On a list that is already sorted, that hits the slow O(n²) case.

## Three principles

- **Pick a pivot.** Smaller left, larger right.
- **Repeat on each side.** Until all is sorted.
- **Fast on average.** Slow with bad pivots.

## Related terms

- [Merge Sort](https://learn.benibix.com/term/development/merge-sort)
- [Sorting Algorithm](https://learn.benibix.com/term/development/sorting-algorithm)
- [Divide and Conquer](https://learn.benibix.com/term/development/divide-and-conquer)
- [Recursion](https://learn.benibix.com/term/development/recursion)

## Told apart from

- [Merge Sort vs Quicksort](https://learn.benibix.com/compare/merge-sort-vs-quicksort): Merge sort and quicksort are both fast, but they differ. Merge sort is always O(n log n) and needs extra memory. Quicksort sorts in place and is often faster, but bad pivots make it slow.

---

Source: https://learn.benibix.com/term/development/quicksort
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
