See each comparison and swap as it happens
Step forward, backward, or auto-play
Explanations for every step
Understand time complexity differences
Click "Start Sorting" to begin the visualization. Each step will be explained here.
Bubble Sort is O(n²) in the worst case, but O(n) when the array is already sorted (with optimization).
// Optimized Bubble Sort
let swapped = true;
while (swapped) {
swapped = false;
for (let i = 0; i < n-1; i++) {
if (arr[i] > arr[i+1]) {
swap(arr, i, i+1);
swapped = true;
}
}
}
Don't use Bubble Sort for large datasets. For 10,000 elements, it needs ~100 million comparisons!
// Bad: Using Bubble Sort for large data
const largeArray = [...]; // 10000 elements
bubbleSort(largeArray); // Very slow!
// Better: Use Quick Sort or built-in sort
largeArray.sort((a, b) => a - b);
It depends on your use case. Quick Sort and Merge Sort are generally fast (O(n log n)). Use Insertion Sort for small or nearly-sorted arrays. JavaScript's built-in .sort() is usually the best choice in practice.
Understanding sorting algorithms teaches fundamental CS concepts like recursion, divide-and-conquer, and complexity analysis. These skills transfer to many other problems and are essential for technical interviews.
Yes, the visualization accurately represents how each algorithm works. However, it's simplified for educational purposes. Real implementations may include optimizations not shown here.