No Login Data Private Local Save

Sorting Algorithms Visualizer - Online Bubble, Quick & Merge

16
0
0
0

Sorting Algorithm Visualizer

Watch Bubble, Quick & Merge Sort in action β€” step by step

Comparisons: 0 Swaps: 0 Time: 0ms
Default Comparing Swapping Sorted Pivot Merge L Merge R
Select an algorithm and press Start to visualize sorting
Algorithm Complexity
AlgorithmBestAverageWorstSpaceStable
Bubble SortO(n)O(nΒ²)O(nΒ²)O(1)Yes
Quick SortO(n log n)O(n log n)O(nΒ²)O(log n)No
Merge SortO(n log n)O(n log n)O(n log n)O(n)Yes
Key Insights
  • Bubble Sort β€” Simplest but slowest for large datasets. Great for learning.
  • Quick Sort β€” Fastest in practice for most real-world data. Not stable.
  • Merge Sort β€” Consistent O(n log n), stable, ideal for linked lists and external sorting.
  • For small arrays (n < 50), Bubble Sort's simplicity can be acceptable.
  • Quick Sort's worst case occurs with already-sorted data and poor pivot selection.

Frequently Asked Questions

What is a sorting algorithm?

A sorting algorithm rearranges elements of a list or array into a specific order β€” typically numerical ascending or descending. Sorting is fundamental in computer science, enabling efficient searching, data analysis, and optimized algorithms that rely on ordered data.

How does Bubble Sort work?

Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them if they're in the wrong order. Larger elements "bubble up" to the end with each pass. It's intuitive but inefficient β€” O(nΒ²) in average and worst cases β€” making it impractical for large datasets.

How does Quick Sort work?

Quick Sort picks a "pivot" element, partitions the array so that smaller elements are left of the pivot and larger ones are right, then recursively sorts the sub-arrays. It averages O(n log n) and is often the fastest in practice due to cache efficiency, though worst-case is O(nΒ²).

How does Merge Sort work?

Merge Sort divides the array into halves, recursively sorts each half, then merges the sorted halves back together. It guarantees O(n log n) in all cases and is stable, but requires O(n) extra space, making it ideal when stability and predictability matter.

Which sorting algorithm is the fastest?

In practice, Quick Sort is usually fastest for general-purpose in-memory sorting due to low constant factors and cache locality. However, Merge Sort is more predictable and excels with linked lists or external data. For very small arrays, insertion sort (not shown here) can beat both.

What does "stable" sorting mean?

A stable sorting algorithm preserves the relative order of equal elements. If two items have the same key, their original order is maintained after sorting. Merge Sort and Bubble Sort are stable; Quick Sort (typical implementations) is not. Stability matters when sorting by multiple criteria.

When should I use Bubble Sort vs Quick Sort?

Use Bubble Sort only for educational purposes or tiny datasets (n < 20). For real applications, Quick Sort is the default choice for arrays in most programming languages' standard libraries. If stability or guaranteed O(n log n) is required, consider Merge Sort or Timsort.

What is in-place sorting?

An in-place sorting algorithm uses O(1) or O(log n) extra space beyond the input array. Bubble Sort is in-place (O(1)). Quick Sort is in-place (O(log n) for recursion stack). Merge Sort is not in-place β€” it requires O(n) auxiliary space for merging.