0

I need help to get a functioning code for bucketSort in C++; all codes on internet are somehow not executable.

What does this operator [] in front of vector (bucket) do? I know it creates n buckets but I am not sure if it creates n subvectors within the main vector bucket!

furthermore I can't figure out why the code is not working, please help me with that. Thank you in advance.

#include <iostream>
#include <vector>
#include <algorithm
#include <iterator>
using namespace std;


void bucketSort (vector <int> &v){

int n;
v.resize(n)


vector<int> buckets[v];

   for (int i=0; i<n; i++)
   {
      int correctIndex = n*v[i];
      buckets[correctIndex].push_back(v[i]);
   }

for (int i=0; i<n; i++)
   {
      sort(buckets[i].begin(), buckets[i].end());
   }
   int index = 0;
   for (int i = 0; i < n; i++)
   {
       for (int j = 0; j < buckets[i].size(); j++)
         arr[index++] = b[i][j];
  }
}
yohida
  • 11
  • 3
    First thing I notice: in `v.resize(n)`, what value do you think has `n`? – churill Dec 03 '19 at 09:32
  • 2
    `vector buckets[v];` is also wrong. If you want to create an array of vectors (which you probably don't want!) `v` has to be an compile time constant integer and not a vector. – churill Dec 03 '19 at 09:34
  • 2
    Your `#include buckets[v];` is wrong (unless the [] operator is overloaded elsewhere). – JL. Sanchez Dec 03 '19 at 09:55
  • If you found this code somewhere and can't get it to work with such basic errors, I suggest you try to write it yourself, which will also be nice exercise to learn C++. – churill Dec 03 '19 at 10:01

0 Answers0