home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1690
Discussions: 188,402, Posts: 2,243,609, Members: 232,632
Old April 18th, 2005, 01:13 AM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Apr 2005
Posts: 2
Help with a bucket sort please.

I filled an array with a random generator. and that part of my code works fine. I just can't get it sort by using a bucket sort. I have looked them up with search engines but they are only confusing me more. Could somebody please help me out with this.

dm06040 is offline   Reply With Quote
Old April 18th, 2005, 02:46 PM     #2 (permalink)
Junior Member
 
Join Date: Apr 2005
Posts: 2
Sorry I forgot to post the code I have written from my array.

Here is the code I have written to fill my array. Now I just can't figure out how sort it with a bucket sort.

[c]
#include <cstdlib>
#include <ctime>
#include <iostream.h>


int main ()

{


const int MAXELS = 20;

srand(time(NULL));
int i, numbers[MAXELS],z;
for(i=0; i<MAXELS; i++)
{


numbers[i] = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 99.00;



if (numbers[i] == z)
cout << "This is a duplicate number " <<numbers[i]<< "\n";
else
{

cout <<"\nThe code is: " <<numbers[i]<<endl;

z++;

}

}

return 0;
}
[/c]

dm06040 is offline   Reply With Quote
Old April 18th, 2005, 02:54 PM     #3 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
Join Date: Jan 2003
Location: Maryland Suburbia
Posts: 4,327
I'm not familar with bucket sort, but if you know the range of the numbers you are sorting..(which would be the case when you are randomly generating numbers like this), counting sort is an "n" algorithm and very easy to implement. Most other sorts are n*log(n) or worse.

VHockey86 is offline   Reply With Quote
Old April 19th, 2005, 03:20 PM     #4 (permalink)
may contain mild peril
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,329
What part has you stumped?

I have just hacked together a quick bucket sort method in Ruby and the process would appear to have three stages...

1. Setup your buckets.
2. Iterate over your data set and stick each value in the corresponding bucket.
3. Look in each bucket and see if you have anything in it.

My crufty code...
Code:
class Array
  def bucketsort
    buckets = Array.new((self.max + 1), 0)

    self.each { |i| buckets[i] += 1 }

    sorted = Array.new
    buckets.each_index { |index| buckets[index].times { sorted << index } }

    return sorted
  end
end
If I get time later I will try and rewrite it in C++, but I wouldn't hold your breath

Regards

ed
__________________
I dreamt that a large eagle circled the room three times and then got into bed with me and took all the blankets.

Last edited by SpookyEddy : April 19th, 2005 at 03:22 PM.
SpookyEddy is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may 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
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatica data sort out! qsd2005 Applications and Operating Systems 2 March 19th, 2005 09:18 PM
aint nothing like a bucket of.... CubKid PC Modification 20 April 29th, 2004 07:37 PM
Stealthing a floppy...sort of BeeQuewl PC Modification 4 June 21st, 2003 04:57 AM
i know nothing of the sort.... jp22382 Mobile Computing 2 November 20th, 2002 02:36 AM
More logo help...sort of fatal xception Graphic Design and Digital Photography 1 October 9th, 2002 12:09 PM

Most Active Discussions
Is It Just Me? (2906)
3-days in and no threads about Gaza (161)
Misery Loves Company... (2144)
New Build ( Finally ) (7)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
RCA 52Inch HDTV wont turn on (5)
Folderchat Weekday thread (444)
Recent Discussions
Futronix has water features? (0)
Laptop proccesor to desktop mob.. (2)
Please help! multiple problems! (4)
RCA 52Inch HDTV wont turn on (5)
New Build ( Finally ) (7)
Common Spyware Solutions (97)
How do you move a hard-drive to.. (4)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 04:34 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28