Bucket Sort
Note:
This code was written during a crunch period and isn't perfect. There will
be some errant spacing, some files will be
using namespace std,
etc. But it's all still usable and can be a
handy guideline if you're learning Data Structures.
#include <vector>
/*
Bucket Sort
*/
using namespace std;
class BucketSort
{
public:
BucketSort(vector<int> x) : elements(x) {}
void bucketSort();
vector<int> elements;
};
void BucketSort::bucketSort()
{
vector<int> b[elements.size()];
for (int i = 0; i < elements.size(); i++)
{
b[bi].push_back(elements[i]);
}
for (int i = 0; i < elements.size(); i++)
sort(b[i].begin(), b[i].end());
int index = 0;
for (int i = 0; i < elements.size(); i++)
for (int j = 0; j < b[i].size(); j++)
elements[index++] = b[i][j];
}
int main(int argc, char *argv[])
{
BucketSort sorter(nummies);
cout << "Before Sort: [ " << endl;
for (int i : nummies)
{
cout << i << ' ';
}
cout << "]" << endl
<< endl;
sorter.bucketSort();
cout << "After Sort: [ " << endl;
for (int i : sorter.elements)
{
cout << i << ' ';
}
cout << "]";
}