Junior — Middle
What is the time complexity of the quicksort algorithm in the worst, average, and best cases?
sobes.tech AI
Answer from AI
Time complexity of the QuickSort algorithm:
- Worst case: O(n²) — occurs when the pivot element is chosen poorly (for example, always the largest or smallest element), and the array is partitioned unevenly.
- Average case: O(n log n) — with a random choice of the pivot or good partitioning.
- Best case: O(n log n) — when the array is divided into two roughly equal parts at each step.
Thus, QuickSort generally works very efficiently, but in the worst case, it can degrade to quadratic complexity.