Sorting Algorithms Revision
Sorting Algorithms Revision
1. Faster Performance:
- Merge Sort is faster with time complexity O(n log n) compared to O(n²) for Bubble and Selection Sort.
2. Stable Sort:
- Merge Sort is stable, preserving the order of equal elements. Selection Sort is not stable.
- Suitable for large datasets. Bubble and Selection become slow with more elements.
4. Consistent Time:
- Merge Sort always runs in O(n log n) time, regardless of input order.
- Arrange elements so smaller ones are on the left and larger ones on the right of the pivot.
- This step divides the array for recursive sorting on both sides.
Example:
Min Heap:
Example:
/ \
3 5
/\ /\
7 8 10 6
Max Heap:
Example:
10
/ \
7 9
/\ /\
3 6 2 5
Use Case:
| Sorting Technique | Best Case | Average Case | Worst Case | Stable? | Extra Space |
|-------------------|-------------|--------------|------------|---------|------------------|
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | Yes | Yes (O(n)) |
Sorting Algorithms: Key Concepts & Comparisons