wu :: forums
« wu :: forums - nth order statistics where n is power of 2 »

Welcome, Guest. Please Login or Register.
May 1st, 2025, 11:46pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: SMQ, ThudnBlunder, william wu, towr, Grimbal, Eigenray, Icarus)
   nth order statistics where n is power of 2
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: nth order statistics where n is power of 2  (Read 1398 times)
singhar
Newbie
*





   


Posts: 22
nth order statistics where n is power of 2  
« on: Mar 21st, 2010, 9:21am »
Quote Quote Modify Modify

We know that sorting is O(nlogn). But is it possible to find the kth order statistics of an unsorted array faster than O(nlogn) given the fact that k is a power of 2. So we are looking for the 1st, 2nd, 4th, 8th, 16th, ... smallest numbers here.
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: nth order statistics where n is power of 2  
« Reply #1 on: Mar 21st, 2010, 9:54am »
Quote Quote Modify Modify

It doesn't matter whether k is a power of 2, you can do it in O(n) time regardless.
When you pick a pivot, you only need to work on with one of the two halves of the array. You can use median of medians to choose a good pivot which guarantees O(n) runtime (choosing a pivot randomly only gives O(n) on average)
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
mmgc
Newbie
*





   


Gender: male
Posts: 47
Re: nth order statistics where n is power of 2  
« Reply #2 on: Mar 22nd, 2010, 1:39am »
Quote Quote Modify Modify

We can traverse the array once and figure out the nth smallest element.Just need to keep track of nth smallest element.
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: nth order statistics where n is power of 2  
« Reply #3 on: Mar 22nd, 2010, 3:51am »
Quote Quote Modify Modify

on Mar 22nd, 2010, 1:39am, mmgc wrote:
We can traverse the array once and figure out the nth smallest element.Just need to keep track of nth smallest element.
And how exactly are you going to do that?
If you, say, keep a maxheap of size K (where you replace the max by the current element if the latter is smaller), you'll still take O(N log K) time to find the Kth smallest element.  
How do you plan to keep track of the Kth smallest element in O(1) time per step?
« Last Edit: Mar 22nd, 2010, 3:58am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
singhar
Newbie
*





   


Posts: 22
Re: nth order statistics where n is power of 2  
« Reply #4 on: Mar 22nd, 2010, 10:43am »
Quote Quote Modify Modify

@ towr, you mean we can find all the kth order statistics in O(n) or just any one of them? I am looking for all the kth order statistics where k is a power of 2. In other words, it is almost asking for sorting the array, but doesn't care about the elements whose rank is not a power of 2.
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: nth order statistics where n is power of 2  
« Reply #5 on: Mar 22nd, 2010, 11:22am »
Quote Quote Modify Modify

Ah, I was thinking we only needed to find one.
But I think if you want every 2kth statistic, you can use the same approach. If you split an array in two, then the last half will only contain one of them; and we can find any single kth statistic in O(n) time, so it should only take about twice as long to find all as finding just one.
« Last Edit: Mar 22nd, 2010, 11:22am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
singhar
Newbie
*





   


Posts: 22
Re: nth order statistics where n is power of 2  
« Reply #6 on: Mar 22nd, 2010, 2:14pm »
Quote Quote Modify Modify

But I think you are still right, the time complexity is still O(n) even if we are computing all the kth order statistics where k is a power of 2:
 
T(n) = C * n * (1 + 1/2 + 1/4 + .. + 1/(2^k))
= C * n * ( 2 * (1 - 1/(2 ^ (k+1)))
= C * n * ( 2 * (1 - 1 / 2n))
= C * n * ( 2 * (2n - 1) / 2*n)
= C * (2n - 1) = O(n)  
 
n is given / guranteed to be a power of 2 here.
IP Logged
mmgc
Newbie
*





   


Gender: male
Posts: 47
Re: nth order statistics where n is power of 2  
« Reply #7 on: Mar 22nd, 2010, 9:40pm »
Quote Quote Modify Modify

on Mar 22nd, 2010, 3:51am, towr wrote:

And how exactly are you going to do that?
If you, say, keep a maxheap of size K (where you replace the max by the current element if the latter is smaller), you'll still take O(N log K) time to find the Kth smallest element.  
How do you plan to keep track of the Kth smallest element in O(1) time per step?

 
I completely missed the complexity involved...thanks for making me think again..
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board