Quick Sort Visualizer
Interactive visualization of quick sort algorithm with divide-and-conquer approach
Array Visualization
Controls
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}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
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
Choose Pivot
Select an element as the pivot (usually last)
Partition
Rearrange array around pivot element
Place Pivot
Put pivot in its final sorted position
Divide
Split array into two sub-arrays
Conquer
Recursively sort both sub-arrays