1

I read that the time complexity of quick select is:

T(n) = T(n/5) + T(7n/10) + O(n)

I read the above thing as "time taken to quick select from n elements = (time taken to select from 7n/10 elements)+ (time taken to quickselect from n/5 elements) + (some const *n)"

So I understand that once we find decent pivot, only 7n/10 elements are left, and doing one round of arranging the pivot takes time n.

But the n/5 part confuses me. I know it has got to do with median of medians, but i don't quite get it. Median of medians from what i understood , is recursively splitting into 5 and finding the medians, till u get 1.

I found that the time taken to do that, is about n So T of mom(n)=n

How do you equate that T of quick_select(n) = T_mom(n)/5?

In other words, this is what I think the equation should read:

T(n)= O(n)+n+T(7n/10)
where,
O(n) -> for finding median
n-> for getting the pivot into its position
T(7n/10) -> Doing the same thing for the other 7n/10 elements. (worst case)

Can someone tell me where I'm going wrong?

Mahathi Vempati
  • 1,238
  • 1
  • 12
  • 33

2 Answers2

0

In this setup, T(n) refers to the number of steps required to compute MoM on an array of n elements. Let's go through the algorithm one step at a time and see what happens.

First, we break the input into blocks of size 5, sort each block, form a new array of the medians of those blocks, and recursively call MoM to get the median of that new array. Let's see how long each of those steps takes:

  1. Break the input into blocks of size 5: this could be done in time O(1) by just implicitly partitioning the array into blocks without moving anything.

  2. Sort each block: sorting an array of any constant size takes time O(1). There are O(n) such blocks (specifically, ⌈n / 5⌉), so this takes time O(n).

  3. Get the median of each block and form a new array from those medians. The median element of each block can be found in time O(1) by just looking at the center element. There are O(n) blocks, so this step takes time O(n).

  4. Recursively call MoM on that new array. This takes time T(⌈n/5⌉), since we're making a recursive call on the array of that size we formed in the previous step.

So this means that the logic to get the actual median of medians takes time O(n) + T(⌈n/5⌉).

So where does the T(7n/10) part come from? Well, the next step in the algorithm is to use the median of medians we found in step (4) as a partition element to split the elements into elements less than that pivot and elements greater than that pivot. From there, we can determine whether we've found the element we're looking for (if it's at the right spot in the array) or whether we need to recurse on the left or right regions of the array. The advantage of picking the median of the block medians as the splitting point is that it guarantees a worst-case 70/30 split in this step between the smaller and larger elements, so if we do have to recursively continue the algorithm, in the worst case we do so with roughly 7n/10 elements.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • T(n/5) is for the mom, and T(7n/10) is for the next step of quick select. How is it justified to write it this way if the entire mom happens in every step of quick select? – Mahathi Vempati Jan 20 '17 at 03:31
  • You have to keep the two recursive calls separate in your mind. The call to a problem of size 7n/10 will in turn do a bunch of work and fire off its own recursive calls, but when writing out a recurrence relation we just focus on a single call and the calls it immediately fires off. It's only in solving the recurrence that we start to think about how all those calls add up. – templatetypedef Jan 20 '17 at 15:10
-1

In the median of median part, we do the followings:

  1. Take median of sublists which each of them has at most 5 elements. for each of this lists we need O(1) operations and there are n/5 such lists so totally it takes O(n) to just find median of each of them.
  2. We take median of those n/5 medians (median of medians). This needs T(n/5), because there are only n/5 elements which we should check.

So the median of median part is actually T(n/5) + O(n), BTW the T(7n/10) part is not exactly as what you said.

Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83