Hi I am supposed to write that implements a simple version of the BucketSort algorithm the only problem is I have no clue what that is. Any tips and examples on this would be awesome Thanks

No. When I attempted to Google the topic I was unable to find useful information.

Edited 6 Years Ago by programer411: n/a

I made this program but I was then told that the function prototype can only have one argument and the program that i made has three arguments can someone tell me how to make it so it will with only one argument?

#include <stdio.h>

void bucketSort(int birthYears[],int n, int max)
{
	int* sort = new int[max];
	int SIZE = n;

	for(int j = 0 ; j <= max ; j++)
	{
		sort[j] = 0; 
    }
	for(int i = 0 ; i < SIZE ; i++)
	{
		++sort[birthYears[i]];
    }	
	for(int i = 0 , j = 0 ; j <= max ; ++j)
	{
		for(int k = sort[j] ; k > 0 ; k--)
		{
			birthYears[i++] = j;
        }
   }	
	for(int i = 0 ; i < SIZE ; i++)
	{
		printf("%d ",birthYears[i]); 
    }	
	printf("\n");
}

int main()
{
	int birthYears[] = {84, 51, 92, 72, 17, 62, 1, 16, 10, 28, 99, 71, 45, 18, 61};
	int elem = sizeof(birthYears)/sizeof(int);
	
	int max = birthYears[0];
	for(int i = 0 ; i < elem ; i++)
        if(birthYears[i] > max)
           max = birthYears[i];
           
	bucketSort(birthYears, elem, max);
	getchar();
	return 0;
}

>I made this program but I was then told that the function prototype can only have one argument and the program that i made has three arguments

Told by whom?
If it was a professor, than dump him. If it was a compiler, than change a prototype.

>can someone tell me how to make it so it will with only one argument?

I'd rather change the prototype, so it it will take three arguments. BTW, can we see the prototype?

My professor told me that the prototype can only have 1 argument. And this is the function

#
void bucketSort(int birthYears[],int n, int max)

Are you allowed to use a vector? If so you just pass in the vector and you'll have the count and you can find the max in the bucketSort function itself.
Even if you can't use the vector (noting your use of printf might lead me to believe that you were trying to make a C program compiled under C++) you can still calculate max within your bucketSort function.
Failing all that you can probably tack your n onto the end of your array and just make sure you avoid it in the loops, but that could be confusing for you and inviting trouble.

Edited 6 Years Ago by jonsca: n/a

Or you can make n as a global variable
Max can be calculated in the function as well

Edited 6 Years Ago by abhimanipal: n/a

Thanks for helping guys I finished the program

Edited 6 Years Ago by programer411: n/a

This article has been dead for over six months. Start a new discussion instead.