CS205

Sorting Algorithms

Master the fundamental sorting algorithms. Learn how they work, compare their performance, and practice sorting arrays yourself.

Race ModeCompare algorithms
QuizTest your knowledge
O(n^2)Simple algorithms
O(n log n)Efficient algorithms

Simple Algorithms

O(n^2)

These algorithms are easy to understand and implement, but have quadratic time complexity. Good for small datasets or educational purposes.

Bubble Sort
Repeatedly swaps adjacent elements that are out of order. Simple but inefficient.
Time: O(n^2)
Learn
Selection Sort
Finds the minimum element and places it at the beginning. Easy to understand.
Time: O(n^2)
Learn
Insertion Sort
Builds sorted array one element at a time. Efficient for small or nearly sorted data.
Time: O(n^2)
Learn

Efficient Algorithms

O(n log n)

These algorithms are more complex but much faster for large datasets. They use divide-and-conquer or heap-based approaches to achieve optimal time complexity.

Merge Sort
Divides array in half, sorts recursively, then merges. Stable and consistent.
Time: O(n log n)
Learn
Quick Sort
Picks a pivot, partitions around it, sorts recursively. Very fast in practice.
Time: O(n log n)
Learn
Heap Sort
Builds a heap, then extracts elements. In-place with guaranteed O(n log n).
Time: O(n log n)
Learn

Algorithm Comparison

AlgorithmBestAverageWorstSpaceStable
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Selection SortO(n²)O(n²)O(n²)O(1)No
Insertion SortO(n)O(n²)O(n²)O(1)Yes
Merge SortO(n log n)O(n log n)O(n log n)O(n)Yes
Quick SortO(n log n)O(n log n)O(n²)O(log n)No
Heap SortO(n log n)O(n log n)O(n log n)O(1)No