Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Quick Sort Algorithm

Last updated: 27 Sept 20269 min read
algorithm
MediumBy AITrove Editorial

Divide-and-conquer sorting using a pivot and partitioning.

Quick sort picks a pivot and partitions the array around it.

cpp
int partition(vector<int>& a, int lo, int hi) {
  int p = a[hi], i = lo - 1;
  for (int j = lo; j < hi; j++) if (a[j] < p) swap(a[++i], a[j]);
  swap(a[i + 1], a[hi]);
  return i + 1;
}
CaseTime
AverageO(n log n)
WorstO(n²)
dsa
sorting
Storage details