Sorting Algorithms Comparison
Sorting Algorithms Comparison
Algorithms
Understanding Key Differences in
Popular Sorting Methods
Introduction
• Sorting algorithms organize data in a specific
order (ascending/descending).
• They differ in:
• 1. Time Complexity
• 2. Space Complexity
• 3. Stability
• 4. Approach (Comparison-based/Non-
comparison-based)
Bubble Sort
• Definition: Repeatedly swaps adjacent
elements if they are in the wrong order.
• Best Time Complexity: O(n)
• Worst Time Complexity: O(n²)
• Space Complexity: O(1)
• Stable: Yes
• Suitable for: Small datasets, easy
implementation
Quick Sort
• Definition: Divides data into smaller partitions
around a pivot.
• Best Time Complexity: O(n log n)
• Worst Time Complexity: O(n²)
• Space Complexity: O(log n)
• Stable: No
• Suitable for: Large datasets, efficient
Insertion Sort
• Definition: Builds a sorted array one item at a
time.
• Best Time Complexity: O(n)
• Worst Time Complexity: O(n²)
• Space Complexity: O(1)
• Stable: Yes
• Suitable for: Small datasets, nearly sorted
arrays
Heap Sort
• Definition: Uses a binary heap to sort
elements.
• Best Time Complexity: O(n log n)
• Worst Time Complexity: O(n log n)
• Space Complexity: O(1)
• Stable: No
• Suitable for: Large datasets, priority queues
Selection Sort
• Definition: Repeatedly selects the smallest
element and moves it to the sorted portion.
• Best Time Complexity: O(n²)
• Worst Time Complexity: O(n²)
• Space Complexity: O(1)
• Stable: No
• Suitable for: Small datasets, simple to
implement
Shell Sort
• Definition: Generalized version of insertion
sort with gaps.
• Best Time Complexity: O(n log n)
• Worst Time Complexity: O(n²)
• Space Complexity: O(1)
• Stable: No
• Suitable for: Medium-sized datasets
Radix Sort
• Definition: Sorts numbers digit by digit (non-
comparison-based).
• Best Time Complexity: O(nk)
• Worst Time Complexity: O(nk)
• Space Complexity: O(n + k)
• Stable: Yes
• Suitable for: Large integers or strings
Merge Sort
• Definition: Divides the array into halves and
merges them in sorted order.
• Best Time Complexity: O(n log n)
• Worst Time Complexity: O(n log n)
• Space Complexity: O(n)
• Stable: Yes
• Suitable for: Large datasets, external sorting
Comparative Table
Algorithm Best Time Worst Time Space Stable Use Case
Merge Sort O(n log n) O(n log n) O(n) Yes Large datasets
Conclusion
• Sorting algorithms differ in their efficiency and
use cases.
• Choose based on:
• 1. Dataset size
• 2. Data structure
• 3. Time and space constraints