Sorting Comparison Corrected Merge Quick Heap
Sorting Comparison Corrected Merge Quick Heap
a) Working:
Merge Sort is a divide and conquer algorithm. It divides the array into two halves,
recursively sorts them, and then merges the two sorted halves back together.
b) Variations:
Top-down Merge Sort: The most common implementation, where the array is divided
recursively into smaller subarrays and merged after sorting.
Bottom-up Merge Sort: The array is iteratively sorted in small chunks and merged in larger
chunks as the iteration progresses.
d) Analysis:
Worst-case time complexity: O(n log n).
Best-case time complexity: O(n log n).
Average-case time complexity: O(n log n).
Space complexity: O(n).
2. Quick Sort
a) Working:
Quick Sort is a divide and conquer algorithm. It selects a pivot element and partitions the
array such that all elements smaller than the pivot are to its left, and all elements larger are
to its right. It then recursively applies the same process to the subarrays.
b) Variations:
Randomized Quick Sort: A random pivot is chosen to avoid worst-case behavior on sorted
or nearly sorted arrays.
3-Way Quick Sort: This version divides the array into three parts: less than the pivot, equal
to the pivot, and greater than the pivot.
d) Analysis:
Worst-case time complexity: O(n^2).
Best-case time complexity: O(n log n).
Average-case time complexity: O(n log n).
Space complexity: O(log n) for recursive function calls.
3. Heap Sort
a) Working:
Heap Sort uses a binary heap data structure. The array is first converted into a max-heap,
and then the largest element is repeatedly removed and placed at the end of the array.
b) Variations:
Min-Heap Sort: A min-heap is used to sort the array in descending order.
d) Analysis:
Worst-case time complexity: O(n log n).
Best-case time complexity: O(n log n).
Average-case time complexity: O(n log n).
Space complexity: O(1).
Merge Sort is best used when stability is important, and it guarantees O(nlogn)
performance in all cases, though it uses extra space.
Quick Sort is the fastest on average, but it has a worst-case time complexity of
O(n2), which can be avoided with randomized pivots. It is often preferred for in-
place sorting due to its low space complexity.
Heap Sort is not stable, but it has a consistent time complexity of O(nlogn) and sorts
in place with O(1) space complexity. It’s ideal when memory usage is a concern,
although it's usually slower than Quick Sort in practice.