What is the time complexity of the quicksort algorithm?

Sorting Algorithms Questions Medium



80 Short 66 Medium 49 Long Answer Questions Question Index

What is the time complexity of the quicksort algorithm?

The time complexity of the quicksort algorithm is typically expressed as O(n log n) in the average and best cases, where n represents the number of elements being sorted. This means that the algorithm's performance grows at a rate proportional to the logarithm of the input size multiplied by the input size itself.

However, in the worst case scenario, where the pivot chosen is consistently the smallest or largest element, the time complexity can degrade to O(n^2). This occurs when the input array is already sorted or nearly sorted, leading to a highly unbalanced partitioning.

Overall, quicksort is considered to be one of the most efficient sorting algorithms due to its average-case time complexity of O(n log n) and its ability to sort in-place, requiring only a small amount of additional memory.