Sorting Algorithms

swap_vert

Simple Sorts O(n²)

Bubble Sort

A simple comparison-based algorithm that repeatedly swaps adjacent elements if they are in the wrong order. While inefficient for large datasets, it's easy to understand and implement, making it excellent for educational purposes.

Key Features
  • O(n²) time complexity
  • O(1) space complexity
  • Stable sorting algorithm
  • Simple to understand and implement
  • Swaps adjacent elements
  • Adaptive - performs well on nearly sorted data
Best Use Cases
Teaching sorting conceptsSmall datasets (< 100 elements)Nearly sorted dataSimple implementations where code clarity mattersEmbedded systems with memory constraints
Selection Sort

Finds the minimum element in the unsorted portion and places it at the beginning. While still O(n²), it minimizes the number of swaps, making it useful when write operations are expensive.

Key Features
  • O(n²) time complexity
  • O(1) space complexity
  • Unstable sorting algorithm
  • Fewer swaps than bubble sort
  • In-place sorting
  • Finds minimum in each iteration
Best Use Cases
When write operations are costlySmall datasetsMemory-constrained environmentsWhen swap count matters more than comparisonsSimple embedded applications
Insertion Sort

Builds the sorted array one element at a time by inserting each element into its correct position. Efficient for small or nearly sorted datasets and can sort data as it's received (online algorithm).

Key Features
  • O(n²) time complexity
  • O(1) space complexity
  • Stable sorting algorithm
  • Excellent for small datasets
  • Adaptive - O(n) for nearly sorted data
  • Online algorithm - can sort streaming data
Best Use Cases
Small datasets (< 50 elements)Nearly sorted or partially sorted dataOnline sorting (streaming data)Part of hybrid algorithms (TimSort)Sorting linked lists
sort

Efficient Sorts O(n log n)

Merge Sort

A divide-and-conquer algorithm that splits the array into halves, recursively sorts them, and merges the results. Offers predictable O(n log n) performance but requires extra space for merging.

Key Features
  • O(n log n) time complexity
  • O(n) space complexity
  • Stable sorting algorithm
  • Divide-and-conquer strategy
  • Predictable performance - no worst case
  • Not in-place - requires auxiliary space
Best Use Cases
When stability is requiredSorting linked listsExternal sorting (large files)Parallel processing opportunitiesWhen worst-case O(n log n) is needed
Quick Sort

A divide-and-conquer algorithm that selects a pivot and partitions the array around it. Generally faster than other O(n log n) algorithms due to cache efficiency, though it can degrade to O(n²) with poor pivot selection.

Key Features
  • O(n log n) average time
  • O(n²) worst case time
  • O(log n) space complexity
  • Unstable sorting algorithm
  • In-place sorting
  • Pivot-based partitioning
  • Cache-friendly - good locality of reference
Best Use Cases
General-purpose sortingWhen average-case performance mattersLarge datasets with good pivot selectionSystems programmingWhen space is limited
Heap Sort

Uses a binary heap data structure to sort elements. Guarantees O(n log n) performance with O(1) space complexity, making it ideal when space is limited and worst-case performance matters.

Key Features
  • O(n log n) time complexity
  • O(1) space complexity
  • Unstable sorting algorithm
  • In-place sorting
  • Uses binary heap structure
  • No worst-case degradation
  • Not adaptive
Best Use Cases
When space is extremely limitedSystems with strict memory constraintsWhen worst-case O(n log n) is requiredEmbedded systemsReal-time systems with predictable timing
tune

Specialized Sorts

Counting Sort

A non-comparison based algorithm that counts occurrences of each element. Works only with integer keys within a limited range, but can be extremely fast (linear time) for suitable data.

Key Features
  • O(n + k) time complexity
  • O(k) space complexity
  • Stable sorting algorithm
  • Integer keys only
  • Limited range required
  • Not comparison-based
  • Linear time for suitable data
Best Use Cases
Sorting integers with small rangeAge sorting in databasesCharacter frequency countingPart of radix sortWhen range k is small compared to n
Radix Sort

Sorts integers or strings by processing individual digits or characters from least significant to most significant (LSD) or vice versa (MSD). Non-comparison based and can be very fast for appropriate data types.

Key Features
  • O(d(n + k)) time complexity
  • O(n + k) space complexity
  • Stable sorting algorithm
  • Digit-by-digit processing
  • Works with integers and strings
  • LSD (least significant digit) or MSD variants
  • Not comparison-based
Best Use Cases
Sorting fixed-length integersSorting strings of similar lengthParallel processing opportunitiesDatabase sorting operationsWhen digit count d is small
Bucket Sort

Distributes elements into buckets, sorts each bucket individually, and concatenates results. Works well with uniformly distributed data but can degrade to O(n²) if distribution is poor.

Key Features
  • O(n + k) average time
  • O(n²) worst case time
  • Space depends on bucket count
  • Requires uniform distribution
  • Divides data into buckets
  • Sorts each bucket separately
  • Not comparison-based
Best Use Cases
Uniformly distributed floating-point numbersExternal sortingParallel sorting opportunitiesWhen data distribution is knownApproximate sorting
compare_arrows

Hybrid & Practical Sorts

Tim Sort

A hybrid stable sorting algorithm combining merge sort and insertion sort. Designed to perform well on real-world data with existing order. Used as the default sort in Python and Java.

Key Features
  • O(n log n) time complexity
  • O(n) space complexity
  • Stable sorting algorithm
  • Hybrid merge sort + insertion sort
  • Optimized for real-world data
  • Exploits existing order in data
  • Default in Python and Java
Best Use Cases
General-purpose sorting in productionReal-world data with partial orderingWhen stability is requiredPython list.sort() and Java Arrays.sort()Data with natural runs
Intro Sort

A hybrid algorithm that begins with quick sort and switches to heap sort when recursion depth exceeds a threshold. Provides quick sort's average-case speed with heap sort's worst-case guarantee.

Key Features
  • O(n log n) time complexity
  • O(log n) space complexity
  • Unstable sorting algorithm
  • Hybrid quick sort + heap sort
  • Worst-case O(n log n) guarantee
  • Default in C++ STL
  • Switches algorithms to avoid worst case
Best Use Cases
C++ std::sort() implementationWhen worst-case guarantee needed but average speed mattersGeneral-purpose sortingSystems programmingWhen stability not required