Sorting Algorithms Visualization

Understand how different sorting algorithms work through interactive visualizations

Sorting Algorithms

Bubble Sort

Simple comparison-based algorithm

Merge Sort

Divide and conquer approach

Quick Sort

Efficient partitioning algorithm

Heap Sort

Uses binary heap data structure

Insertion Sort

Builds final sorted array one item at a time

Controls

Array Size

Small Large

Bubble Sort Visualization

Time: O(n²)
Space: O(1)

Current Step

Comparing elements at positions 2 and 3. Since 40 > 28, we swap them.

Pseudocode

for i = 0 to n-1:
    for j = 0 to n-i-2:
        if array[j] > array[j+1]:
            swap(array[j], array[j+1])

Mathematical Analysis

\( \text{Worst Case: } O(n^2) \)

In the worst case (reverse sorted array), bubble sort requires \( n-1 \) passes, with the \( i^{th} \) pass making \( n-i \) comparisons. The total number of comparisons is:

\( \sum_{i=1}^{n-1}(n-i) = \frac{n(n-1)}{2} = O(n^2) \)