Topic wise lesson
Introduction to different sorting techniques
Sorting is the process of arranging data in a specific order, usually:
-
Ascending order (small → large)
-
Descending order (large → small)
Sorting improves searching efficiency, data organization, and overall performance of algorithms.
Classification of Sorting Techniques
Sorting algorithms are mainly classified into:
1️⃣ Internal Sorting
2️⃣ External Sorting
1️⃣ Internal Sorting
-
Entire data fits in main memory (RAM).
-
Used for smaller datasets.
Common Internal Sorting Techniques
✔ Bubble Sort
-
Repeatedly compares adjacent elements and swaps them if needed.
-
Simple but inefficient for large data.
-
Time Complexity: O(n²)
✔ Selection Sort
-
Selects the smallest element and places it in the correct position.
-
Time Complexity: O(n²)
✔ Insertion Sort
-
Inserts each element into its proper position.
-
Efficient for small or nearly sorted data.
-
Time Complexity: O(n²)
✔ Merge Sort
-
Uses Divide and Conquer technique.
-
Divides the array into halves, sorts them, and merges.
-
Time Complexity: O(n log n)
-
Stable sort.
✔ Quick Sort
-
Also uses Divide and Conquer.
-
Selects a pivot and partitions the array.
-
Average Time Complexity: O(n log n)
-
Worst Case: O(n²)
✔ Heap Sort
-
Uses Heap data structure.
-
Time Complexity: O(n log n)
-
Not stable.
2️⃣ External Sorting
-
Used when data does not fit into main memory.
-
Data is stored in external storage (disk).
✔ Example:
-
External Merge Sort
🔹 Comparison Table
| Algorithm | Best Case | Worst Case | Stable | Method |
|---|---|---|---|---|
| Bubble Sort | O |
O(n²) | Yes | Comparison |
| Selection Sort | O(n²) | O(n²) | No | Selection |
| Insertion Sort | O |
O(n²) | Yes | Insertion |
| Merge Sort | O(n log n) | O(n log n) | Yes | Divide & Conquer |
| Quick Sort | O(n log n) | O(n²) | No | Divide & Conquer |
| Heap Sort | O(n log n) | O(n log n) | No | Heap |
Based on Method Used
1️⃣ Comparison-Based Sorting
-
Bubble, Selection, Insertion, Merge, Quick, Heap
2️⃣ Non-Comparison Sorting
-
Counting Sort
-
Radix Sort
-
Bucket Sort
(Time complexity can be better than O(n log n) under certain conditions.)
Conclusion
-
For small datasets → Insertion or Bubble Sort
-
For large datasets → Merge Sort or Quick Sort
-
For memory-limited systems → Heap Sort
-
For very large external data → External Merge Sort