4

Consider the following recursion tree for quick sort, which constantly divides subproblems into a 3-to-1 ratio (source: Khan Academy). Quicksort Recursion Tree

I understand that the partition subroutine in quicksort iterates through each subproblem and thus is O(n). However, I'm confused about why the "Total partitioning time for all subproblems" is cn, and not just n. What does the constant represent in this case? When/why would it be anything but 1? Is c something you can solve for, or more a "conceptual" variable?

If I understand correctly, the partition subroutine visits each element in the given subproblem exactly once. And since the sum size of all subproblems at each level is n, doesn't that mean exactly n work is done at each level (at least before the log n / log4 boundary)?

Thanks

  • 4
    Processing an element being partitioned has costs. You have to fetch the element, run the comparison, then put the value into the right bucket. The time that takes cost `c` per element being partitioned. – btilly May 17 '18 at 23:59
  • 1
    When you say "exactly n work", what does that mean? What is 1 work? I know we are talking about time, but is it a minute? a second? a microsecond? Doesn't that depend on how much you paid for you computer? – Matt Timmermans May 18 '18 at 01:18

3 Answers3

2

The constant c refers to the length of time it takes to do simple tasks, such as get an element. So, yeah, it's a conceptual variable. You will never be able to calculate c, since it's different for each iteration.

EDIT: I meant that c is different for every implementation.

OrdoFlammae
  • 721
  • 4
  • 13
  • `c` is a constant. It is not different gor every iteration. Maybe you meant it is different for use with every implementation. – rici May 18 '18 at 00:43
  • That's true. And yeah, that is exactly what I meant. Thanks for pointing that out. – OrdoFlammae May 18 '18 at 02:07
2

What does the constant represent in this case? When/why would it be anything but 1?

Let's flip the question and see when would it be exactly 1. That's a simple one to answer.

It will be 1 when there is only one step to take. So when we are doing anything other than just a single step, the value will vary.

In case of partition, some of the other things that we do are the calculation of the new boundary of the sub-partitions (if we are partitioning an array) or instantiate new lists (if we are partitioning a list).

Is c something you can solve for, or more a "conceptual" variable?

Yes, you can successfully attempt to solve for c for each level and conclude its value, but in case of Big-O, it will not be meaningful endeavor since Big-O ignores the constants.

... since the sum size of all subproblems at each level is n, doesn't that mean exactly n work is done at each level (at least before the log n / log4 boundary)?

No. As it is clear from the two sub-answers above, the work is not 1 * n, but (some book-keeping, etc. steps) * n, also equal to c * n.

displayName
  • 13,888
  • 8
  • 60
  • 75
2

When/why would it be anything but 1?

At the bottom of the log4(n) line, the total partitioning time is stated as "cn". Since the left most branch of the stack tree at this level only has one element, this implies that that are "c" operations even in the case of a single element, and that the end of every branch of the stack tree with a single element would take "c" operations. Time complexity ignores constants, so if the partition time is "c", then time complexity is O(1). If partition time is "cn", then time complexity is O(n).

The only thing changing where the stack tree goes deeper than log4(n) is the total number of elements being processed is less than n, so total partition time for that level is < "cn", and at the bottom right of the stack tree, only one element is process with partition time "c".

In this example based on quicksort, "c", isn't really a constant, since the number of swaps depends on the data and how the pivot is chosen. In the case of a single element, quicksort would normally just return without ever performing a partition step.

rcgldr
  • 27,407
  • 3
  • 36
  • 61