Sorting Algorithms
Sorting Algorithms
Algorithms
Quick Sort
Overview of Quick Sort
Algorithm
Quick Sort has an average case time complexity of O(n log n),
making it efficient for large datasets. In the worst case, such as
when the smallest or largest element is consistently chosen as
the pivot, the time complexity can degrade to O(n^2).
However, with good pivot selection methods, such as choosing
the median, the performance can be optimized. Quick Sort is
also in-place, negligible additional memory usage compared to
Merge Sort, which requires additional space.
02
Merge Sort
Overview of Merge Sort
Algorithm
1. Divide the array into two halves. For example, with the array
[38, 27, 43, 3, 9, 82, 10], divide it into [38, 27, 43] and [3, 9,
82, 10].
2. Recursively apply Merge Sort to each half until each sub-
array consists of a single element.
3. Begin merging the sorted sub-arrays. For example, merge
[27, 38, 43] with [3, 9, 10, 82] sequentially, creating a sorted
array [3, 9, 10, 27, 38, 43, 82].
Time Complexity and
Performance Analysis
Merge Sort has a time complexity of O(n log n) for all cases,
making it consistently efficient, though it does require
additional space of O(n) for the temporary arrays. This
characteristic makes it less suitable for systems with limited
memory. While Merge Sort is stable and can be easily
implemented, its nature of needing extra space can be a
significant disadvantage in memory-constrained environments.
Conclusions