0% found this document useful (0 votes)
8 views3 pages

Sorting Comparison Corrected Merge Quick Heap

The document compares three sorting algorithms: Merge Sort, Quick Sort, and Heap Sort. Merge Sort is stable with a consistent O(n log n) time complexity, Quick Sort is the fastest on average but has a worst-case of O(n^2), and Heap Sort offers O(n log n) time complexity with O(1) space usage but is not stable. The choice of algorithm depends on the specific requirements such as stability and memory usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Sorting Comparison Corrected Merge Quick Heap

The document compares three sorting algorithms: Merge Sort, Quick Sort, and Heap Sort. Merge Sort is stable with a consistent O(n log n) time complexity, Quick Sort is the fastest on average but has a worst-case of O(n^2), and Heap Sort offers O(n log n) time complexity with O(1) space usage but is not stable. The choice of algorithm depends on the specific requirements such as stability and memory usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Comparison of Merge Sort, Quick Sort,

and Heap Sort


1. Merge Sort

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.

c) Example (Sorting [5, 2, 4, 7, 1, 3, 2, 6]):


1. Divide the array into two halves until you reach individual elements:
- [5, 2, 4, 7], [1, 3, 2, 6]
- Continue dividing: [5, 2], [4, 7], [1, 3], [2, 6]
- Single elements: [5], [2], [4], [7], [1], [3], [2], [6]

2. Merge sorted halves back together:


- [5, 2] → [2, 5], [4, 7] → [4, 7], [1, 3] → [1, 3], [2, 6] → [2, 6]
- Continue merging: [2, 5], [4, 7] → [2, 4, 5, 7], [1, 2, 3, 6] → [1, 2, 3, 6]
- Final merge: [2, 4, 5, 7], [1, 2, 3, 6] → [1, 2, 2, 3, 4, 5, 6, 7].

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.

c) Example (Sorting [3, 6, 8, 10, 1, 2, 1]):


1. Choose a pivot (e.g., 3), partition the array:
- [1, 2, 1], 3, [6, 8, 10]
2. Recursively apply the same process to the subarrays:
- [1, 2, 1] → [1, 1, 2]
- [6, 8, 10] is already sorted.
3. Final array: [1, 1, 2, 3, 6, 8, 10].

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.

c) Example (Sorting [4, 10, 3, 5, 1]):


1. Convert the array into a max-heap:
- [10, 5, 3, 4, 1]
2. Swap the root with the last element:
- [1, 5, 3, 4, 10]
3. Reheapify the array:
- [5, 4, 3, 1, 10]
4. Continue the process until sorted:
- [3, 1, 4, 5, 10].

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).

5. Conclusion: Which Algorithm to Choose?

 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.

4. Comparison of Merge Sort, Quick Sort, and Heap Sort


Aspect Merge Sort Quick Sort Heap Sort

Time Complexity O(n log n) O(n log n) O(n log n)


(Best)

Time Complexity O(n log n) O(n log n) O(n log n)


(Average)

Time Complexity O(n log n) O(n^2) O(n log n)


(Worst)

Complexity O(n) O(log n) O(1)

You might also like