Quick Sort

Quick Sort is a divide-and-conquer algorithm that selects a pivot element, partitions the array into two sub-arrays (elements less than and greater than the pivot), and recursively sorts them.

Quick Sort is efficient for large datasets with average time complexity of O(n log n), but in the worst case (already sorted array), it becomes O(n²).

Example

// Quick Sort in JavaScript
function quickSort(arr) {
  if (arr.length < 2) return arr;

  const pivot = arr[0];
  const left = arr.slice(1).filter(el => el <= pivot);
  const right = arr.slice(1).filter(el => el > pivot);

  return [...quickSort(left), pivot, ...quickSort(right)];
}

console.log(quickSort([8, 4, 7, 3, 9, 1])); // Output: [1, 3, 4, 7, 8, 9]
Quick Sort

Build Array

6 elements

Start with an empty array of a chosen length. New slots start as NULL — fill them with Push or Insert below.

Sort

Picks a pivot, partitions smaller values to its left and larger to its right, then repeats on each side.

Partitions: 0Comparisons: 0
Speed
Array Visualizer
6 elements
0
1
2
3
4
5
20
64
101
95
7
80
Pivot
Scanning
Placed correctly
Real-life Use (Quick Sort)

Quick Sort is widely used in real-world systems like database engines, search engines, and programming libraries for its speed and efficiency.

For example, Quick Sort can be used to sort customer transactions in banking apps or search results in e-commerce platforms.

Example

// Sorting transaction amounts
const transactions = [1200, 350, 890, 2200, 100];
console.log(quickSort(transactions)); // Output: [100, 350, 890, 1200, 2200]