Is the algorithm implemented wrongly?

Hi.
Have I implemented the Bucket_sort algorithm wrongly?
Here is my code:
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
29
30
m = atol( argv[1]);
	 base = atol( argv[2]);
	 r = atol( argv[3]);
	 
	 long iRangeSize = r - base + 1;
         
         A = new int[m];
	 int *aux = new int[iRangeSize];
	 
	 for(int j=0;j<iRangeSize;++j)
	    aux[j] = -1;
	 
 	 generate(A, m, iRangeSize, base);
 	 
 	 cout << "Array A: " << endl;
 	 
 	 for(int j=0;j<m;++j)
	 	 cout << A[j] << ", ";
 	 
	 int index_a = 0;
	    
	 //Sorting using bucket sort
	 for(int j=0;j<m;++j)
	    aux[ A[j] ] = A[j];
	    			
	 cout << "After bucket sort:" << endl;
	  	  
	  for(long j=0;j<iRangeSize;++j)
	 	 if(aux[j] != -1 )
	 	   cout << aux[j] << ", ";


It doesn't work.
What's wrong with it?
Last edited on
Alas, it is wrong.

That isn't a bucket sort... you are actually trying to do something along the lines of a counting sort, only you aren't storing the number of items in the bucket either.

http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/bucket-sort/

Remember, the following conditions apply for a bucket sort:

- there are fewer buckets than items
- a bucket may hold more than one item
- once everything is in a bucket, each bucket must be sorted

http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/counting-sort/

And for an integer counting sort, a bucket holds:

- the integer value
- the number of times the integer value appears in the input

A counting sort is a kind of bucket sort, so if it makes no difference to your professor (or wherever you got the idea to do this), then I recommend you look at the counting sort, parts 1 and 2 in the link.

If it must be a bucket sort, then you need to redesign your algorithm.


Hope this helps.

Read the links
Topic archived. No new replies allowed.