Quick Sort Visualizer

Interactive visualization of quick sort algorithm with divide-and-conquer approach

Array Visualization

64
[0]
34
[1]
25
[2]
12
[3]
22
[4]
11
[5]
90
[6]
76
[7]
Pivot
Comparing
Swapping
Left Partition
Right Partition
Sorted
Partition: [0, 0] | Pivot Index: N/A
i: N/A | j: 0 | Recursion Depth: 0
Comparisons: 0 | Swaps: 0

Controls

SlowFast

Quick Sort Algorithm

1function quickSort(array, low, high) {
2  if (low < high) {
3    // Partition the array and get pivot index
4    const pivotIndex = partition(array, low, high);
5    
6    // Recursively sort elements before partition
7    quickSort(array, low, pivotIndex - 1);
8    
9    // Recursively sort elements after partition
10    quickSort(array, pivotIndex + 1, high);
11  }
12}
13
14function partition(array, low, high) {
15  // Choose the rightmost element as pivot
16  const pivot = array[high];
17  let i = low - 1; // Index of smaller element
18  
19  for (let j = low; j < high; j++) {
20    // If current element is smaller than pivot
21    if (array[j] < pivot) {
22      i++; // Increment index of smaller element
23      [array[i], array[j]] = [array[j], array[i]]; // Swap
24    }
25  }
26  
27  // Place pivot in correct position
28  [array[i + 1], array[high]] = [array[high], array[i + 1]];
29  return i + 1; // Return pivot index
30}
Step 1 of 11

Check if partition is valid (low < high)

About Quick Sort

Understanding the divide-and-conquer approach, partitioning strategy, and complexity analysis of quick sort

What is Quick Sort?

Quick Sort is a highly efficient divide-and-conquer sorting algorithm. It works by selecting a 'pivot' element and partitioning the array around it, ensuring all elements smaller than the pivot come before it and all larger elements come after. The algorithm then recursively applies the same process to the sub-arrays.

Advantages

  • Average O(n log n) time complexity
  • In-place sorting (O(log n) space)
  • Cache-efficient due to good locality
  • Often faster than other O(n log n) algorithms

Disadvantages

  • Worst case O(n²) time complexity
  • Not stable (doesn't preserve relative order)
  • Performance depends on pivot selection
  • Recursive overhead

Properties

Type:Divide & Conquer
Stability:Unstable
Best Case:O(n log n)
Average Case:O(n log n)
Worst Case:O(n²)
Space:O(log n)

When to Use

General Purpose Sorting

Most common sorting algorithm in practice

Large Datasets

Excellent performance on large arrays

Memory Constrained

In-place sorting with minimal extra space

Random Data

Performs well on randomly distributed data

System Sort Functions

Used in many standard library implementations

Algorithm Steps

1

Choose Pivot

Select an element as the pivot (usually last)

2

Partition

Rearrange array around pivot element

3

Place Pivot

Put pivot in its final sorted position

4

Divide

Split array into two sub-arrays

5

Conquer

Recursively sort both sub-arrays