What is the big-O notation of the quicksort algorithm?

The big-O notation of the quicksort algorithm is O(n log n) in the best case and average case, and O(n^2) in the worst case.

Quicksort is a popular sorting algorithm that uses the divide-and-conquer strategy to sort elements. The big-O notation is used to describe the performance or complexity of an algorithm. In the case of quicksort, the best and average case time complexity is O(n log n), while the worst-case time complexity is O(n^2).

The best case scenario occurs when the pivot element is always the median of the array. This means that the array is divided into two equal halves at each step, leading to a time complexity of O(n log n). This is because the algorithm divides the array into two halves at each step (which takes log n steps), and it takes n operations to find the pivot at each step.

The average case scenario is also O(n log n), even though the splits of the array are not perfectly in half. This is because the splits are still reasonably balanced, leading to a similar time complexity as the best case.

The worst case scenario occurs when the pivot element is either the smallest or the largest element in the array. This means that the array is not divided at all (or divided very unevenly), leading to a time complexity of O(n^2). This is because the algorithm has to go through all n elements at each of the n steps.

To deepen understanding of how quicksort fits within the wider context of standard algorithms, it's helpful to compare its efficiency and design with other common sorting and searching methods.

For those looking to further explore how algorithms like quicksort are constructed, the principles behind their fundamental programming constructs can provide significant insights into their operational mechanics.


It's important to note that while the worst case scenario sounds quite bad, it is quite rare in practice, especially if the pivot is chosen randomly. Therefore, quicksort is still considered a very efficient sorting algorithm in practice, despite its worst-case time complexity. Additionally, exploring more about algorithms in computer science can help contextualise where quicksort sits in the broader landscape of computational methods.

A-Level Computer Science Tutor Summary: Quicksort is a sorting method that efficiently organises elements most of the time in O(n log n) complexity, both on average and ideally. In rare cases, it can slow to O(n^2) when the pivot choice is poor. Despite this, its divide-and-conquer approach, which works by splitting the array and sorting each part, makes it a favoured choice for sorting.

Study and Practice for Free

Trusted by 100,000+ Students Worldwide

Achieve Top Grades in your Exams with our Free Resources.

Practice Questions, Study Notes, and Past Exam Papers for all Subjects!

Need help from an expert?

4.93/5 based on546 reviews

The world’s top online tutoring provider trusted by students, parents, and schools globally.

Related Computer Science a-level Answers

    Read All Answers
    Loading...