in Algorithms retagged by
1,147 views
0 votes
0 votes

in Algorithms retagged by
1.1k views

13 Comments

O(nlogn)
0
0
edited by
Apply median of median algorithm..

 

U will get time complexity O(n) only
2
2
Suppose i=n then how much time will it take to find element in min heap tree
0
0
well than i will apply bubble sort...first pass..it will give n(th) minimum in O(n)..
0
0

for sorting,worst case time complexity take O(n2).

what is sorting?

finding 1st minimum, 2nd minimum,.... ith minimum, .... (n-1)th minimum, nth minimum.

therefore ith minimum takes O(n) only..

 

0
0
why you are ignoring the worst case time complexity(O(n^2)) for sorting.it is not given that number is in sorted.order???
0
0

BASANT KUMAR, i didn't ignore that the worst case time complexity(O(n2)) for sorting.

i gave the reason why it is O(n2).

 

i mean,due to find ith element in O(n), The worst case time complexity is O(n2)

1
1
Apply median of median algorithm...it will give time complexity O(n)
0
0

neerajyadav is right. There is an algorithm with O(n) running time in worst case to find the ith smallest element in the input array of  size 'n' but it is difficult to explain here.

0
0
Can u provide link for that algorithm
0
0

i hope answer given in the manual is wrong.... it should be O( n log n )

https://gateoverflow.in/27194/tifr2014-b-9

0
0
@shaik brother How will it be O(n log n), I didn't get, please explain
0
0
As we need to go for tightest upper bound so nlogn  best possibility ....it can also be done in order of n
0
0

3 Answers

2 votes
2 votes
Here, worst case can occur if we want the $\frac{n}{2}^{th}$ smallest element and if you have a trillion of elements, you need to bother about your algorithm design.

 

So, one can think of like running $\frac{n}{2}$ passes of selection sort and giving answer as O(n), but it will be wrong because your $i$ can be anything ranging from 1 to n and if it is $\frac{n}{2}$ then, since each pass requires O(n), this will amount to total of

$\frac{n}{n}*n=O(n^2)$ considering n to be very large.

Now,I will look for the best algorithm that can sort my huge list of numbers and yes we have  O(nlogn) algorithms which can do that. So, once we have sorted our elements, we can return the $i^{th}$ smallest element in O(1) time amounting to a total runtime of O(nlogn)
edited by

4 Comments

@Ayush

u mean bubble and selection sort at same time??
0
0

each pass requires O(n)

u mean list is in descending order . Worst case means that. So, answer should be $O\left ( n^{2} \right )$ and not $O\left ( nlogn\right )$

right?

0
0
@srestha-No not at the same time. You could use any one of them if you always wanted $n-1^{th}$ smallest element every time in just O(2n) and that is O(n).
0
0
0 votes
0 votes

Array can be sorted in O(nlogn) & then i-th element can be found in O(1) using just index.

So, tightest bound of worst case ==> O(nlogn)

3 Comments

I also tried by same approach...but answer is given as O (n)
0
0
actually answer for abve qsn will be O(n) when you consider i as constant not function of n..than you will get b as answr by applying partition algo i times.i.e O(i*n)=O(n)....but in worst case i can also be equal to n...so than you can apply first pass of selection sort so that also gives O(n)..

so according to me answer would be O(n) only..please check.!
1
1

but,  O(nlogn) should be tighest bound of Worst case. (Use Merge sort!)

0
0
0 votes
0 votes
option : c is correct &  if in question mention that  (i<<n ) then option b is true

Related questions