š¹ 1. Bubble Sort
š” Concept:
Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order.
āļø How it Works:
- Compare first and second elements
- Swap if needed
- Continue for the entire array
- Repeat until no swaps are needed
ā±ļø Complexity:
- Time: O(n²)
- Space: O(1)
ā Best For:
- Small datasets
- Learning purposes
š¹ 2. Selection Sort
š” Concept:
Find the minimum element and place it at the beginning.
āļø Steps:
- Find the smallest element in the array
- Swap it with the first element
- Repeat for the remaining array
ā±ļø Complexity:
- Time: O(n²)
- Space: O(1)
ā Best For:
- Simple implementations
š¹ 3. Insertion Sort
š” Concept:
Build the sorted array one element at a time.
āļø Steps:
- Take one element
- Insert it into its correct position in the sorted part
ā±ļø Complexity:
- Time: O(n²), Best case: O(n)
- Space: O(1)
ā Best For:
- Nearly sorted arrays
š¹ 4. Merge Sort
š” Concept:
Divide and conquer algorithm.
āļø Steps:
- Divide array into halves
- Recursively sort each half
- Merge sorted halves
ā±ļø Complexity:
- Time: O(n log n)
- Space: O(n)
ā Best For:
- Large datasets
- Stable sorting
š¹ 5. Quick Sort
š” Concept:
Pick a pivot and partition the array around it.
āļø Steps:
- Choose a pivot
- Place smaller elements to left, larger to right
- Recursively apply to subarrays
ā±ļø Complexity:
- Average: O(n log n)
- Worst: O(n²)
ā Best For:
- Fast practical sorting
š¹ 6. Heap Sort
š” Concept:
Uses a binary heap structure.
āļø Steps:
- Build a max heap
- Extract maximum repeatedly
ā±ļø Complexity:
- Time: O(n log n)
- Space: O(1)
ā Best For:
- Memory-efficient sorting
š¹ 7. Counting Sort
š” Concept:
Counts frequency of elements.
āļø Steps:
- Count occurrences
- Place elements accordingly
ā±ļø Complexity:
- Time: O(n + k)
- Space: O(k)
ā Best For:
- Small range integers
š¹ 8. Radix Sort
š” Concept:
Sort numbers digit by digit.
āļø Steps:
- Sort based on least significant digit
- Move to most significant digit
ā±ļø Complexity:
- Time: O(nk)
- Space: O(n + k)
ā Best For:
- Large numbers
Comparison Table
| Algorithm | Time Complexity | Space | Stable |
|---|---|---|---|
| Bubble Sort | O(n²) | O(1) | Yes |
| Selection Sort | O(n²) | O(1) | No |
| Insertion Sort | O(n²) | O(1) | Yes |
| Merge Sort | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(log n) | No |
| Heap Sort | O(n log n) | O(1) | No |
| Counting Sort | O(n + k) | O(k) | Yes |
| Radix Sort | O(nk) | O(n+k) | Yes |
Top comments (0)