Sorting Algorithms - Detailed Lecture Notes
1. Introduction to Sorting
Sorting is the process of arranging data in a specific order, typically numerical or lexicographical. It
is a fundamental operation in computer science.
1 Used to improve searching efficiency
2 Improves readability of data
3 Essential in many applications such as databases and analytics
2. Classification of Sorting Algorithms
1 In-place vs Not In-place
2 Stable vs Not Stable
3 Adaptive vs Non-adaptive
4 Online vs Offline
In-place vs Not In-place
In-place sorting algorithms do not require extra memory, while not in-place algorithms require
additional storage.
Stable Sorting
Stable algorithms preserve the relative order of equal elements. This is important when sorting
structured data.
Adaptive Sorting
Adaptive algorithms take advantage of already sorted input, improving performance.
3. Bubble Sort
Bubble sort repeatedly swaps adjacent elements if they are in the wrong order.
1 Simple but inefficient
2 Time Complexity: O(n^2)
3 Stable and in-place
4. Counting Sort
Counting sort counts occurrences of elements and reconstructs the sorted output.
1 Non-comparison based
2 Efficient for small range of integers
3 Time Complexity: O(n + k)
5. Radix Sort
Radix sort processes digits of numbers from least significant to most significant.
1 Uses stable sorting internally
2 Time Complexity: O(d(n + k))
6. Bucket Sort
Bucket sort distributes elements into buckets and sorts each bucket individually.
1 Works well for uniformly distributed data
2 Average Time Complexity: O(n)
7. Shell Sort
Shell sort improves insertion sort by comparing elements far apart using a gap sequence.
1 Gap reduces over iterations
2 Time complexity depends on gap sequence
8. Choosing the Right Algorithm
1 Nearly sorted → Insertion sort
2 Reverse sorted → Merge/Heap sort
3 Many duplicates → Counting sort
4 Random large range → Quick/Merge sort
9. Conclusion
Sorting algorithms vary based on input characteristics and constraints. Understanding their
trade-offs is essential for efficient algorithm design.