DAA Lab 3
DAA Lab 3
Description:
Bucket Sort is a sorting algorithm that distributes elements into a number of buckets, sorts each bucket
individually (often using another sorting algorithm like insertion sort), and then concatenates the sorted
buckets. It is efficient when input values are uniformly distributed over a known range.
Analysis:
- Worst case: When all elements fall into a single bucket, causing sorting within that bucket to be inefficient.
- Best case: When elements are uniformly distributed across buckets, and each bucket has very few elements.
- Average case: O(n + k), where n is the number of elements and k is the number of buckets. If an efficient
sorting algorithm like insertion sort is used inside buckets, it can perform close to O(n) for small buckets.
Optimization:
- Adaptive Bucket Size: Choosing the right number of buckets dynamically instead of a fixed count can
improve efficiency.
- Efficient Sorting Inside Buckets: If the number of elements in each bucket is small, using Insertion Sort
(O(n²)) is optimal, while Quick Sort or Merge Sort can be used for larger buckets.
Complexity Analysis Table:
Worst Case O(n²) (if all elements fall into one bucket) O(n + k)
Description:
The Dutch National Flag Algorithm is a sorting algorithm that efficiently sorts an array containing three
distinct values, typically 0, 1, and 2. It works by maintaining three pointers (low, mid, and high) and swapping
elements to place them in the correct partition. This algorithm is commonly used to solve problems involving
three-way partitioning, such as sorting colors in an array where: 0 represents red, 1 represents white and 2
represents blue.
Analysis:
- Worst case: O(n) when all elements must be moved.
- Best case: O(n) since each element is processed at most once.
- Average case: O(n) as only a single pass is required.
Optimisation:
- Efficient Memory Usage: Operates in O(1) extra space since sorting is done in place.
- Linear Time Complexity: Unlike traditional sorting algorithms (O(n log n)), this runs in O(n), making it
optimal for sorting three distinct elements.
(3)Merge Subintervals
Description:
The Merge Overlapping Intervals problem involves merging intervals that overlap into a single interval.
Given a list of intervals where each interval is represented as [start, end], the goal is to merge overlapping
intervals while maintaining non-overlapping intervals as they are.
The greedy approach ensures that we process intervals in sorted order, merging only when necessary, which
leads to an optimal O(n log n) time complexity due to sorting.
Analysis:
- Worst case: O(n log n) (due to sorting)
- Best case: O(n) (if already sorted and non-overlapping)
- Average case: O(n log n) (sorting + linear traversal)
Optimisation:
1. Sorting in O(n log n): Sorting ensures intervals are processed in order.
2. Single Pass Merging: The merging step runs in O(n), making the approach efficient.
3. In-place Merging Possible: Can be optimized further by modifying the input list.