CS With NehalCS With Nehal

Sorting Algorithm Visualiser

Visualise sorting algorithms

Array Settings

Generate an array and choose whether sorted or nearly sorted arrays should be non-decreasing or non-increasing.

2 to 50 values

Custom Array

Enter comma-separated values from 1 to 100.

Algorithm

Array Visualization

Bubble Sort (Efficient) is sorting in non-decreasing order.

Frame 1/136
Normal
Comparing
Moving / Swapping
Sorted

Bubble Sort (Efficient)

Current line is highlighted.

29
0
10
1
14
2
37
3
14
4
6
5
48
6
22
7
31
8
17
9

CURRENT STEP

Start efficient bubble sort.

SPEED

700ms

TIMELINE

1

ZOOM

100%

Scroll inside the chart area to pan horizontally and vertically. Use zoom to fit more or fewer bars.

About This Simulation

This sorting algorithm visualiser shows how bubble sort, selection sort, insertion sort, merge sort, quick sort and other algorithms rearrange an array step by step, with comparisons, swaps and the matching pseudocode highlighted.

Features

  • Generate random, sorted, reversed or nearly sorted arrays
  • Every comparison and swap highlighted on the bars
  • Pseudocode with the current line highlighted
  • Playback controls: play, pause, step and speed
  • Comparison and swap counts to compare algorithms

Explanation

A sorting algorithm puts the elements of a list in order. Simple algorithms such as bubble, selection and insertion sort take O(n²) time, while divide-and-conquer algorithms such as merge sort and quick sort take O(n log n) on average.

How it works

  1. Bubble sort repeatedly swaps adjacent elements that are out of order, so the largest remaining value 'bubbles' to the end after each pass.
  2. Selection sort finds the smallest remaining element and swaps it into the next position.
  3. Insertion sort takes the next element and shifts larger sorted elements right until it can be inserted in place.
  4. Merge sort splits the array in half, sorts each half recursively and merges the two sorted halves.
  5. Quick sort picks a pivot, partitions the array into values smaller and larger than the pivot, and sorts each part recursively.

Why it matters

Sorting is one of the most common operations in computing and the standard way to learn time complexity, recursion and divide and conquer. Comparing the algorithms on the same input makes their best, average and worst cases easy to see. It is a core CSE221 topic.

Frequently Asked Questions

Which sorting algorithm is the fastest?

For general data, merge sort, heap sort and quick sort run in O(n log n). Quick sort is usually fastest in practice but has an O(n²) worst case. For small or nearly sorted arrays, insertion sort is often faster.

What is a stable sorting algorithm?

A stable sort keeps equal elements in their original relative order. Bubble sort, insertion sort and merge sort are stable; selection sort and quick sort are not, in their usual forms.

What are the time complexities of the common sorting algorithms?

Bubble, selection and insertion sort: O(n²) average and worst case (insertion and optimised bubble sort are O(n) on already sorted data). Merge sort: O(n log n) in every case. Quick sort: O(n log n) average, O(n²) worst case.

Which sorting algorithms are in-place?

Bubble, selection, insertion, quick and heap sort sort in place with O(1) or O(log n) extra space. Standard merge sort needs O(n) extra space for merging.

Topics covered

  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • Merge Sort
  • Quick Sort
  • Time Complexity of Sorting
  • Stable vs Unstable Sorting

Tags

  • sorting

Related Simulations