GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Apr-2005, 10:40
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

Help with a bucket sort please.


Hi I am trying to sort my array with a bucket sort. Several different kind of sorts for arrays are listed in my book but not bucket sorts. I get the concept on 97 goes in 7 and 3 goes in three when sorting by ones and then in 9 and 0 when sorting by ones but I don[t have any information on how to code a bucket sort. Please help me.
  #2  
Old 12-Apr-2005, 11:58
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

Sorry forgot to add the code I already written on earlier post.


This is the code for the array that I am trying to sort with a bucket sort.

CPP / C++ / C Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>


int main ()

{
	const int MAXELS = 20;


		int newcode,i,a;
		int id[MAXELS]={0};

	srand(time(NULL));
	a=0;
	while(a<20)
	

	{
		newcode = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 99.00;
		
		if (newcode == a)
			cout << "This is a duplicate number " <<newcode<< "\n";
		else
		{
			
			cout <<"\nThe code is: " <<newcode<<"\n";
			
			a++;
		
		}
	}
	return 0;
}
  #3  
Old 12-Apr-2005, 14:41
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Try a google search for "bucket sort code". Only 416,000 hits and at least 6 on the first page look likely.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #4  
Old 15-Apr-2005, 14:08
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

I did the search on many search engines but I am still confused.


I keep trying different ways to get this bucket sort but I don't have much to go on because their is nothing on bucket sorts in my book. I have used search engines to try to find out about them. They show me some code about them but nothing on how they work in arrays. I am just getting more confused.


CPP / C++ / C Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>



void bucketSort(int a;int size)

{
   int b[10][size-1];
   int digit =1;
     for (int j = 0;j<size;++j)
      int k=(a[j]%(10*digit)-a[j]%digit)/digit;
           b[k][k++]=a[j];
  int m=0;
  for (j=0;j<size;j++)
     {
    for (k=0;k<b[][k];k++)
        a[m++]=b[j][k];
       
       }

       for (k=0;k<size;k++)
           b[][k]=0;
         digit*=10;
}//end of the function
int main ()

{


  const int MAXELS = 20;


    int code,z;
    int id[MAXELS]={0};

  srand(time(NULL));
  z=0;
  while(z<20)
 

  {
   code = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 99.00;
    
    if (size == z)
      cout << "This is a duplicate number " <<code<< "\n";
    else
    {
      
      cout <<"\nThe code is: " <<code<<"\n";
      
      z++;
    
    }
  }
  
  return 0;
}
Last edited by LuciWiz : 16-Apr-2005 at 02:08. Reason: The way to do it is [c] code [/c]
  #5  
Old 15-Apr-2005, 17:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by glulu76
I keep trying different ways to get this bucket sort but I don't have much to go on because their is nothing on bucket sorts in my book.

The code you posted makes no sense to me. The main program doesn't create an array of random numbers, it doesn't call the sort routine, and the sort routine won't compile (not even close).

I will tell you that the expression "bucket sort" has been used in lots of ways through the ages, so lots of search engine links will have nothing to do with what I think you are trying to tell us. I know it's confusing and frustrating. Maybe you can go back through your course notes and ask the instructor for clarification of things you don't understand.

However, even after having said all that: your description makes me think you should look at This Link since the description there seems to be somewhat consistent with the way you tried to state the problem (even using the example numbers 97 and 3).

Maybe you can look there and see if you can follow the instructions. Make a real effort to put it together, and ask specific questions if you get stuck.

Regards,

Dave
  #6  
Old 17-Apr-2005, 23:02
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

Ok I think I fixed part of it.


Ok I restarted from the beginnig I think that I now have an array that is being filled with random numbers. Now I am working on the bucket sort.

CPP / C++ / C Code:
#include <cstdlib>
#include <ctime>
#include <iostream.h>





int main()
{

int j;
const int Max = 99;


srand(time(NULL));
rand();
int num_array[99] = {false}; //make a 99  element array (to accomodate
//99 possibilities) filled will the value false

for (int i = 0; i < 20; i++) {
j = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 99.00;;
j++;
if(!num_array[j-1]){ //check to see if it's been used or not
cout << j << endl; //if it's not been used, print the value
num_array[j-1]=true; //and toggle the associated array element
}
else{
i--; //reset the loop point
}
}


return 0;
}

  #7  
Old 18-Apr-2005, 00:11
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

Ok this is what I have for my bucket sort.


I tried to write a bucket code function and then call it into the generated array but I keep getting this error : missing function header (old-style formal list?)

this is my code so far.

CPP / C++ / C Code:
 #include <cstdlib>
#include <ctime>
#include <iostream.h>

void bucketSort(int a,int size);
{
   int b[10][size-1];
   int digit =1;
     for (int j = 0;j<size;++j)
      int k=(a[j]%(10*digit)-a[j]%digit)/digit;
           b[k][k++]=a[j];
  int m=0;
  for (j=0;j<size;j++)
     {
    for (k=0;k<b[][k];k++)
        a[m++]=b[j][k];
       
       }

       for (k=0;k<size;k++)
           b[][k]=0;
         digit*=10;
         }//end of the function

int main()
{

int j;
const int Max = 99;


srand(time(NULL));
rand();
int num_array[Max] = {false}; //make a 99  element array (to accomodate
//99 possibilities) filled will the value false

for (int i = 0; i < 20; i++) {
j = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 99.00;;
j++;
if(!num_array[j-1]){ //check to see if it's been used or not

void bucketSort(int a,int size);//to call bucket sort function

cout << j << endl; //if it's not been used, print the value
num_array[j-1]=true; //and toggle the associated array element
}
else{
i--; //reset the loop point
}


}


return 0;
}
I tried to look online for help and I tried to ask my instructor he just emailed me to research it on the internet. This is my first c++ class and this project is driving me insane. Please help me figure out what I am doing wrong.
  #8  
Old 18-Apr-2005, 00:51
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by glulu76
I tried to write a bucket code function and then call it into the generated array but I keep getting this error : missing function header (old-style formal list?)

Get rid of the .h on #include <iostream.h>

Search this site for additional info... use iostream.h as a search parameter and see what turns up.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #9  
Old 18-Apr-2005, 01:04
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by glulu76
I tried to write a bucket code function and then call it into the generated array but I keep getting this error : missing function header (old-style formal list?)

You have a misplaced semicolon:

CPP / C++ / C Code:
void bucketSort(int a,int size)//;
{

Also, what is size here:

CPP / C++ / C Code:
	int b[10][size-1];

I hope it's a constant.

Best regards,
Lucain
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
Last edited by LuciWiz : 18-Apr-2005 at 02:39.
  #10  
Old 18-Apr-2005, 02:47
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
I just noticed you pass a as a int to the function, but then you use it as an array:

CPP / C++ / C Code:
      int k=(a[j]%(10*digit)-a[j]%digit)/digit;

Also, you can't use an array without an indexer in C/C++ like this:

CPP / C++ / C Code:
	b [] [k]=0;

Regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 21:33
bucket sort problem ap6118 C++ Forum 3 15-Apr-2005 19:02
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 09:48
insert sort saphir55 C Programming Language 4 06-Dec-2004 15:00
help with Sort arrays/Size justachessgame C Programming Language 1 13-Nov-2004 00:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.