■ Sorting Algorithms with Real-Life Examples
Bubble Sort
Definition: Compares each pair and swaps if needed until sorted.
Real-life Example: Arranging playing cards by swapping wrong pairs.
Time Complexity: O(n²)
Insertion Sort
Definition: Builds sorted list one element at a time.
Real-life Example: Inserting each card into the correct position in your hand.
Time Complexity: O(n²)
Selection Sort
Definition: Finds the smallest element and places it in the correct position.
Real-life Example: Picking the smallest exam paper one by one.
Time Complexity: O(n²)
Merge Sort
Definition: Divides array, sorts halves, and merges them back.
Real-life Example: Dividing and merging sorted piles of cards.
Time Complexity: O(n log n)
Quick Sort
Definition: Chooses a pivot and partitions around it.
Real-life Example: Grouping people shorter or taller than a pivot person.
Time Complexity: O(n log n)
Heap Sort
Definition: Uses a heap to repeatedly extract the largest element.
Real-life Example: Ranking tournament players from highest to lowest.
Time Complexity: O(n log n)
Counting Sort
Definition: Counts occurrences of each number to place elements in order.
Real-life Example: Sorting exam scores (0–100) by counting occurrences.
Time Complexity: O(n + k)
Summary Table
Algorithm Best Worst Stable Real-life Example
Bubble Sort O(n) O(n²) Yes Swapping wrong pairs
Insertion Sort O(n) O(n²) Yes Arranging playing cards
Selection Sort O(n²) O(n²) No Picking smallest repeatedly
Merge Sort O(n log n) O(n log n) Yes Merging sorted piles
Quick Sort O(n log n) O(n²) No Grouping around pivot
Heap Sort O(n log n) O(n log n) No Ranking winners
Counting Sort O(n+k) O(n+k) Yes Counting exam scores