Quick Sort

A fast and widely used sorting algorithm based on divide and conquer.

Time Complexity

O(n log n)

Nice

Space Complexity

O(log n)

Very Fast

In-Place

Yes

Yes

Stability

No

No

Difficulty

Hard

★★★★☆

Description

Quick Sort is a highly efficient sorting algorithm based on the divide and conquer paradigm.

It works by selecting a pivot element and partitioning the array such that elements less than the pivot go to its left and elements greater go to its right. Then, it recursively sorts the two partitions.

Steps:

  1. Choose a pivot element
  2. Partition the array around the pivot
  3. Recursively sort the sub-arrays

Although the worst case is O(n^2), this can be avoided using randomized pivots or techniques like median-of-three. It is in-place and generally faster than Merge Sort for average cases.

Computational Complexity

O(n log n)
Best
O(n log n)
Average
O(n^2)
Worst
O(log n)
Spatial

Implementation