advertisement

Sorting Algorithm Visualizer

Watch sorting algorithms execute step by step with detailed explanations of each operation.

Visual Learning

See each comparison and swap as it happens

Full Control

Step forward, backward, or auto-play

Deep Understanding

Explanations for every step

Compare Algorithms

Understand time complexity differences

Settings

Slow Fast

Algorithm Info

Bubble Sort

Repeatedly steps through the list, compares adjacent elements and swaps them if they're in the wrong order.

Time (Best) O(n)
Time (Worst) O(n²)
Space O(1)

Visualization

Step 0 / 0

Current Step Explanation

Click "Start Sorting" to begin the visualization. Each step will be explained here.

Derivation Path

0
Initial Array

How to Use

  1. Select an algorithm from the dropdown
  2. Enter your own array or click "Randomize"
  3. Click "Start Sorting" to generate steps
  4. Use the controls to step through the visualization
  5. Read the explanation for each step

Limitations

  • Maximum array size: 20 elements
  • Only positive integers supported
  • For educational purposes only
  • Performance may vary in browsers

Examples & Anti-patterns

Good Practice

Understanding Complexity

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;
    }
  }
}
Common Mistake

Using O(n²) for Large Data

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);

Frequently Asked Questions

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.