Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 29th, 2003, 11:33 PM
me_no_xpert me_no_xpert is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: India
Posts: 19 me_no_xpert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
mth largest element in array

Given an array of N elements . cud any of u suggest how to find the mth largest element where 1<m<=n

thankx

Reply With Quote
  #2  
Old June 30th, 2003, 04:50 AM
heinrich's Avatar
heinrich heinrich is offline
digital sinner
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: sinner's land
Posts: 68 heinrich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 30 sec
Reputation Power: 11
Send a message via Yahoo to heinrich
use the binary method
(i'll get back with more)

Reply With Quote
  #3  
Old June 30th, 2003, 07:39 AM
mathurnitin mathurnitin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Bangalore, India
Posts: 13 mathurnitin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 10 sec
Reputation Power: 0
Send a message via Yahoo to mathurnitin
Question

Try binary search/ linear search or any of the searching methods and then get the mth member

Last edited by mathurnitin : June 30th, 2003 at 07:42 AM.

Reply With Quote
  #4  
Old June 30th, 2003, 11:24 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
for binary search, u need to sort it first
sorting will cost 'n*log(n)' for unknown range of numbers, or cost 'n' with known range using bucket sort
once you have that just pick it out of the mth position in the array.

That's the best i can think of.
You will obviously need to inspect every element which will cost you 'n' and then get it, so with a known range of numbers this is the best you will get. And without a known range, 'nlogn' is still excellent!

Cheers

Reply With Quote
  #5  
Old June 30th, 2003, 10:48 PM
me_no_xpert me_no_xpert is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: India
Posts: 19 me_no_xpert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
E'one's posted replies saying to use binary search and all.. but that wudnt really be reqd... sorting wud ensure that all elements fall in place .. jus accessing the mth element from the array would see me thro'.

Sorting is 1 of the ways i agree , but given n elements and
1<= m <=n why shud i sort elements upto m-1... when all i need is elements starting from m onwards sorted... I hope u are getting my point

Reply With Quote
  #6  
Old June 30th, 2003, 11:05 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
Quote:
Originally posted by me_no_xpert
Sorting is 1 of the ways i agree , but given n elements and
1<= m <=n why shud i sort elements upto m-1... when all i need is elements starting from m onwards sorted... I hope u are getting my point


no .. u need to check every element

for example getting the 3rd smallest element from a list
what if you added an element that is smaller than all in the list, and if u didnt check all elements (including the lsat one) then u'd have the 4th smallest instead of hte 3rd smallest

u need to sort EVERY element.

Reply With Quote
  #7  
Old July 1st, 2003, 12:21 AM
me_no_xpert me_no_xpert is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: India
Posts: 19 me_no_xpert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ok... i have been working in dis algo dis way... heap-sorting the elements... so for the 1st ele... the ele on d top of d heap is d answer...
but say i want the 3/4/5th ele... then ???

Reply With Quote
  #8  
Old July 1st, 2003, 12:39 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
ahh .. my flatmate thought of a much better way than sorting!

-pick a pivot (as in quick sort)
-split up elements to
left side = element less than pivot
right side = elements more than pivot

then you would be ablt to decide which side to continue searching on for ur element

for avg case,you need to inspect
n + n/2 + n/4 + .......
which limits to 2n which is pretty ****ing excellent!

cheers

Reply With Quote
  #9  
Old July 1st, 2003, 05:42 AM
me_no_xpert me_no_xpert is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: India
Posts: 19 me_no_xpert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
yogi,
are u asking me to apply quick sort algo??
iam still not clear what u'd written.. can u clarify plz

Reply With Quote
  #10  
Old July 1st, 2003, 05:51 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
no don't use quick sort, just use the pivot idea from it

for example, say you hve the numbers
2,4,7,8,1,3,5,6
and you wanna find the 3rd largest

you pick a pivot, say (first+last)/2 = 4
then split into those less(or equal) and thos more
less_1 = 2,4,1,3
more_1 = 7,8,5,6
now you want 3rd largest so you only need to check the right (more_1) side since the 4 largest elements are there.

so do it again .. but with 7,8,5,6
pick pivot = (7+6)/2 = 6 (after rounding)

more_2 = 7,8
less_2 = 5,6

you want third largest, and cuz the more_2 is the two largest and the more_1 is the 4 largest, you'd do it again and look for the largest in less_2

you do this recursively splitting it in half each time.
so the avg cost will be n + n/2 + n/4 + .....
which limits to 2n in cost.

Cheers

Reply With Quote
  #11  
Old July 1st, 2003, 06:46 AM
me_no_xpert me_no_xpert is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: India
Posts: 19 me_no_xpert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks yogi !

Reply With Quote
  #12  
Old July 2nd, 2003, 04:41 PM
palebear palebear is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Magrath, Alberta CANADA
Posts: 2 palebear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ummm...
I don't think yogi is lazy enough.

I am totally NOT dumping on your algorithm, but by the time you've done the grouping (into less and more groups) you could have been done finding the correct element.

I'll show you:
pick pivot (+1)
do comparisons with every element in the list (+n)
pick new pivot (+1)
re-compare etc.

Try a quicksort and index the mth largest element directly.
quicksort (+nlogn)
retrieve element (+1)

Also easier to implement since everyone on the planet's written a quicksort

-pb

Reply With Quote
  #13  
Old July 3rd, 2003, 06:33 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
haha .. I am lazy!! And I'll prove it!!

ur wrong ! - twice !
1. I am lazy!!


quicksort cost :
---------------
best : n
avg: nlogn
worst: n^2

mine:
-----
best: n (if u split it right first time)
avg: 2n = O(n)
worst = n^2

avg case is most often used and O(n) is better than O(nlogn)

QED: I am lazy!

Reply With Quote
  #14  
Old July 3rd, 2003, 07:22 AM
heinrich's Avatar
heinrich heinrich is offline
digital sinner
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: sinner's land
Posts: 68 heinrich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 30 sec
Reputation Power: 11
Send a message via Yahoo to heinrich
offtopic: lazy_yoigi....u wrote those lines...therefore u r not lazy :P

Reply With Quote
  #15  
Old July 3rd, 2003, 07:37 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
OY!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > mth largest element in array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap