I read a link here about a month ago and a guy said it don't matter if you ++i or i++, and he made it clear that ++i makes cleaner code so I been using that every since and than I notices a few days ago while preparing this algorithms to my coding style the program would not compile when I did it like this DATA[++A] = B; } } My guest is the = equate operator that demand it to of standard way "A++" i++. You learn a lot of things when you tinker around. I do that with all of my spare time sometimes.
Btw: it works on my devcpp.exe even when I get the file from here. Maybe someone don't want me to seen it. The algorithms is beautiful but it seems I was barking up the wrong tree as a bucketSORT. I was told this was a counting sort... Now I'm confussed too.
I'll check it out and zip it next time. Give me a day.
Have you read the the Wikipedia Bucket Sort algorithm link I posted earlier? It's all explained there. |
kbw, yes I did but only had pesudo-code that I did not understand. Four hours of trying went down the drain. Check out the complete list below... No C++ but a little pseudo-code.
I can write some pseudo-code but I can't understand all. I am a trial and error person. I got to see the code too. Than I checked out a full working JAVA source on wiki that gave me the additional clue.
Wikipedia:
Bucket sort (PHP) (1840 bytes)
Bucket sort (Java) (2054 bytes)
Bucket sort (C) (2023 bytes)
Bucket sort (Python) (1752 bytes)
But no C++
Today I found these on my machine downloaded months. I been trying for weeks and searching for a clues with all my spare time and had it along, maybe. This sounds like true bucketSort for C++. Is one or both of these the true working buckSort algorithm for C++? Also, is what I presented in the code above is DEFINITELY is not a true buckSORT code?. I like C++ for the Vector, MAP and it, for months. I beleive these are the only type questions I had from day-1 posted here. You got to have something to love or go back to simple ASM. C++ is not that bad.
1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void bucketSort(int *inArr, int arrSize, int variance){
int *bucket = (int*)malloc(sizeof(int) * variance);
// Initialize all bucket nodes to have a count of 0
memset(bucket, 0, sizeof(int) * variance);
// Throw the count of each node into the bucket
for (int i = 0; i < arrSize; i++){
bucket[inArr[i]]++;
}
// We'll fill the new array with whatever is in the bucket
int newArrayPosition = 0;
for (int x = 0; x < variance; x++){
for (int j = 0; j < bucket[x]; j++){
inArr[j + newArrayPosition] = x;
}
newArrayPosition += bucket[x];
}
free(bucket);
}
|
2)
1 2 3 4 5 6 7 8 9 10 11 12
|
void BucketSort (unsigned int a [], unsigned int n)
{
int buckets [m];
for (unsigned int j = 0; j < m; ++j)
buckets [j] = 0;
for (unsigned int i = 0; i < n; ++i)
++buckets [a [i]];
for (unsigned int i = 0, j = 0; j < m; ++j)
for (unsigned int k = buckets [j]; k > 0; --k)
a [i++] = j;
}
|