FAQ: Quicksort: Conceptual - Quicksort Runtime

This community-built FAQ covers the “Quicksort Runtime” exercise from the lesson “Quicksort: Conceptual”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Sorting Algorithms

FAQs on the exercise Quicksort Runtime

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

When looking at the worst-case runtime for quicksort in the video. If the greatest element is chosen at each level, and the original array is 8 elements long, the performance is O(n^2). The instructor in the video asserts that roughly n elements will be touched n times. My issue here is that the number of elements touched decrease at every level from 8 to 7 to 6 to 5 and so on until one, is it still correct to say that it touches n elements at every level even though it technically doesn’t?

1 Like

I think it would be more accurate to say that the algorithm’s number of operations follows the sequence N [first level] + (N-1) [second level] + (N-2) [third level] + … + 1 = N * (N - 1 ) / 2. This is still O(N^2) but I think it describes the worst case scenario better. Let me know if i’m wrong!

2 Likes

Ok, so it turns out that even in the average N * log N case of quicksort. The instructor says it’s roughly n elements touched log n times. How rough does he mean?

1 Like

Everyone seems to agree that worst case is (n^2), & best case & average cases are (nlog(n)). “Roughly” is defined here as the time “averaged over all n ! permutations of n elements with equal probability.”

Here is the best explanation I’ve seen.

2 Likes

Thank you for all your help so far!

1 Like